ext: popup, options, and the panel bridge (build order step 6)
Popup and Options are separate documents from the background page and can't reach its live VeloxTransport directly, so background/bridge.ts relays it over one browser.runtime.connect port per document (call/subscribe/getStatus/reconnect/pair/unpair/setOverride in; result/event/status/pairError out). shared/panel-client.ts is the client side both surfaces use. Popup (src/popup/): status dot + text, active-downloads list driven by event.task.progress/added/state/removed (repaints ride the event's own <=4 Hz cap rather than adding a second timer), pause/resume buttons, "Start it" wired to a reconnect request. State lives in a DOM-free store.ts for unit testing; rendering uses createElement, not innerHTML (web-ext lint flags the latter). Options (src/options/): transport override select, pairing (code entry + pair/unpair, backed by two new WebSocketTransport methods, pairWithCode/unpair), and the daemon's capture policy mirrored via capture.getRules. The capture-policy form is editable only when the active transport is native messaging (uds) -- settings.set and rules.upsert are privileged, uds-only methods per shared/protocol METHODS, so WebSocket can't write them no matter what the page shows; CLAUDE.md section 2 rules out working around that locally. The bridge's status payload adds a kind field (which transport is live) for this to key off. Default category is the one piece of state that's genuinely the extension's own, not the daemon's, and lives in browser.storage.local via options/prefs.ts. Pure decisions (statusLine, pairingAvailable, captureRulesEditable) are split into view.ts for unit testing without a DOM. manifest.json registers the popup action and options_ui page (and, in the same edit, the content_scripts entry the next commit's media detection needs -- split by file, not by manifest line). build.mjs gains popup/options as further esbuild entry points, plus copying their static HTML/CSS into dist/. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Ed8KEmAW48v4YHdxLtqsMB
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { formatBytes, formatEta, formatSpeed, PopupStore, progressPercent } from '../../src/popup/store.js';
|
||||
import type { TaskSummary } from '../../src/shared/protocol/index.js';
|
||||
|
||||
function summary(over: Partial<TaskSummary> = {}): TaskSummary {
|
||||
return {
|
||||
taskId: 't1',
|
||||
filename: 'file.zip',
|
||||
saveDir: '/home/u/Downloads',
|
||||
url: 'https://example.com/file.zip',
|
||||
downloadedBytes: 0,
|
||||
state: 'downloading',
|
||||
speedBps: 0,
|
||||
resumable: true,
|
||||
segments: 1,
|
||||
createdAt: '2026-01-01T00:00:00Z',
|
||||
...over,
|
||||
};
|
||||
}
|
||||
|
||||
describe('PopupStore', () => {
|
||||
it('starts empty', () => {
|
||||
const s = new PopupStore();
|
||||
expect(s.rows()).toEqual([]);
|
||||
expect(s.size).toBe(0);
|
||||
});
|
||||
|
||||
it('seeds from download.list', () => {
|
||||
const s = new PopupStore();
|
||||
s.setInitial([summary({ taskId: 'a' }), summary({ taskId: 'b', filename: 'b.zip' })]);
|
||||
expect(s.size).toBe(2);
|
||||
});
|
||||
|
||||
it('adds a row on event.task.added', () => {
|
||||
const s = new PopupStore();
|
||||
s.onAdded({ taskId: 't1', summary: summary() });
|
||||
expect(s.rows()).toHaveLength(1);
|
||||
expect(s.rows()[0]!.filename).toBe('file.zip');
|
||||
});
|
||||
|
||||
it('applies event.task.progress as a patch, not a rebuild', () => {
|
||||
const s = new PopupStore();
|
||||
s.setInitial([summary({ taskId: 't1', sizeBytes: 1000 })]);
|
||||
s.onProgress({ at: '2026-01-01T00:00:00Z', tasks: [{ taskId: 't1', downloadedBytes: 500, speedBps: 2048, etaSeconds: 4 }] });
|
||||
const row = s.rows()[0]!;
|
||||
expect(row.downloadedBytes).toBe(500);
|
||||
expect(row.speedBps).toBe(2048);
|
||||
expect(row.etaSeconds).toBe(4);
|
||||
expect(row.filename).toBe('file.zip'); // untouched by the patch
|
||||
});
|
||||
|
||||
it('ignores a progress tick for an unknown task rather than inserting a partial row', () => {
|
||||
const s = new PopupStore();
|
||||
s.onProgress({ at: '2026-01-01T00:00:00Z', tasks: [{ taskId: 'ghost', downloadedBytes: 1, speedBps: 1 }] });
|
||||
expect(s.size).toBe(0);
|
||||
});
|
||||
|
||||
it('replaces the row on event.task.state when a summary is attached', () => {
|
||||
const s = new PopupStore();
|
||||
s.setInitial([summary({ taskId: 't1', state: 'downloading' })]);
|
||||
s.onState({ taskId: 't1', state: 'paused', summary: summary({ taskId: 't1', state: 'paused' }) });
|
||||
expect(s.rows()[0]!.state).toBe('paused');
|
||||
});
|
||||
|
||||
it('updates just the state when event.task.state has no summary', () => {
|
||||
const s = new PopupStore();
|
||||
s.setInitial([summary({ taskId: 't1', state: 'downloading' })]);
|
||||
s.onState({ taskId: 't1', state: 'failed' });
|
||||
expect(s.rows()[0]!.state).toBe('failed');
|
||||
});
|
||||
|
||||
it('drops the row on event.task.removed', () => {
|
||||
const s = new PopupStore();
|
||||
s.setInitial([summary({ taskId: 't1' })]);
|
||||
s.onRemoved({ taskId: 't1', deletedFile: false });
|
||||
expect(s.size).toBe(0);
|
||||
});
|
||||
|
||||
it('sorts active tasks before paused/finished ones, then by filename', () => {
|
||||
const s = new PopupStore();
|
||||
s.setInitial([
|
||||
summary({ taskId: 'z', filename: 'z-paused.zip', state: 'paused' }),
|
||||
summary({ taskId: 'b', filename: 'b-active.zip', state: 'downloading' }),
|
||||
summary({ taskId: 'a', filename: 'a-active.zip', state: 'downloading' }),
|
||||
]);
|
||||
expect(s.rows().map((r) => r.taskId)).toEqual(['a', 'b', 'z']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('format helpers', () => {
|
||||
it('formats bytes', () => {
|
||||
expect(formatBytes(500)).toBe('500 B');
|
||||
expect(formatBytes(2048)).toBe('2.0 KB');
|
||||
expect(formatBytes(5 * 1024 * 1024)).toBe('5.0 MB');
|
||||
});
|
||||
|
||||
it('formats speed, blank when idle', () => {
|
||||
expect(formatSpeed(0)).toBe('');
|
||||
expect(formatSpeed(1024)).toBe('1.0 KB/s');
|
||||
});
|
||||
|
||||
it('formats eta, blank when unknown', () => {
|
||||
expect(formatEta(null)).toBe('');
|
||||
expect(formatEta(-1)).toBe('');
|
||||
expect(formatEta(45)).toBe('45s');
|
||||
expect(formatEta(125)).toBe('2m 5s');
|
||||
});
|
||||
|
||||
it('computes a progress percent, null when size is unknown', () => {
|
||||
expect(progressPercent({ taskId: 't', filename: 'f', state: 'downloading', downloadedBytes: 50, sizeBytes: 100, speedBps: 0, etaSeconds: null })).toBe(50);
|
||||
expect(progressPercent({ taskId: 't', filename: 'f', state: 'downloading', downloadedBytes: 50, sizeBytes: null, speedBps: 0, etaSeconds: null })).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user