mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-04-09 16:19:25 -07:00
feat(stats): add v1 immersion stats dashboard (#19)
This commit is contained in:
57
src/stats-word-helper-client.test.ts
Normal file
57
src/stats-word-helper-client.test.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { createInvokeStatsWordHelperHandler } from './stats-word-helper-client';
|
||||
|
||||
test('word helper client returns note id when helper responds before exit', async () => {
|
||||
const calls: string[] = [];
|
||||
const handler = createInvokeStatsWordHelperHandler({
|
||||
createTempDir: () => '/tmp/stats-word-helper',
|
||||
joinPath: (...parts) => parts.join('/'),
|
||||
spawnHelper: async (options) => {
|
||||
calls.push(
|
||||
`spawnHelper:${options.scriptPath}:${options.responsePath}:${options.userDataPath}:${options.word}`,
|
||||
);
|
||||
return new Promise<number>((resolve) => setTimeout(() => resolve(0), 20));
|
||||
},
|
||||
waitForResponse: async (responsePath) => {
|
||||
calls.push(`waitForResponse:${responsePath}`);
|
||||
return { ok: true, noteId: 123 };
|
||||
},
|
||||
removeDir: (targetPath) => {
|
||||
calls.push(`removeDir:${targetPath}`);
|
||||
},
|
||||
});
|
||||
|
||||
const noteId = await handler({
|
||||
helperScriptPath: '/tmp/stats-word-helper.js',
|
||||
userDataPath: '/tmp/SubMiner',
|
||||
word: '猫',
|
||||
});
|
||||
|
||||
assert.equal(noteId, 123);
|
||||
assert.deepEqual(calls, [
|
||||
'spawnHelper:/tmp/stats-word-helper.js:/tmp/stats-word-helper/response.json:/tmp/SubMiner:猫',
|
||||
'waitForResponse:/tmp/stats-word-helper/response.json',
|
||||
'removeDir:/tmp/stats-word-helper',
|
||||
]);
|
||||
});
|
||||
|
||||
test('word helper client throws helper response errors', async () => {
|
||||
const handler = createInvokeStatsWordHelperHandler({
|
||||
createTempDir: () => '/tmp/stats-word-helper',
|
||||
joinPath: (...parts) => parts.join('/'),
|
||||
spawnHelper: async () => 0,
|
||||
waitForResponse: async () => ({ ok: false, error: 'helper failed' }),
|
||||
removeDir: () => {},
|
||||
});
|
||||
|
||||
await assert.rejects(
|
||||
async () =>
|
||||
handler({
|
||||
helperScriptPath: '/tmp/stats-word-helper.js',
|
||||
userDataPath: '/tmp/SubMiner',
|
||||
word: '猫',
|
||||
}),
|
||||
/helper failed/,
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user