Add frontend autoformatting and set CI to require formatted code for all languages (#6052)

# Description of Changes
Changes the strategy for autoformatting to reject PRs if they are not
formatted correctly instead of allowing them to merge and then spawning
a new PR to fix the formatting. The old strategy just caused more work
for us because we'd have to manually approve the followup PR and get it
merged, which required 2 reviewers so in practice it rarely got done and
just meant everyone's PRs ended up containing reformatting for unrelated
files, which makes code review unnecessarily difficult. If the PR's code
is not formatted correctly after this PR, a comment will be added
automatically to tell the author how to run the formatter script to fix
their code so it can go in.

This also enables autoformatting for the frontend code, using Prettier.
I've enabled it for pretty much everything in the frontend folder, other
than 3rd party files and files it doesn't make sense for. I also
excluded Markdown because it sounds likely to be more annoying to have
to autoformat the Markdown in the frontend folder but nowhere else. Open
to changing this though if people disagree.

> [!note]
> 
> Advice to reviewers: The first commit contains all of the actual logic
I've introduced (CI changes, Prettier config, etc.)
> The second commit is just the reformatting of the entire frontend
folder.
> The first commit needs proper review, the second one just give it a
spot-check that it's doing what you'd expect.
This commit is contained in:
James Brunton
2026-04-10 17:41:19 +01:00
committed by GitHub
parent 33b2b5827a
commit a3e45bc182
1359 changed files with 57785 additions and 57461 deletions
@@ -1,16 +1,16 @@
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
import { springAuth } from '@app/auth/springAuthClient';
import { startOAuthNavigation } from '@app/extensions/oauthNavigation';
import apiClient from '@app/services/apiClient';
import { AxiosError, type AxiosResponse, type InternalAxiosRequestConfig } from 'axios';
import { describe, it, expect, beforeEach, vi, afterEach } from "vitest";
import { springAuth } from "@app/auth/springAuthClient";
import { startOAuthNavigation } from "@app/extensions/oauthNavigation";
import apiClient from "@app/services/apiClient";
import { AxiosError, type AxiosResponse, type InternalAxiosRequestConfig } from "axios";
// Mock apiClient
vi.mock('@app/services/apiClient');
vi.mock('@app/extensions/oauthNavigation', () => ({
vi.mock("@app/services/apiClient");
vi.mock("@app/extensions/oauthNavigation", () => ({
startOAuthNavigation: vi.fn().mockResolvedValue(false),
}));
describe('SpringAuthClient', () => {
describe("SpringAuthClient", () => {
beforeEach(() => {
// Clear localStorage before each test
localStorage.clear();
@@ -22,8 +22,8 @@ describe('SpringAuthClient', () => {
vi.restoreAllMocks();
});
describe('getSession', () => {
it('should return null session when no JWT in localStorage', async () => {
describe("getSession", () => {
it("should return null session when no JWT in localStorage", async () => {
const result = await springAuth.getSession();
expect(result.data.session).toBeNull();
@@ -31,16 +31,16 @@ describe('SpringAuthClient', () => {
expect(apiClient.get).not.toHaveBeenCalled();
});
it('should validate JWT and return session when JWT exists', async () => {
const mockToken = 'mock-jwt-token';
it("should validate JWT and return session when JWT exists", async () => {
const mockToken = "mock-jwt-token";
const mockUser = {
id: '123',
email: 'test@example.com',
username: 'testuser',
role: 'USER',
id: "123",
email: "test@example.com",
username: "testuser",
role: "USER",
};
localStorage.setItem('stirling_jwt', mockToken);
localStorage.setItem("stirling_jwt", mockToken);
vi.mocked(apiClient.get).mockResolvedValueOnce({
status: 200,
@@ -49,7 +49,7 @@ describe('SpringAuthClient', () => {
const result = await springAuth.getSession();
expect(apiClient.get).toHaveBeenCalledWith('/api/v1/auth/me', {
expect(apiClient.get).toHaveBeenCalledWith("/api/v1/auth/me", {
headers: { Authorization: `Bearer ${mockToken}` },
suppressErrorToast: true,
skipAuthRedirect: true,
@@ -60,76 +60,64 @@ describe('SpringAuthClient', () => {
expect(result.error).toBeNull();
});
it('should clear invalid JWT on 401 error', async () => {
const mockToken = 'invalid-jwt-token';
localStorage.setItem('stirling_jwt', mockToken);
it("should clear invalid JWT on 401 error", async () => {
const mockToken = "invalid-jwt-token";
localStorage.setItem("stirling_jwt", mockToken);
const mockError = new AxiosError(
'Unauthorized',
'ERR_BAD_REQUEST',
undefined,
undefined,
{
status: 401,
statusText: 'Unauthorized',
data: {},
headers: {},
config: {} as InternalAxiosRequestConfig,
}
);
const mockError = new AxiosError("Unauthorized", "ERR_BAD_REQUEST", undefined, undefined, {
status: 401,
statusText: "Unauthorized",
data: {},
headers: {},
config: {} as InternalAxiosRequestConfig,
});
vi.mocked(apiClient.get).mockRejectedValueOnce(mockError);
const result = await springAuth.getSession();
expect(localStorage.getItem('stirling_jwt')).toBeNull();
expect(localStorage.getItem("stirling_jwt")).toBeNull();
expect(result.data.session).toBeNull();
// 401 is handled gracefully, so error should be null
expect(result.error).toBeNull();
});
it('should clear invalid JWT on 403 error', async () => {
const mockToken = 'forbidden-jwt-token';
localStorage.setItem('stirling_jwt', mockToken);
it("should clear invalid JWT on 403 error", async () => {
const mockToken = "forbidden-jwt-token";
localStorage.setItem("stirling_jwt", mockToken);
const mockError = new AxiosError(
'Forbidden',
'ERR_BAD_REQUEST',
undefined,
undefined,
{
status: 403,
statusText: 'Forbidden',
data: {},
headers: {},
config: {} as InternalAxiosRequestConfig,
}
);
const mockError = new AxiosError("Forbidden", "ERR_BAD_REQUEST", undefined, undefined, {
status: 403,
statusText: "Forbidden",
data: {},
headers: {},
config: {} as InternalAxiosRequestConfig,
});
vi.mocked(apiClient.get).mockRejectedValueOnce(mockError);
const result = await springAuth.getSession();
expect(localStorage.getItem('stirling_jwt')).toBeNull();
expect(localStorage.getItem("stirling_jwt")).toBeNull();
expect(result.data.session).toBeNull();
// 403 is handled gracefully, so error should be null
expect(result.error).toBeNull();
});
});
describe('signInWithPassword', () => {
it('should successfully sign in with email and password', async () => {
describe("signInWithPassword", () => {
it("should successfully sign in with email and password", async () => {
const credentials = {
email: 'test@example.com',
password: 'password123',
email: "test@example.com",
password: "password123",
};
const mockToken = 'new-jwt-token';
const mockToken = "new-jwt-token";
const mockUser = {
id: '123',
id: "123",
email: credentials.email,
username: credentials.email,
role: 'USER',
role: "USER",
};
vi.mocked(apiClient.post).mockResolvedValueOnce({
@@ -144,34 +132,32 @@ describe('SpringAuthClient', () => {
} as unknown as AxiosResponse);
// Spy on window.dispatchEvent
const dispatchEventSpy = vi.spyOn(window, 'dispatchEvent');
const dispatchEventSpy = vi.spyOn(window, "dispatchEvent");
const result = await springAuth.signInWithPassword(credentials);
expect(apiClient.post).toHaveBeenCalledWith(
'/api/v1/auth/login',
"/api/v1/auth/login",
{
username: credentials.email,
password: credentials.password,
},
{ withCredentials: true }
);
expect(localStorage.getItem('stirling_jwt')).toBe(mockToken);
expect(dispatchEventSpy).toHaveBeenCalledWith(
expect.objectContaining({ type: 'jwt-available' })
{ withCredentials: true },
);
expect(localStorage.getItem("stirling_jwt")).toBe(mockToken);
expect(dispatchEventSpy).toHaveBeenCalledWith(expect.objectContaining({ type: "jwt-available" }));
expect(result.user).toEqual(mockUser);
expect(result.session?.access_token).toBe(mockToken);
expect(result.error).toBeNull();
});
it('should return error on failed login', async () => {
it("should return error on failed login", async () => {
const credentials = {
email: 'wrong@example.com',
password: 'wrongpassword',
email: "wrong@example.com",
password: "wrongpassword",
};
const errorMessage = 'Invalid credentials';
const errorMessage = "Invalid credentials";
const mockError = Object.assign(new Error(errorMessage), {
isAxiosError: true,
response: {
@@ -191,18 +177,18 @@ describe('SpringAuthClient', () => {
});
});
describe('signUp', () => {
it('should successfully register new user', async () => {
describe("signUp", () => {
it("should successfully register new user", async () => {
const credentials = {
email: 'newuser@example.com',
password: 'newpassword123',
email: "newuser@example.com",
password: "newpassword123",
};
const mockUser = {
id: '456',
id: "456",
email: credentials.email,
username: credentials.email,
role: 'USER',
role: "USER",
};
vi.mocked(apiClient.post).mockResolvedValueOnce({
@@ -213,25 +199,25 @@ describe('SpringAuthClient', () => {
const result = await springAuth.signUp(credentials);
expect(apiClient.post).toHaveBeenCalledWith(
'/api/v1/user/register',
"/api/v1/user/register",
{
username: credentials.email,
password: credentials.password,
},
{ withCredentials: true }
{ withCredentials: true },
);
expect(result.user).toEqual(mockUser);
expect(result.session).toBeNull(); // No auto-login on signup
expect(result.error).toBeNull();
});
it('should return error on failed registration', async () => {
it("should return error on failed registration", async () => {
const credentials = {
email: 'existing@example.com',
password: 'password123',
email: "existing@example.com",
password: "password123",
};
const errorMessage = 'User already exists';
const errorMessage = "User already exists";
const mockError = Object.assign(new Error(errorMessage), {
isAxiosError: true,
response: {
@@ -251,10 +237,10 @@ describe('SpringAuthClient', () => {
});
});
describe('signOut', () => {
it('should successfully sign out and clear JWT', async () => {
const mockToken = 'jwt-to-clear';
localStorage.setItem('stirling_jwt', mockToken);
describe("signOut", () => {
it("should successfully sign out and clear JWT", async () => {
const mockToken = "jwt-to-clear";
localStorage.setItem("stirling_jwt", mockToken);
vi.mocked(apiClient.post).mockResolvedValueOnce({
status: 200,
@@ -264,39 +250,39 @@ describe('SpringAuthClient', () => {
const result = await springAuth.signOut();
expect(apiClient.post).toHaveBeenCalledWith(
'/api/v1/auth/logout',
"/api/v1/auth/logout",
null,
expect.objectContaining({ withCredentials: true })
expect.objectContaining({ withCredentials: true }),
);
expect(localStorage.getItem('stirling_jwt')).toBeNull();
expect(localStorage.getItem("stirling_jwt")).toBeNull();
expect(result.error).toBeNull();
});
it('should clear JWT even if logout request fails', async () => {
const mockToken = 'jwt-to-clear';
localStorage.setItem('stirling_jwt', mockToken);
it("should clear JWT even if logout request fails", async () => {
const mockToken = "jwt-to-clear";
localStorage.setItem("stirling_jwt", mockToken);
vi.mocked(apiClient.post).mockRejectedValueOnce({
isAxiosError: true,
response: { status: 500 },
message: 'Server error',
message: "Server error",
});
const result = await springAuth.signOut();
expect(localStorage.getItem('stirling_jwt')).toBeNull();
expect(localStorage.getItem("stirling_jwt")).toBeNull();
expect(result.error).toBeTruthy();
});
});
describe('refreshSession', () => {
it('should refresh JWT token successfully', async () => {
const newToken = 'refreshed-jwt-token';
describe("refreshSession", () => {
it("should refresh JWT token successfully", async () => {
const newToken = "refreshed-jwt-token";
const mockUser = {
id: '123',
email: 'test@example.com',
username: 'testuser',
role: 'USER',
id: "123",
email: "test@example.com",
username: "testuser",
role: "USER",
};
vi.mocked(apiClient.post).mockResolvedValueOnce({
@@ -312,33 +298,33 @@ describe('SpringAuthClient', () => {
const result = await springAuth.refreshSession();
expect(localStorage.getItem('stirling_jwt')).toBe(newToken);
expect(localStorage.getItem("stirling_jwt")).toBe(newToken);
// Note: refreshSession does not dispatch jwt-available event, only notifies listeners
expect(result.data.session?.access_token).toBe(newToken);
expect(result.error).toBeNull();
});
it('should clear JWT and return error on 401', async () => {
localStorage.setItem('stirling_jwt', 'expired-token');
it("should clear JWT and return error on 401", async () => {
localStorage.setItem("stirling_jwt", "expired-token");
vi.mocked(apiClient.post).mockRejectedValueOnce({
isAxiosError: true,
response: { status: 401 },
message: 'Token expired',
message: "Token expired",
});
const result = await springAuth.refreshSession();
expect(localStorage.getItem('stirling_jwt')).toBeNull();
expect(localStorage.getItem("stirling_jwt")).toBeNull();
expect(result.data.session).toBeNull();
expect(result.error).toBeTruthy();
});
});
describe('signInWithOAuth', () => {
it('should redirect to OAuth provider', async () => {
describe("signInWithOAuth", () => {
it("should redirect to OAuth provider", async () => {
const mockAssign = vi.fn();
Object.defineProperty(window, 'location', {
Object.defineProperty(window, "location", {
value: { assign: mockAssign },
writable: true,
});
@@ -346,18 +332,18 @@ describe('SpringAuthClient', () => {
vi.mocked(startOAuthNavigation).mockResolvedValueOnce(false);
const result = await springAuth.signInWithOAuth({
provider: '/oauth2/authorization/github',
options: { redirectTo: '/auth/callback' },
provider: "/oauth2/authorization/github",
options: { redirectTo: "/auth/callback" },
});
expect(startOAuthNavigation).toHaveBeenCalledWith('/oauth2/authorization/github');
expect(mockAssign).toHaveBeenCalledWith('/oauth2/authorization/github');
expect(startOAuthNavigation).toHaveBeenCalledWith("/oauth2/authorization/github");
expect(mockAssign).toHaveBeenCalledWith("/oauth2/authorization/github");
expect(result.error).toBeNull();
});
it('should skip redirect when handled by extension', async () => {
it("should skip redirect when handled by extension", async () => {
const mockAssign = vi.fn();
Object.defineProperty(window, 'location', {
Object.defineProperty(window, "location", {
value: { assign: mockAssign },
writable: true,
});
@@ -365,11 +351,11 @@ describe('SpringAuthClient', () => {
vi.mocked(startOAuthNavigation).mockResolvedValueOnce(true);
const result = await springAuth.signInWithOAuth({
provider: '/oauth2/authorization/github',
options: { redirectTo: '/auth/callback' },
provider: "/oauth2/authorization/github",
options: { redirectTo: "/auth/callback" },
});
expect(startOAuthNavigation).toHaveBeenCalledWith('/oauth2/authorization/github');
expect(startOAuthNavigation).toHaveBeenCalledWith("/oauth2/authorization/github");
expect(mockAssign).not.toHaveBeenCalled();
expect(result.error).toBeNull();
});