Files
obelisk/tests/app-indexer-worker-client.test.mjs
T
tommy0103 905c10789a refactor(app): migrate to ESM; preload emitted as CJS for sandbox (Phase 5d-3b)
App source (main/preload/worker/renderer) -> ESM; app is now type: module;
__dirname via import.meta.url; worker spawned with type module. Preload is built
as CJS (electron-vite output format) because the sandboxed renderer does not
support ESM preload; main loads ../preload/index.js. Removed dead imports
(nativeImage, readline) and the obsolete scripts/dev.js.

Tests: 4 app tests require->import; app-main-settings rewritten with node:test
mock.module + dynamic import (replacing CJS Module._load mocking); test script
adds --experimental-test-module-mocks. electron-vite build clean, 119/119, and
npm run dev verified: app launches, preload bridges IPC, data loads.
2026-07-09 11:05:00 +08:00

75 lines
1.9 KiB
JavaScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
import { createWorkerBuildIndex } from '../app/src/main/indexer-worker-client.js';
test('worker build client resolves build results from a worker thread', async () => {
const instances = [];
class MockWorker {
constructor(workerPath) {
this.workerPath = workerPath;
this.handlers = {};
this.messages = [];
this.terminated = false;
instances.push(this);
}
on(event, handler) {
this.handlers[event] = handler;
return this;
}
postMessage(message) {
this.messages.push(message);
queueMicrotask(() => {
this.handlers.message({
id: message.id,
result: { files: 1, reason: message.args.reason },
});
});
}
terminate() {
this.terminated = true;
}
}
const client = createWorkerBuildIndex({ WorkerImpl: MockWorker, workerPath: '/tmp/indexer-worker.js' });
const result = await client.buildIndex({ reason: 'watch' });
assert.equal(instances.length, 1);
assert.equal(instances[0].workerPath, '/tmp/indexer-worker.js');
assert.deepEqual(result, { files: 1, reason: 'watch' });
client.stop();
assert.equal(instances[0].terminated, true);
});
test('worker build client rejects pending builds when worker exits cleanly', async () => {
class MockWorker {
constructor() {
this.handlers = {};
}
on(event, handler) {
this.handlers[event] = handler;
return this;
}
postMessage() {
queueMicrotask(() => {
this.handlers.exit(0);
});
}
}
const client = createWorkerBuildIndex({ WorkerImpl: MockWorker, workerPath: '/tmp/indexer-worker.js' });
await assert.rejects(
client.buildIndex({ reason: 'startup' }),
/Indexer worker exited with code 0/,
);
});