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
+92 -1
View File
@@ -1,7 +1,8 @@
import { describe, expect, it } from 'vitest';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
SERVER_MAGIC_STRING_PREFIX,
DECODED_PASSWORD_VISUAL_MASK,
copyTextToClipboard,
decodeServerMagicString,
decodeServerMagicStringFromText,
encodeServerMagicString,
@@ -36,12 +37,55 @@ describe('serverMagicString', () => {
expect(decodeServerMagicString(encoded)).toEqual(original);
});
it('drops a name that becomes empty after trim', () => {
const encoded = encodeServerMagicString({
url: 'https://x.example',
username: 'u',
password: 'p',
name: ' ',
});
const decoded = decodeServerMagicString(encoded);
expect(decoded?.name).toBeUndefined();
});
it('rejects invalid input', () => {
expect(decodeServerMagicString('')).toBeNull();
expect(decodeServerMagicString('nope')).toBeNull();
expect(decodeServerMagicString(`${SERVER_MAGIC_STRING_PREFIX}%%%`)).toBeNull();
});
it('rejects an empty payload after the prefix', () => {
expect(decodeServerMagicString(SERVER_MAGIC_STRING_PREFIX)).toBeNull();
expect(decodeServerMagicString(`${SERVER_MAGIC_STRING_PREFIX} `)).toBeNull();
});
it('rejects a payload that is not JSON', () => {
// valid base64url of "not-json" → JSON.parse throws
const garbage = `${SERVER_MAGIC_STRING_PREFIX}bm90LWpzb24`;
expect(decodeServerMagicString(garbage)).toBeNull();
});
it('rejects a payload with the wrong version', () => {
const wrongVersion = btoa(JSON.stringify({ v: 2, url: 'https://x', u: 'u', w: 'p' }))
.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
expect(decodeServerMagicString(SERVER_MAGIC_STRING_PREFIX + wrongVersion)).toBeNull();
});
it('rejects a payload missing url or username', () => {
const noUrl = btoa(JSON.stringify({ v: 1, url: '', u: 'u', w: 'p' }))
.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
expect(decodeServerMagicString(SERVER_MAGIC_STRING_PREFIX + noUrl)).toBeNull();
const noUser = btoa(JSON.stringify({ v: 1, url: 'https://x', u: '', w: 'p' }))
.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
expect(decodeServerMagicString(SERVER_MAGIC_STRING_PREFIX + noUser)).toBeNull();
});
it('rejects a payload where url/username are not strings', () => {
const wrongTypes = btoa(JSON.stringify({ v: 1, url: 42, u: ['a'], w: 'p' }))
.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
expect(decodeServerMagicString(SERVER_MAGIC_STRING_PREFIX + wrongTypes)).toBeNull();
});
it('decodes invite embedded in surrounding text', () => {
const original = {
url: 'https://music.example.com',
@@ -52,4 +96,51 @@ describe('serverMagicString', () => {
expect(decodeServerMagicStringFromText(`Copy:\n${line}\nThanks`)).toEqual(original);
expect(decodeServerMagicStringFromText('no token')).toBeNull();
});
it('rejects text that contains only the bare prefix', () => {
expect(decodeServerMagicStringFromText(`prefix only: ${SERVER_MAGIC_STRING_PREFIX} done`)).toBeNull();
});
});
describe('copyTextToClipboard', () => {
const originalExecCommand = document.execCommand;
beforeEach(() => {
// setup.ts already installs a clipboard mock — start each test fresh.
vi.mocked(navigator.clipboard.writeText).mockResolvedValue();
});
afterEach(() => {
document.execCommand = originalExecCommand;
});
it('uses the modern clipboard API on success', async () => {
const ok = await copyTextToClipboard('hello');
expect(ok).toBe(true);
expect(navigator.clipboard.writeText).toHaveBeenCalledWith('hello');
});
it('falls back to execCommand("copy") when clipboard API rejects', async () => {
vi.mocked(navigator.clipboard.writeText).mockRejectedValueOnce(new Error('blocked'));
document.execCommand = vi.fn(() => true) as unknown as typeof document.execCommand;
const ok = await copyTextToClipboard('fallback-text');
expect(ok).toBe(true);
expect(document.execCommand).toHaveBeenCalledWith('copy');
});
it('returns false when both clipboard API and execCommand fail', async () => {
vi.mocked(navigator.clipboard.writeText).mockRejectedValueOnce(new Error('blocked'));
document.execCommand = vi.fn(() => {
throw new Error('not allowed');
}) as unknown as typeof document.execCommand;
const ok = await copyTextToClipboard('x');
expect(ok).toBe(false);
});
it('returns the result of execCommand even when it returns false', async () => {
vi.mocked(navigator.clipboard.writeText).mockRejectedValueOnce(new Error('blocked'));
document.execCommand = vi.fn(() => false) as unknown as typeof document.execCommand;
const ok = await copyTextToClipboard('x');
expect(ok).toBe(false);
});
});