Files
EventSnap/frontend/src/lib/avatar.test.ts
fabi f8cba95e49 chore(frontend): add ESLint + Prettier; fix real findings; format the tree
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>
2026-07-15 20:45:42 +02:00

81 lines
2.6 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { avatarPalette, initials } from './avatar';
describe('avatarPalette', () => {
it('returns the neutral palette for empty / nullish names', () => {
expect(avatarPalette(null)).toContain('bg-gray-100');
expect(avatarPalette(undefined)).toContain('bg-gray-100');
expect(avatarPalette('')).toContain('bg-gray-100');
});
it('is deterministic for the same name', () => {
// Same input, repeated calls AND a separately-constructed equal string — the palette
// must be a pure function of the name's characters, not of identity or call order.
expect(avatarPalette('Alice')).toBe(avatarPalette('Alice'));
expect(avatarPalette('Alice')).toBe(avatarPalette('Ali' + 'ce'));
expect(avatarPalette('Zoë Müller')).toBe(avatarPalette('Zoë Müller'));
});
it('returns a real palette entry (not neutral) for a non-empty name', () => {
expect(avatarPalette('Bob')).toMatch(/bg-(blue|purple|green|amber|rose|teal)-100/);
});
it('maps different names to different palette entries', () => {
// Without this, `name => name ? PALETTE[0] : NEUTRAL` (i.e. the hash loop deleted)
// passes every other test in this file — the palette would be a constant and every
// avatar in the app would render the same colour.
expect(avatarPalette('Alice')).not.toBe(avatarPalette('Bob'));
});
it('spreads names across the whole palette, not just one bucket', () => {
const names = [
'Alice',
'Bob',
'Carol',
'Dave',
'Erin',
'Frank',
'Grace',
'Heidi',
'Ivan',
'Judy',
'Mallory',
'Niaj',
'Olivia',
'Peggy',
'Rupert',
'Sybil',
'Trent',
'Victor',
'Walter',
'Xena'
];
const distinct = new Set(names.map((n) => avatarPalette(n)));
// 6 colours in the palette; 20 names must land on more than a couple of them. This
// catches a hash that collapses (e.g. always returns index 0, or ignores all but the
// first character in a way that clusters).
expect(distinct.size).toBeGreaterThanOrEqual(4);
});
});
describe('initials', () => {
it('returns "?" for empty / nullish / whitespace-only names', () => {
expect(initials(null)).toBe('?');
expect(initials(undefined)).toBe('?');
expect(initials('')).toBe('?');
expect(initials(' ')).toBe('?');
});
it('uses the first letter (uppercased) for a single word', () => {
expect(initials('alice')).toBe('A');
});
it('uses the first letters of the first two words', () => {
expect(initials('Alice Bob Carol')).toBe('AB');
});
it('collapses runs of whitespace between words', () => {
expect(initials(' john doe ')).toBe('JD');
});
});