Files
obelisk/tests/app-indexer-worker-client.test.mjs
T
tommy0103 80c125a572 build(app): migrate to electron-vite (main/preload/renderer), keep electron-builder (Phase 5d-3a)
Restructure app into src/{main,preload,renderer}; electron.vite.config.ts builds
all three (each main module its own input so CJS requires + the indexer worker
resolve; better-sqlite3 externalized; Vue plugin for renderer). main/index.js
paths updated for the out/ layout + ELECTRON_RENDERER_URL. Still JS/CJS — TS+ESM
and core consumption are the next stages. Verified: npm run dev launches clean;
electron-vite build succeeds; root suite 119/119.
2026-07-09 10:20:18 +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);
const { createWorkerBuildIndex } = require('../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/,
);
});