Files
Gitea/web_src/js/modules/worker.test.ts
T
silverwind 61be9fcdfa chore: update eslint and stylelint configs and re-sync modern-normalize (#38982)
- update the vendored `modern-normalize` to v3.0.1
- require descriptions for lint disables in TS and CSS, same as we
already have in Go.
- disable core rules covered by `regexp/*` and `unicorn/*`, and ones
that cannot fire
- stop applying vitest rules to the playwright files in `tests/e2e`
- enable 7 stylelint rules, mostly `no-unknown` and `no-invalid` checks
- drop 2 unnecessary vendor prefixes (safari v17+, chrome v120+)
- look up ids via `querySelector` with `CSS.escape` instead of
`getElementById`
- remove stale doc about `@ts-expect-error`, it's forbidden
- misc dev doc fixes

Every declaration that `modern-normalize` v3 removes was checked against
chromium, webkit and firefox defaults first. The `hr` color and the
`:-moz-focusring` outline are kept as documented deviations, dropping
those does change rendering.

---------

Signed-off-by: silverwind <me@silverwind.io>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
2026-08-20 09:57:56 -04:00

70 lines
2.6 KiB
TypeScript

import type {UserEventMessage, WorkerInboundMessage} from '../types.ts';
// Minimal SharedWorker/MessagePort doubles: worker.ts wires user events onto
// `sharedWorker.port`, so we capture the port to feed it messages and assert dispatch behavior.
type PortListener = (ev: {data: WorkerInboundMessage}) => void;
class MockMessagePort {
listeners: Record<string, PortListener[]> = {};
posted: unknown[] = [];
addEventListener(type: string, cb: PortListener) {
(this.listeners[type] ||= []).push(cb);
}
removeEventListener() {}
postMessage(msg: unknown) { this.posted.push(msg) }
start() {}
close() {}
// Simulate the underlying worker delivering a message to the page.
deliver(msg: UserEventMessage) {
for (const cb of this.listeners['message'] || []) cb({data: {msgType: 'user-event', msgData: msg}});
}
}
let lastWorker: MockSharedWorker;
class MockSharedWorker {
port = new MockMessagePort();
// eslint-disable-next-line unicorn/no-this-assignment -- the test needs a handle on the instance the module constructs
constructor() { lastWorker = this }
addEventListener() {}
}
// worker.ts caches module-scope state (subscribers, initialized), so re-import
// a fresh module per test after stubbing the globals it reads on init.
async function freshWorker() {
vi.resetModules();
vi.stubGlobal('WebSocket', class {});
vi.stubGlobal('SharedWorker', MockSharedWorker);
return await import('./worker.ts');
}
afterEach(() => {
vi.unstubAllGlobals();
});
// sequential: freshWorker resets the module registry and stubs globals, which is unsafe
// to interleave with the other test under the repo's `sequence.concurrent` vitest config.
// Every push follows a real DB write, so a repeated value still means the
// underlying list changed (e.g. a new comment on an already-unread issue).
test('dispatches every push, including repeated values', {concurrent: false}, async () => {
const {onUserEvent} = await freshWorker();
const received: number[] = [];
onUserEvent('notification-count', (msg) => { received.push(msg.eventData.count) });
lastWorker.port.deliver({eventType: 'notification-count', eventData: {count: 1}});
lastWorker.port.deliver({eventType: 'notification-count', eventData: {count: 1}});
expect(received).toEqual([1, 1]);
});
test('worker-connected flags the page and reaches its subscribers', {concurrent: false}, async () => {
const {onUserEvent} = await freshWorker();
let connects = 0;
onUserEvent('worker-connected', () => { connects++ });
lastWorker.port.deliver({eventType: 'worker-connected'});
expect(connects).toBe(1);
expect(document.documentElement.getAttribute('data-user-events-connected')).toBe('true');
});