test(frontend): harness expansion + utility coverage push (F0 + F6) (#539)

* test(frontend): expand harness for store/component/contract tests

- factories: makeSubsonicSong, makeServer, makeAuthState, makeQueueState
- storeReset.ts: per-test reset for player/auth/preview/orbit stores
- mocks/subsonic.ts: realistic fixtures + stream/cover URL helpers
- mocks/browser.ts: ResizeObserver/IntersectionObserver/matchMedia/clipboard/object URLs
- mocks/tauri.ts: tauriMockListenerCount for listener-lifecycle regression tests
- renderWithProviders: pin i18n language to 'en' by default; { language } opt-out
- vitest.config: pool 'forks' + isolate to avoid module-mock + Zustand-global flake
- README: documented patterns, store-reset policy, i18n rule, isolation rationale

* test(frontend): bump utility coverage + expand hot-path gate

serverMagicString: 71→100% (encode/decode rejection branches, clipboard
fallback paths). shareLink: 69→97% (all entity kinds, queue trim, orbit
decoder, findServerIdForShareUrl). dynamicColors: 44→100% (extractCoverColors
DOM paths via Image / canvas / fetch mocks).

Gate adds shareLink.ts and dynamicColors.ts — both stable above 95%.
Comments updated for the new floor and the M4 hard-gate handoff.
This commit is contained in:
Frank Stellmacher
2026-05-11 21:11:23 +02:00
committed by GitHub
parent a228ce1c91
commit 4f9ad07d65
13 changed files with 990 additions and 35 deletions
+206 -1
View File
@@ -1,10 +1,14 @@
import { describe, expect, it } from 'vitest';
import {
PSYSONIC_SHARE_PREFIX,
decodeOrbitSharePayloadFromText,
decodeSharePayloadFromText,
encodeSharePayload,
PSYSONIC_SHARE_PREFIX,
findServerIdForShareUrl,
normalizeShareServerUrl,
} from './shareLink';
import { decodeServerMagicString, encodeServerMagicString, SERVER_MAGIC_STRING_PREFIX } from './serverMagicString';
import { makeServer } from '@/test/helpers/factories';
describe('shareLink vs serverMagicString', () => {
it('uses the same psysonic* prefix family as server invites (distinct digit)', () => {
@@ -63,3 +67,204 @@ describe('shareLink vs serverMagicString', () => {
expect(decodeServerMagicString(encoded)).toBeNull();
});
});
describe('normalizeShareServerUrl', () => {
it('returns empty string for whitespace input', () => {
expect(normalizeShareServerUrl(' ')).toBe('');
expect(normalizeShareServerUrl('')).toBe('');
});
it('strips trailing slashes', () => {
expect(normalizeShareServerUrl('https://x.example/')).toBe('https://x.example');
expect(normalizeShareServerUrl('https://x.example')).toBe('https://x.example');
});
it('prepends http:// when no scheme is given', () => {
expect(normalizeShareServerUrl('192.168.1.10:4533')).toBe('http://192.168.1.10:4533');
expect(normalizeShareServerUrl('music.local')).toBe('http://music.local');
});
it('leaves https URLs unchanged (modulo trailing slash)', () => {
expect(normalizeShareServerUrl('https://music.example.com')).toBe('https://music.example.com');
});
});
describe('encodeSharePayload — entity kinds', () => {
it('round-trips a track share', () => {
const encoded = encodeSharePayload({ srv: 'https://x.example', k: 'track', id: 't-1' });
expect(decodeSharePayloadFromText(encoded)).toEqual({
srv: 'https://x.example',
k: 'track',
id: 't-1',
});
});
it('round-trips an artist share', () => {
const encoded = encodeSharePayload({ srv: 'https://x.example', k: 'artist', id: 'ar-1' });
expect(decodeSharePayloadFromText(encoded)).toEqual({
srv: 'https://x.example',
k: 'artist',
id: 'ar-1',
});
});
it('round-trips a composer share', () => {
const encoded = encodeSharePayload({ srv: 'https://x.example', k: 'composer', id: 'co-1' });
expect(decodeSharePayloadFromText(encoded)).toEqual({
srv: 'https://x.example',
k: 'composer',
id: 'co-1',
});
});
it('trims whitespace in queue ids and drops empty ones', () => {
const encoded = encodeSharePayload({
srv: 'https://x.example',
k: 'queue',
ids: [' a ', '', 'b', ' '],
});
expect(decodeSharePayloadFromText(encoded)).toEqual({
srv: 'https://x.example',
k: 'queue',
ids: ['a', 'b'],
});
});
});
describe('decodeSharePayloadFromText — rejection paths', () => {
it('rejects text with no prefix', () => {
expect(decodeSharePayloadFromText('just text')).toBeNull();
});
it('rejects bare prefix with no token', () => {
expect(decodeSharePayloadFromText(`a ${PSYSONIC_SHARE_PREFIX} b`)).toBeNull();
});
it('rejects a payload with the wrong version', () => {
const body = JSON.stringify({ v: 2, srv: 'https://x.example', k: 'track', id: 't' });
const b64 = btoa(body).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
expect(decodeSharePayloadFromText(PSYSONIC_SHARE_PREFIX + b64)).toBeNull();
});
it('rejects a payload with non-string srv', () => {
const body = JSON.stringify({ v: 1, srv: 42, k: 'track', id: 't' });
const b64 = btoa(body).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
expect(decodeSharePayloadFromText(PSYSONIC_SHARE_PREFIX + b64)).toBeNull();
});
it('rejects an unknown entity kind', () => {
const body = JSON.stringify({ v: 1, srv: 'https://x.example', k: 'playlist', id: 'pl-1' });
const b64 = btoa(body).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
expect(decodeSharePayloadFromText(PSYSONIC_SHARE_PREFIX + b64)).toBeNull();
});
it('rejects an entity payload with an empty id', () => {
const body = JSON.stringify({ v: 1, srv: 'https://x.example', k: 'track', id: ' ' });
const b64 = btoa(body).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
expect(decodeSharePayloadFromText(PSYSONIC_SHARE_PREFIX + b64)).toBeNull();
});
it('rejects a queue payload with no ids', () => {
const body = JSON.stringify({ v: 1, srv: 'https://x.example', k: 'queue', ids: [] });
const b64 = btoa(body).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
expect(decodeSharePayloadFromText(PSYSONIC_SHARE_PREFIX + b64)).toBeNull();
});
it('rejects a queue payload where ids is not an array', () => {
const body = JSON.stringify({ v: 1, srv: 'https://x.example', k: 'queue', ids: 'a,b' });
const b64 = btoa(body).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
expect(decodeSharePayloadFromText(PSYSONIC_SHARE_PREFIX + b64)).toBeNull();
});
it('rejects a queue payload whose ids are all whitespace', () => {
const body = JSON.stringify({ v: 1, srv: 'https://x.example', k: 'queue', ids: [' ', ''] });
const b64 = btoa(body).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
expect(decodeSharePayloadFromText(PSYSONIC_SHARE_PREFIX + b64)).toBeNull();
});
it('rejects malformed base64', () => {
expect(decodeSharePayloadFromText(`${PSYSONIC_SHARE_PREFIX}!!!notbase64!!!`)).toBeNull();
});
it('refuses to surface an orbit payload via the entity decoder', () => {
const orbit = encodeSharePayload({ srv: 'https://x.example', k: 'orbit', sid: 'abcd1234' });
expect(decodeSharePayloadFromText(orbit)).toBeNull();
});
});
describe('decodeOrbitSharePayloadFromText', () => {
it('round-trips an orbit invite', () => {
const encoded = encodeSharePayload({ srv: 'https://x.example', k: 'orbit', sid: 'abcd1234' });
expect(decodeOrbitSharePayloadFromText(`come to orbit: ${encoded}`)).toEqual({
srv: 'https://x.example',
k: 'orbit',
sid: 'abcd1234',
});
});
it('lowercases the session id', () => {
const body = JSON.stringify({ v: 1, srv: 'https://x.example', k: 'orbit', sid: 'ABCD1234' });
const b64 = btoa(body).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
expect(decodeOrbitSharePayloadFromText(PSYSONIC_SHARE_PREFIX + b64)).toEqual({
srv: 'https://x.example',
k: 'orbit',
sid: 'abcd1234',
});
});
it('rejects text with no prefix', () => {
expect(decodeOrbitSharePayloadFromText('hello')).toBeNull();
});
it('rejects bare prefix with no token', () => {
expect(decodeOrbitSharePayloadFromText(`a ${PSYSONIC_SHARE_PREFIX} b`)).toBeNull();
});
it('rejects a non-orbit payload kind', () => {
const encoded = encodeSharePayload({ srv: 'https://x.example', k: 'track', id: 't' });
expect(decodeOrbitSharePayloadFromText(encoded)).toBeNull();
});
it('rejects an invalid version', () => {
const body = JSON.stringify({ v: 9, srv: 'https://x.example', k: 'orbit', sid: 'abcd1234' });
const b64 = btoa(body).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
expect(decodeOrbitSharePayloadFromText(PSYSONIC_SHARE_PREFIX + b64)).toBeNull();
});
it('rejects an empty server', () => {
const body = JSON.stringify({ v: 1, srv: '', k: 'orbit', sid: 'abcd1234' });
const b64 = btoa(body).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
expect(decodeOrbitSharePayloadFromText(PSYSONIC_SHARE_PREFIX + b64)).toBeNull();
});
it('rejects a session id that is not 8 hex characters', () => {
const sids = ['', 'abcd', 'abcd1234e', 'zzzz1234'];
for (const sid of sids) {
const body = JSON.stringify({ v: 1, srv: 'https://x.example', k: 'orbit', sid });
const b64 = btoa(body).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
expect(decodeOrbitSharePayloadFromText(PSYSONIC_SHARE_PREFIX + b64)).toBeNull();
}
});
it('rejects malformed base64', () => {
expect(decodeOrbitSharePayloadFromText(`${PSYSONIC_SHARE_PREFIX}!!!`)).toBeNull();
});
});
describe('findServerIdForShareUrl', () => {
it('matches by normalized URL', () => {
const a = makeServer({ id: 'a', url: 'https://music.example.com/' });
const b = makeServer({ id: 'b', url: 'http://other.local' });
expect(findServerIdForShareUrl([a, b], 'https://music.example.com')).toBe('a');
expect(findServerIdForShareUrl([a, b], 'http://other.local/')).toBe('b');
});
it('returns null when no server matches', () => {
const a = makeServer({ id: 'a', url: 'https://music.example.com' });
expect(findServerIdForShareUrl([a], 'https://elsewhere.example')).toBeNull();
});
it('returns null on an empty server list', () => {
expect(findServerIdForShareUrl([], 'https://x.example')).toBeNull();
});
});