Files
vdm/extension/tests/capture/offered-urls.test.ts
T
samiandClaude Sonnet 5 175185bbfc ext: capture/downloads-api.ts — downloads.onCreated safety net
Downloads that never reach the header hook (form POST results, service-worker
responses, clicks Firefox routes straight to its downloader) surface here.
If one looks like the daemon's, offer it FIRST and only cancel + erase
Firefox's copy on {action:"take"} — a failed or slow offer can never leave
the user with nothing. blob:/data: downloads are left to Firefox (the daemon
can't fetch a blob URL).

- offered-urls.ts: short-lived, bounded TTL set of URLs the header hook has
  already offered; the safety net checks it (via wasOffered) so nothing is
  double-handled. Hook gains an onOffered hook to populate it.
- background/index.ts: both paths share one offer(), getCookies, rules
  mirror, and OfferedUrls instance.

13 new tests incl. fail-open (offer rejects -> 'error', ignore ->
'offer_declined', cancel() throwing after take still returns 'taken').
101 tests green; web-ext lint clean.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012Y9RU58hD1BuwP82DySUHk
2026-09-10 15:39:40 +04:00

40 lines
1.1 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { OfferedUrls } from '../../src/background/capture/offered-urls.js';
describe('OfferedUrls', () => {
it('remembers a URL for the TTL, then forgets it', () => {
let now = 0;
const o = new OfferedUrls({ ttlMs: 1000, now: () => now });
o.mark('https://x/a.zip');
expect(o.has('https://x/a.zip')).toBe(true);
now = 1001;
expect(o.has('https://x/a.zip')).toBe(false);
expect(o.size).toBe(0);
});
it('re-mark refreshes the TTL', () => {
let now = 0;
const o = new OfferedUrls({ ttlMs: 1000, now: () => now });
o.mark('u');
now = 900;
o.mark('u');
now = 1800; // 900 since the last mark
expect(o.has('u')).toBe(true);
});
it('evicts the oldest past the size cap', () => {
const o = new OfferedUrls({ maxEntries: 2, ttlMs: 10_000, now: () => 0 });
o.mark('a');
o.mark('b');
o.mark('c');
expect(o.has('a')).toBe(false);
expect(o.has('b')).toBe(true);
expect(o.has('c')).toBe(true);
});
it('is false for a URL it has never seen', () => {
expect(new OfferedUrls().has('nope')).toBe(false);
});
});