From a74e7c3e07620ca14bdca640604075557bc16b64 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 26 Apr 2026 06:15:30 +0000 Subject: [PATCH] test: add tests for i18n, streaming, and cli modules Extend tests/modules.test.js with: - parseSseBuffer: partial buffer, multi-event chunk, skipped non-data lines, malformed JSON line handling - i18n: multi-variable interpolation, zh translation, missing-key fallback - cli: resolveCliPath with override path trimming and mocked fs.existsSync to verify filesystem search and bare-name fallback https://claude.ai/code/session_016QvEfqw6YZ3RjwBHrJ4w8S --- tests/modules.test.js | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/tests/modules.test.js b/tests/modules.test.js index 42a2c72..73e8439 100644 --- a/tests/modules.test.js +++ b/tests/modules.test.js @@ -456,4 +456,71 @@ const anthSse = t.parseSseBuffer( ); assert.deepStrictEqual(anthSse.deltas, ['token'], 'Anthropic SSE extracts text delta'); +// parseSseBuffer: partial line (no trailing newline → stays in rest) +const ssePartial = t.parseSseBuffer('data: {"choices":[{"delta":{"content":"ok"}}]}', openaiExtract); +assert.deepStrictEqual(ssePartial.deltas, [], 'incomplete line yields no deltas'); +assert.ok(ssePartial.rest.includes('"ok"'), 'partial line kept in rest'); + +// parseSseBuffer: multi-event in one chunk +const sseMulti = t.parseSseBuffer( + 'data: {"choices":[{"delta":{"content":"a"}}]}\ndata: {"choices":[{"delta":{"content":"b"}}]}\ndata: {"choices":[{"delta":{"content":"c"}}]}\n', + openaiExtract, +); +assert.deepStrictEqual(sseMulti.deltas, ['a', 'b', 'c'], 'three events in one chunk'); +assert.strictEqual(sseMulti.rest, '', 'nothing left in rest when chunk ends with newline'); + +// parseSseBuffer: non-data lines are skipped +const sseMixed = t.parseSseBuffer( + ': comment\nevent: ping\ndata: {"choices":[{"delta":{"content":"x"}}]}\n', + openaiExtract, +); +assert.deepStrictEqual(sseMixed.deltas, ['x'], 'comment and event lines ignored'); + +// parseSseBuffer: malformed JSON is silently skipped +const sseBad = t.parseSseBuffer('data: not_json\ndata: {"choices":[{"delta":{"content":"y"}}]}\n', openaiExtract); +assert.deepStrictEqual(sseBad.deltas, ['y'], 'bad JSON line skipped, good line extracted'); + +// i18n: additional edge cases +assert.strictEqual( + t.translate({ uiLanguage: 'en' }, 'generationDone', { count: 3, suffix: '' }), + 'Generated 3 sections', + 'variable interpolation with multiple vars', +); +assert.strictEqual( + t.translate({ uiLanguage: 'zh' }, 'appTitle'), + '对照阅读笔记', + 'zh translation for appTitle', +); +// missing key fallback chain: should return the key itself +assert.strictEqual(t.translate({ uiLanguage: 'en' }, '__no_such_key__'), '__no_such_key__', 'missing key returns key'); + +// ── cli.ts: resolveCliPath ── + +// Override path: should return the trimmed override immediately +assert.strictEqual(t.resolveCliPath('claude', ' /usr/bin/claude '), '/usr/bin/claude', 'trims override path'); +assert.strictEqual(t.resolveCliPath('claude', '/custom/path'), '/custom/path', 'returns custom path unchanged'); + +// Empty override: falls back to filesystem search; mock fs.existsSync to control behavior +const fsModule = require('fs'); +const origExistsSync = fsModule.existsSync; + +// Mock existsSync to pretend a specific path exists +const mockPath = require('path').join(require('os').homedir(), '.local', 'bin', 'claude'); +fsModule.existsSync = (p) => p === mockPath; +try { + const resolved = t.resolveCliPath('claude', ''); + assert.strictEqual(resolved, mockPath, 'resolveCliPath finds binary in mocked ~/.local/bin'); +} finally { + fsModule.existsSync = origExistsSync; +} + +// No match: falls back to bare name +fsModule.existsSync = () => false; +try { + const fallback = t.resolveCliPath('claude', ''); + assert.strictEqual(fallback, 'claude', 'resolveCliPath falls back to bare name when not found'); +} finally { + fsModule.existsSync = origExistsSync; +} + console.log('modules tests passed');