The frontend had no JS/TS linter — only svelte-check. Add flat-config ESLint (typescript-eslint
+ eslint-plugin-svelte) and Prettier (tabs/single-quote, matching the existing style).
Rules encode "catch bugs, not enforce taste":
- svelte/require-each-key KEPT — it is the exact bug class as the feed mis-tap fix. Fixed every
flagged block: keyed activeFilters, filteredUsers, stagedFiles (by previewUrl), captionTags,
admin tabs/jobs/users, the export-viewer suggestions/filters/comments, and the static skeleton
loops.
- svelte/prefer-svelte-reactivity KEPT — inline-disabled only the verified-safe sites (a local
freq Map in a $derived.by, throwaway URLSearchParams query builders), with a reason each.
- svelte/no-navigation-without-resolve OFF — wants resolve() around every goto()/href; taste, not
a bug, and pure churn.
- svelte/no-unused-svelte-ignore OFF — those comments are consumed by svelte-check, which ESLint
can't see, so it wrongly calls them unused; removing them would reintroduce a11y warnings.
- no-explicit-any OFF for *.test.ts only (partial fixtures legitimately use any).
Real code fixes beyond keys: removed a dead jobLabel(), an unused ViewerComment import and unused
catch binding, an unused scroll-lock arg, and replaced an empty interface with a type alias.
Then `prettier --write` (54 files). Formatting only. Verified: eslint clean, svelte-check 0 errors,
vitest 46 passed, vite build succeeds.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
118 lines
3.9 KiB
TypeScript
118 lines
3.9 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
|
|
// auth.ts guards every localStorage access behind `browser`; force it true so the
|
|
// jsdom localStorage is actually used (the shared mock stubs it to false).
|
|
vi.mock('$app/environment', () => ({
|
|
browser: true,
|
|
dev: false,
|
|
building: false,
|
|
version: 'test'
|
|
}));
|
|
|
|
import {
|
|
setAuth,
|
|
setAdminAuth,
|
|
getToken,
|
|
getPin,
|
|
getExpiry,
|
|
getRole,
|
|
getUserId,
|
|
clearAuth,
|
|
clearPin
|
|
} from './auth';
|
|
|
|
/** Build a JWT-shaped string (header.payload.sig) with the given claims. */
|
|
function makeJwt(claims: object): string {
|
|
const seg = (o: object) => btoa(JSON.stringify(o));
|
|
return `${seg({ alg: 'HS256', typ: 'JWT' })}.${seg(claims)}.sig`;
|
|
}
|
|
|
|
beforeEach(() => {
|
|
localStorage.clear();
|
|
sessionStorage.clear();
|
|
});
|
|
|
|
describe('auth — token storage', () => {
|
|
it('setAuth then getToken/getPin round-trips', () => {
|
|
setAuth('a.b.c', '1234', 'uid-1', 'Alice');
|
|
expect(getToken()).toBe('a.b.c');
|
|
expect(getPin()).toBe('1234');
|
|
});
|
|
|
|
it('setAuth without a PIN leaves the PIN unset', () => {
|
|
setAuth('a.b.c', null, 'uid-1');
|
|
expect(getToken()).toBe('a.b.c');
|
|
expect(getPin()).toBeNull();
|
|
});
|
|
|
|
it('clearAuth removes the token but KEEPS the PIN (needed for recovery)', () => {
|
|
setAuth('a.b.c', '1234', 'uid');
|
|
clearAuth();
|
|
expect(getToken()).toBeNull();
|
|
expect(getPin()).toBe('1234');
|
|
});
|
|
|
|
it('clearPin removes the cached PIN', () => {
|
|
setAuth('a.b.c', '1234', 'uid');
|
|
clearPin();
|
|
expect(getPin()).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('auth — admin (sessionStorage) vs guest (localStorage) identity displacement', () => {
|
|
// Regression guard for a privilege-escalation: reads are sessionStorage-first, so a guest
|
|
// login MUST displace any resident admin token (else the guest silently runs as admin on a
|
|
// shared device), and symmetrically an admin login must displace a resident guest token.
|
|
it('a guest login displaces a resident admin session (no privilege escalation)', () => {
|
|
setAdminAuth(makeJwt({ role: 'admin' }), 'admin-uid', 'Admin');
|
|
expect(getRole()).toBe('admin');
|
|
|
|
setAuth(makeJwt({ role: 'guest' }), '1234', 'guest-uid', 'Guest');
|
|
expect(getRole()).toBe('guest'); // NOT admin
|
|
expect(getUserId()).toBe('guest-uid');
|
|
expect(sessionStorage.getItem('eventsnap_jwt')).toBeNull(); // admin token cleared
|
|
});
|
|
|
|
it('an admin login displaces a resident guest session but keeps the guest PIN', () => {
|
|
setAuth(makeJwt({ role: 'guest' }), '1234', 'guest-uid', 'Guest');
|
|
setAdminAuth(makeJwt({ role: 'admin' }), 'admin-uid', 'Admin');
|
|
expect(getRole()).toBe('admin');
|
|
expect(getUserId()).toBe('admin-uid');
|
|
expect(localStorage.getItem('eventsnap_jwt')).toBeNull(); // guest token cleared
|
|
expect(getPin()).toBe('1234'); // PIN preserved for later recovery
|
|
});
|
|
|
|
it('clearAuth wipes both stores', () => {
|
|
setAdminAuth(makeJwt({ role: 'admin' }), 'admin-uid', 'Admin');
|
|
clearAuth();
|
|
expect(getToken()).toBeNull();
|
|
expect(sessionStorage.getItem('eventsnap_jwt')).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('auth — JWT claim decode', () => {
|
|
it('getExpiry decodes the exp claim (seconds → ms Date)', () => {
|
|
const exp = 2_000_000_000; // far-future unix seconds
|
|
setAuth(makeJwt({ exp }), null, 'u');
|
|
expect(getExpiry()?.getTime()).toBe(exp * 1000);
|
|
});
|
|
|
|
it('getExpiry is null with no token, no exp claim, or a malformed token', () => {
|
|
expect(getExpiry()).toBeNull(); // no token
|
|
setAuth(makeJwt({ role: 'guest' }), null, 'u'); // token without exp
|
|
expect(getExpiry()).toBeNull();
|
|
localStorage.setItem('eventsnap_jwt', 'not-a-jwt'); // unparseable
|
|
expect(getExpiry()).toBeNull();
|
|
});
|
|
|
|
it('getRole extracts the role claim; null when absent or malformed', () => {
|
|
setAuth(makeJwt({ role: 'host' }), null, 'u');
|
|
expect(getRole()).toBe('host');
|
|
setAuth(makeJwt({ exp: 1 }), null, 'u'); // no role claim
|
|
expect(getRole()).toBeNull();
|
|
localStorage.setItem('eventsnap_jwt', 'x.y.z'); // unparseable payload
|
|
expect(getRole()).toBeNull();
|
|
});
|
|
});
|