test(e2e): Playwright + Vitest coverage harness and test instrumentation

Add an end-to-end and unit test suite for the SvelteKit frontend:

- Playwright e2e specs (tests/e2e/spa) with a throwaway container stack,
  codegen scenarios, and an Istanbul-based coverage report pipeline.
- Vitest unit tests across API endpoints, components, stores and composables.
- `data-testid` hooks on interactive elements (AppShell, FileViewer,
  ShareDialog, search, photos, files breadcrumbs, login/Nextcloud flows,
  public share pages) so the e2e suite can target them deterministically.
- Serve the SPA app-shell CSP from a <meta> policy (svelte.config.js) plus a
  middleware that skips the CSP header on HTML; move the Nextcloud Login Flow
  v2 grant page to the SvelteKit /nextcloud/login route.
- `just front-codegen` recipe and start-server-spa.sh harness.

Make the test environment robust and consistent:
- Install a deterministic in-memory localStorage/sessionStorage in the Vitest
  setup so storage behaves identically across Node versions (Node 26 ships a
  native Web Storage global that otherwise shadows jsdom's).
- Pin devenv to Node 26 + PostgreSQL 18 and pin every CI job to Node 26.3.0
  so the dev shell and CI run the same toolchain versions.

Repair the API/WebDAV (hurl) suite, which had drifted from the backend:
- Migrate the removed `/api/folders/{id}/listing` endpoint to `/resources`
  (cursor-paginated `{items:[{resource_type,resource}]}` shape) across the
  batch-copy, grants, nested-group, and WebDAV NC tests + the dav_helpers
  wipe routine.
- Stop photos_etag from uploading the dedup-tracked fixture so the dedup
  blob-lifecycle test can own its content-addressed blob exclusively.
- dedup_create now asserts the idempotent same-content re-upload (201 +
  existing file id) instead of the stale 409 expectation.

Generated coverage reports, nyc output and the e2e server runtime data dir
are gitignored rather than committed.
This commit is contained in:
Bradley Nelson
2026-06-21 20:03:32 -06:00
parent 0c40c69f9b
commit e3823ce470
162 changed files with 13213 additions and 554 deletions
+34
View File
@@ -0,0 +1,34 @@
import { describe, it, expect } from 'vitest';
import { iconNameFromClass, formatDate } from './display';
describe('iconNameFromClass', () => {
it('extracts the fa token without its prefix', () => {
expect(iconNameFromClass('fas fa-folder')).toBe('folder');
expect(iconNameFromClass('fa-file-pdf')).toBe('file-pdf');
});
it('skips fa-fw / fa-lg modifier tokens', () => {
expect(iconNameFromClass('fa-fw fa-image')).toBe('image');
expect(iconNameFromClass('fa-lg fa-music')).toBe('music');
});
it('falls back to "file" for missing or unrecognised input', () => {
expect(iconNameFromClass(null)).toBe('file');
expect(iconNameFromClass(undefined)).toBe('file');
expect(iconNameFromClass('')).toBe('file');
expect(iconNameFromClass('no-fa-token-here')).toBe('file');
});
});
describe('formatDate', () => {
it('formats epoch seconds and milliseconds', () => {
expect(formatDate(1_700_000_000)).toMatch(/\d{4}/); // seconds
expect(formatDate(1_700_000_000_000)).toMatch(/\d{4}/); // ms
});
it('formats ISO-8601 strings', () => {
expect(formatDate('2024-01-15')).toMatch(/2024/);
});
it('returns empty string for null/undefined/invalid', () => {
expect(formatDate(null)).toBe('');
expect(formatDate(undefined)).toBe('');
expect(formatDate('definitely not a date')).toBe('');
});
});
+30
View File
@@ -0,0 +1,30 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.mock('$lib/stores/ui.svelte', () => ({ ui: { notify: vi.fn() } }));
import { ui } from '$lib/stores/ui.svelte';
import { errorMessage, errorToast } from './errors';
describe('errorMessage', () => {
it('returns the message of an Error', () => {
expect(errorMessage(new Error('boom'))).toBe('boom');
});
it('stringifies non-Error values', () => {
expect(errorMessage('nope')).toBe('nope');
expect(errorMessage(42)).toBe('42');
expect(errorMessage(null)).toBe('null');
expect(errorMessage(undefined)).toBe('undefined');
});
});
describe('errorToast', () => {
beforeEach(() => vi.clearAllMocks());
it('raises an error toast with the normalised message', () => {
errorToast(new Error('bad'));
expect(ui.notify).toHaveBeenCalledWith('bad', 'error');
});
it('handles non-Error values', () => {
errorToast('plain');
expect(ui.notify).toHaveBeenCalledWith('plain', 'error');
});
});
+35
View File
@@ -0,0 +1,35 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { gridColumns } from './grid';
function mockMatchMedia(matches: boolean) {
vi.stubGlobal(
'matchMedia',
vi.fn().mockReturnValue({
matches,
media: '',
addEventListener: vi.fn(),
removeEventListener: vi.fn()
})
);
}
describe('gridColumns', () => {
beforeEach(() => mockMatchMedia(false));
it('returns 1 for non-positive width', () => {
expect(gridColumns(0)).toBe(1);
expect(gridColumns(-100)).toBe(1);
});
it('computes columns at desktop sizing (cardMin 200, gap 20)', () => {
expect(gridColumns(220)).toBe(1); // floor(240/220)
expect(gridColumns(440)).toBe(2); // floor(460/220)
expect(gridColumns(900)).toBe(4); // floor(920/220)
});
it('uses mobile sizing when the phone media query matches', () => {
mockMatchMedia(true);
expect(gridColumns(300)).toBe(2); // floor(308/148)
expect(gridColumns(600)).toBe(4); // floor(608/148)
});
});
+20
View File
@@ -0,0 +1,20 @@
import { describe, it, expect } from 'vitest';
import { isVideo, photoTimestamp, minimalPhotoItem } from './media';
describe('media helpers', () => {
it('isVideo detects video mime types', () => {
expect(isVideo({ mime_type: 'video/mp4' } as never)).toBe(true);
expect(isVideo({ mime_type: 'image/png' } as never)).toBe(false);
expect(isVideo({} as never)).toBe(false);
});
it('photoTimestamp scales seconds to milliseconds', () => {
expect(photoTimestamp({ sort_date: 1000 } as never)).toBe(1_000_000);
expect(photoTimestamp({ sort_date: 2_000_000_000_000 } as never)).toBe(2_000_000_000_000);
expect(photoTimestamp({ created_at: 5 } as never)).toBe(5000);
expect(photoTimestamp({} as never)).toBe(0);
});
it('minimalPhotoItem stubs a FileItem from an id', () => {
const p = minimalPhotoItem('abc');
expect(p.id).toBe('abc');
expect(p.category).toBe('image');
});
});
+39
View File
@@ -0,0 +1,39 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { relativeTimeAgo } from './time';
describe('relativeTimeAgo', () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date('2024-06-15T12:00:00Z'));
});
afterEach(() => vi.useRealTimers());
it('returns the empty label for null/undefined/empty', () => {
expect(relativeTimeAgo(null)).toBe('');
expect(relativeTimeAgo(undefined)).toBe('');
expect(relativeTimeAgo('')).toBe('');
expect(relativeTimeAgo(null, { empty: 'never' })).toBe('never');
});
it('handles unparseable input', () => {
expect(relativeTimeAgo('garbage')).toBe('');
expect(relativeTimeAgo('garbage', { invalidAsString: true })).toBe('garbage');
});
it('formats past times in the largest matching unit', () => {
expect(relativeTimeAgo(Date.now() - 2 * 31_536_000_000)).toMatch(/year/);
expect(relativeTimeAgo(Date.now() - 2 * 2_592_000_000)).toMatch(/month/);
expect(relativeTimeAgo(Date.now() - 2 * 604_800_000)).toMatch(/week/);
expect(relativeTimeAgo(Date.now() - 3 * 86_400_000)).toMatch(/day/);
expect(relativeTimeAgo(Date.now() - 3_600_000)).toMatch(/hour/);
expect(relativeTimeAgo(Date.now() - 120_000)).toMatch(/minute/);
});
it('accepts epoch seconds as well as milliseconds', () => {
expect(relativeTimeAgo(Math.floor((Date.now() - 3_600_000) / 1000))).toMatch(/hour/);
});
it('falls back to seconds for very recent timestamps', () => {
expect(relativeTimeAgo(Date.now() - 5_000)).toMatch(/second/);
});
});