This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
buildGlobCacheKey,
|
||||
buildGrepCacheKey,
|
||||
buildOfficeCacheKey,
|
||||
buildReadCacheKey,
|
||||
buildWebFetchCacheKey,
|
||||
} from './cache-key.js';
|
||||
|
||||
describe('buildReadCacheKey', () => {
|
||||
it('builds a fully-specified key', () => {
|
||||
expect(buildReadCacheKey({
|
||||
workspacePath: '/ws/1',
|
||||
filePath: 'output/foo.ts',
|
||||
offset: 10,
|
||||
limit: 20,
|
||||
byteOffset: 100,
|
||||
byteLength: 200,
|
||||
})).toBe('read:v1:/ws/1:output/foo.ts:10:20:100:200');
|
||||
});
|
||||
|
||||
it('normalizes undefined range params to "all"', () => {
|
||||
expect(buildReadCacheKey({ workspacePath: '/ws', filePath: 'a.txt' }))
|
||||
.toBe('read:v1:/ws:a.txt:all:all:all:all');
|
||||
});
|
||||
|
||||
it('keeps zero ranges distinct from undefined', () => {
|
||||
const withZero = buildReadCacheKey({ workspacePath: '/ws', filePath: 'a.txt', offset: 0, limit: 0 });
|
||||
const withAll = buildReadCacheKey({ workspacePath: '/ws', filePath: 'a.txt' });
|
||||
expect(withZero).toBe('read:v1:/ws:a.txt:0:0:all:all');
|
||||
expect(withZero).not.toBe(withAll);
|
||||
});
|
||||
|
||||
it('separates identical file paths in different workspaces', () => {
|
||||
const a = buildReadCacheKey({ workspacePath: '/ws/a', filePath: 'same.txt' });
|
||||
const b = buildReadCacheKey({ workspacePath: '/ws/b', filePath: 'same.txt' });
|
||||
expect(a).not.toBe(b);
|
||||
});
|
||||
|
||||
it('separates different range slices of the same file', () => {
|
||||
const a = buildReadCacheKey({ workspacePath: '/ws', filePath: 'f', offset: 1, limit: 10 });
|
||||
const b = buildReadCacheKey({ workspacePath: '/ws', filePath: 'f', offset: 2, limit: 10 });
|
||||
expect(a).not.toBe(b);
|
||||
});
|
||||
|
||||
it('handles unicode file paths', () => {
|
||||
expect(buildReadCacheKey({ workspacePath: '/ws', filePath: 'output/日本語ファイル.md' }))
|
||||
.toBe('read:v1:/ws:output/日本語ファイル.md:all:all:all:all');
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildGrepCacheKey', () => {
|
||||
it('builds a fully-specified key with pattern last', () => {
|
||||
expect(buildGrepCacheKey({
|
||||
workspacePath: '/ws',
|
||||
pattern: 'foo.*bar',
|
||||
path: 'src',
|
||||
glob: '*.ts',
|
||||
})).toBe('grep:v1:/ws:src:*.ts:foo.*bar');
|
||||
});
|
||||
|
||||
it('defaults path to "." and glob to "*"', () => {
|
||||
expect(buildGrepCacheKey({ workspacePath: '/ws', pattern: 'x' }))
|
||||
.toBe('grep:v1:/ws:.:*:x');
|
||||
});
|
||||
|
||||
it('keeps patterns containing colons stable (pattern is the final segment)', () => {
|
||||
expect(buildGrepCacheKey({ workspacePath: '/ws', pattern: 'a:b:c' }))
|
||||
.toBe('grep:v1:/ws:.:*:a:b:c');
|
||||
});
|
||||
|
||||
it('distinguishes same pattern under different globs', () => {
|
||||
const a = buildGrepCacheKey({ workspacePath: '/ws', pattern: 'p', glob: '*.ts' });
|
||||
const b = buildGrepCacheKey({ workspacePath: '/ws', pattern: 'p', glob: '*.js' });
|
||||
expect(a).not.toBe(b);
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildGlobCacheKey', () => {
|
||||
it('builds key with explicit path', () => {
|
||||
expect(buildGlobCacheKey({ workspacePath: '/ws', pattern: '**/*.ts', path: 'src' }))
|
||||
.toBe('glob:v1:/ws:src:**/*.ts');
|
||||
});
|
||||
|
||||
it('defaults path to "."', () => {
|
||||
expect(buildGlobCacheKey({ workspacePath: '/ws', pattern: '*.md' }))
|
||||
.toBe('glob:v1:/ws:.:*.md');
|
||||
});
|
||||
|
||||
it('distinguishes glob from grep keys for similar args', () => {
|
||||
const glob = buildGlobCacheKey({ workspacePath: '/ws', pattern: 'p' });
|
||||
const grep = buildGrepCacheKey({ workspacePath: '/ws', pattern: 'p' });
|
||||
expect(glob).not.toBe(grep);
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildWebFetchCacheKey', () => {
|
||||
it('lower-cases scheme and host', () => {
|
||||
expect(buildWebFetchCacheKey({ url: 'HTTPS://Example.COM/Path' }))
|
||||
.toBe('webfetch:v1:https://example.com/Path');
|
||||
});
|
||||
|
||||
it('drops URL fragments', () => {
|
||||
expect(buildWebFetchCacheKey({ url: 'https://example.com/page#section-2' }))
|
||||
.toBe('webfetch:v1:https://example.com/page');
|
||||
});
|
||||
|
||||
it('preserves the query string', () => {
|
||||
expect(buildWebFetchCacheKey({ url: 'https://example.com/search?q=a&b=2' }))
|
||||
.toBe('webfetch:v1:https://example.com/search?q=a&b=2');
|
||||
});
|
||||
|
||||
it('normalizes bare host and trailing-slash host to the same key', () => {
|
||||
const bare = buildWebFetchCacheKey({ url: 'https://example.com' });
|
||||
const slash = buildWebFetchCacheKey({ url: 'https://example.com/' });
|
||||
expect(bare).toBe(slash);
|
||||
});
|
||||
|
||||
it('preserves path case (only scheme/host are case-insensitive)', () => {
|
||||
const a = buildWebFetchCacheKey({ url: 'https://example.com/A' });
|
||||
const b = buildWebFetchCacheKey({ url: 'https://example.com/a' });
|
||||
expect(a).not.toBe(b);
|
||||
});
|
||||
|
||||
it('keys invalid URLs on themselves', () => {
|
||||
expect(buildWebFetchCacheKey({ url: 'not a url' }))
|
||||
.toBe('webfetch:v1:not a url');
|
||||
expect(buildWebFetchCacheKey({ url: '' }))
|
||||
.toBe('webfetch:v1:');
|
||||
});
|
||||
|
||||
it('treats same URL with different fragments as the same entry', () => {
|
||||
const a = buildWebFetchCacheKey({ url: 'https://x.test/p#one' });
|
||||
const b = buildWebFetchCacheKey({ url: 'https://x.test/p#two' });
|
||||
expect(a).toBe(b);
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildOfficeCacheKey', () => {
|
||||
it('builds key with explicit range', () => {
|
||||
expect(buildOfficeCacheKey({
|
||||
workspacePath: '/ws',
|
||||
toolName: 'ReadExcel',
|
||||
filePath: 'input/book.xlsx',
|
||||
range: 'sheet=Sheet1;rows=1-100',
|
||||
})).toBe('office:v1:ReadExcel:/ws:input/book.xlsx:sheet=Sheet1;rows=1-100');
|
||||
});
|
||||
|
||||
it('defaults range to "all"', () => {
|
||||
expect(buildOfficeCacheKey({ workspacePath: '/ws', toolName: 'ReadPdf', filePath: 'a.pdf' }))
|
||||
.toBe('office:v1:ReadPdf:/ws:a.pdf:all');
|
||||
});
|
||||
|
||||
it('separates same file across different office tools', () => {
|
||||
const pdf = buildOfficeCacheKey({ workspacePath: '/ws', toolName: 'ReadPdf', filePath: 'f' });
|
||||
const docx = buildOfficeCacheKey({ workspacePath: '/ws', toolName: 'ReadDocx', filePath: 'f' });
|
||||
expect(pdf).not.toBe(docx);
|
||||
});
|
||||
|
||||
it('separates same tool+file across workspaces', () => {
|
||||
const a = buildOfficeCacheKey({ workspacePath: '/ws/a', toolName: 'ReadPdf', filePath: 'f' });
|
||||
const b = buildOfficeCacheKey({ workspacePath: '/ws/b', toolName: 'ReadPdf', filePath: 'f' });
|
||||
expect(a).not.toBe(b);
|
||||
});
|
||||
});
|
||||
|
||||
describe('cache key version prefix', () => {
|
||||
it('every formula embeds the v1 version tag right after the tool tag', () => {
|
||||
expect(buildReadCacheKey({ workspacePath: 'w', filePath: 'f' }).startsWith('read:v1:')).toBe(true);
|
||||
expect(buildGrepCacheKey({ workspacePath: 'w', pattern: 'p' }).startsWith('grep:v1:')).toBe(true);
|
||||
expect(buildGlobCacheKey({ workspacePath: 'w', pattern: 'p' }).startsWith('glob:v1:')).toBe(true);
|
||||
expect(buildWebFetchCacheKey({ url: 'https://a.test/' }).startsWith('webfetch:v1:')).toBe(true);
|
||||
expect(buildOfficeCacheKey({ workspacePath: 'w', toolName: 't', filePath: 'f' }).startsWith('office:v1:')).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import type { ToolCall } from '../../llm/openai-compat.js';
|
||||
import { extractInvalidationTrigger } from './invalidation.js';
|
||||
|
||||
function call(name: string, args: unknown): ToolCall {
|
||||
return {
|
||||
id: 'tc-1',
|
||||
type: 'function',
|
||||
function: {
|
||||
name,
|
||||
arguments: typeof args === 'string' ? args : JSON.stringify(args),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe('extractInvalidationTrigger', () => {
|
||||
it('invalidates the edited path for Edit and Write', () => {
|
||||
expect(extractInvalidationTrigger(call('Edit', { file_path: '/ws/a.ts' })))
|
||||
.toEqual({ kind: 'path', path: '/ws/a.ts' });
|
||||
expect(extractInvalidationTrigger(call('Write', { file_path: 'out/b.md' })))
|
||||
.toEqual({ kind: 'path', path: 'out/b.md' });
|
||||
});
|
||||
|
||||
it('falls back to all_files when Edit args have no usable file_path', () => {
|
||||
expect(extractInvalidationTrigger(call('Edit', {}))).toEqual({ kind: 'all_files' });
|
||||
expect(extractInvalidationTrigger(call('Edit', { file_path: '' }))).toEqual({ kind: 'all_files' });
|
||||
expect(extractInvalidationTrigger(call('Edit', { file_path: 42 }))).toEqual({ kind: 'all_files' });
|
||||
});
|
||||
|
||||
it('falls back to all_files when Edit args are unparseable JSON', () => {
|
||||
expect(extractInvalidationTrigger(call('Write', '{not json'))).toEqual({ kind: 'all_files' });
|
||||
});
|
||||
|
||||
it('invalidates everything for Bash regardless of args', () => {
|
||||
expect(extractInvalidationTrigger(call('Bash', { command: 'ls' }))).toEqual({ kind: 'all_files' });
|
||||
expect(extractInvalidationTrigger(call('Bash', '{broken'))).toEqual({ kind: 'all_files' });
|
||||
});
|
||||
|
||||
it('produces no trigger for read-only and unknown tools', () => {
|
||||
for (const name of ['Read', 'Grep', 'Glob', 'WebFetch', 'NoSuchTool']) {
|
||||
expect(extractInvalidationTrigger(call(name, { file_path: '/ws/a.ts' }))).toBeNull();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,185 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { Message, ToolDef } from '../../llm/openai-compat.js';
|
||||
import {
|
||||
estimateMessagesTokens,
|
||||
estimateMessageTokens,
|
||||
estimatePromptTokens,
|
||||
estimateTokensFromChars,
|
||||
estimateTokensFromText,
|
||||
estimateToolsTokens,
|
||||
IMAGE_CONTENT_TOKENS,
|
||||
UNKNOWN_CHARS_TO_TOKENS_FACTOR,
|
||||
} from './token-estimate.js';
|
||||
|
||||
describe('estimateTokensFromChars', () => {
|
||||
it('returns 0 for 0 chars', () => {
|
||||
expect(estimateTokensFromChars(0)).toBe(0);
|
||||
});
|
||||
|
||||
it('applies the unknown-content factor with ceiling', () => {
|
||||
expect(estimateTokensFromChars(1)).toBe(Math.ceil(1 * UNKNOWN_CHARS_TO_TOKENS_FACTOR)); // 2
|
||||
expect(estimateTokensFromChars(5)).toBe(6); // 5 * 1.2 = 6 exactly
|
||||
expect(estimateTokensFromChars(10)).toBe(12); // 10 * 1.2 = 12 exactly
|
||||
expect(estimateTokensFromChars(11)).toBe(14); // 13.2 -> ceil 14
|
||||
});
|
||||
});
|
||||
|
||||
describe('estimateTokensFromText', () => {
|
||||
it('returns 0 for empty string', () => {
|
||||
expect(estimateTokensFromText('')).toBe(0);
|
||||
});
|
||||
|
||||
it('counts ASCII at ~3.5 chars per token', () => {
|
||||
expect(estimateTokensFromText('a'.repeat(7))).toBe(2); // 7 / 3.5 = 2 exactly
|
||||
expect(estimateTokensFromText('a'.repeat(35))).toBe(10); // 35 / 3.5 = 10 exactly
|
||||
expect(estimateTokensFromText('a')).toBe(1); // 0.286 -> ceil 1
|
||||
});
|
||||
|
||||
it('counts CJK at 1.2 tokens per char', () => {
|
||||
expect(estimateTokensFromText('あ')).toBe(2); // 1.2 -> ceil 2
|
||||
expect(estimateTokensFromText('日本語')).toBe(4); // 3.6 -> ceil 4
|
||||
expect(estimateTokensFromText('カタカナ')).toBe(5); // 4.8 -> ceil 5
|
||||
});
|
||||
|
||||
it('classifies full-width forms as CJK', () => {
|
||||
// 'A' (U+FF21) and '!' (U+FF01) are in the full-width block.
|
||||
expect(estimateTokensFromText('AB')).toBe(3); // 2.4 -> ceil 3
|
||||
});
|
||||
|
||||
it('counts other non-ASCII (accented latin, emoji) at 1 token per char', () => {
|
||||
expect(estimateTokensFromText('é')).toBe(1);
|
||||
expect(estimateTokensFromText('éà')).toBe(2);
|
||||
});
|
||||
|
||||
it('iterates astral code points once (emoji is one char, not two surrogates)', () => {
|
||||
expect(estimateTokensFromText('😀')).toBe(1);
|
||||
expect(estimateTokensFromText('😀😀😀')).toBe(3);
|
||||
});
|
||||
|
||||
it('mixes script classes additively before a single ceil', () => {
|
||||
// 'abc' (3 ascii -> 0.857) + 'あ' (1.2) = 2.057 -> ceil 3
|
||||
expect(estimateTokensFromText('abcあ')).toBe(3);
|
||||
});
|
||||
|
||||
it('estimates Japanese text far higher per char than ASCII text', () => {
|
||||
const ascii = estimateTokensFromText('a'.repeat(100));
|
||||
const cjk = estimateTokensFromText('あ'.repeat(100));
|
||||
expect(ascii).toBe(29); // 100 / 3.5 -> ceil
|
||||
expect(cjk).toBe(120); // 100 * 1.2
|
||||
});
|
||||
});
|
||||
|
||||
describe('estimateMessageTokens', () => {
|
||||
it('charges role text plus a fixed 8-token overhead for an empty message', () => {
|
||||
// 'user' = 4 ascii chars -> ceil(4/3.5) = 2, plus 8 overhead.
|
||||
expect(estimateMessageTokens({ role: 'user' })).toBe(10);
|
||||
});
|
||||
|
||||
it('adds string content tokens', () => {
|
||||
const base = estimateMessageTokens({ role: 'user' });
|
||||
const withContent = estimateMessageTokens({ role: 'user', content: 'a'.repeat(35) });
|
||||
expect(withContent).toBe(base + 10);
|
||||
});
|
||||
|
||||
it('sums text parts in array content', () => {
|
||||
const msg: Message = {
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'text', text: 'a'.repeat(7) },
|
||||
{ type: 'text', text: 'b'.repeat(7) },
|
||||
],
|
||||
};
|
||||
expect(estimateMessageTokens(msg)).toBe(estimateMessageTokens({ role: 'user' }) + 4);
|
||||
});
|
||||
|
||||
it('charges a flat image budget per image part', () => {
|
||||
const msg: Message = {
|
||||
role: 'user',
|
||||
content: [{ type: 'image_url', image_url: { url: 'data:image/png;base64,xxxx' } }],
|
||||
};
|
||||
expect(estimateMessageTokens(msg)).toBe(estimateMessageTokens({ role: 'user' }) + IMAGE_CONTENT_TOKENS);
|
||||
});
|
||||
|
||||
it('counts tool_call_id and name on tool messages', () => {
|
||||
const base = estimateMessageTokens({ role: 'tool' });
|
||||
const msg: Message = {
|
||||
role: 'tool',
|
||||
tool_call_id: 'c'.repeat(7), // 2 tokens
|
||||
name: 'n'.repeat(7), // 2 tokens
|
||||
};
|
||||
expect(estimateMessageTokens(msg)).toBe(base + 4);
|
||||
});
|
||||
|
||||
it('counts tool_calls id, function name, and arguments', () => {
|
||||
const base = estimateMessageTokens({ role: 'assistant' });
|
||||
const msg: Message = {
|
||||
role: 'assistant',
|
||||
tool_calls: [{
|
||||
id: 'i'.repeat(7), // 2 tokens
|
||||
type: 'function',
|
||||
function: { name: 'f'.repeat(7), arguments: 'a'.repeat(35) }, // 2 + 10 tokens
|
||||
}],
|
||||
};
|
||||
expect(estimateMessageTokens(msg)).toBe(base + 14);
|
||||
});
|
||||
|
||||
it('handles a message combining content and multiple tool calls', () => {
|
||||
const base = estimateMessageTokens({ role: 'assistant' });
|
||||
const call = {
|
||||
id: 'i'.repeat(7),
|
||||
type: 'function' as const,
|
||||
function: { name: 'f'.repeat(7), arguments: 'a'.repeat(7) },
|
||||
};
|
||||
const msg: Message = {
|
||||
role: 'assistant',
|
||||
content: 'a'.repeat(7),
|
||||
tool_calls: [call, call],
|
||||
};
|
||||
// content 2 + 2 calls * (2 + 2 + 2)
|
||||
expect(estimateMessageTokens(msg)).toBe(base + 2 + 12);
|
||||
});
|
||||
});
|
||||
|
||||
describe('estimateMessagesTokens', () => {
|
||||
it('returns 0 for an empty list', () => {
|
||||
expect(estimateMessagesTokens([])).toBe(0);
|
||||
});
|
||||
|
||||
it('sums per-message estimates', () => {
|
||||
const m1: Message = { role: 'user', content: 'hello' };
|
||||
const m2: Message = { role: 'assistant', content: 'world' };
|
||||
expect(estimateMessagesTokens([m1, m2]))
|
||||
.toBe(estimateMessageTokens(m1) + estimateMessageTokens(m2));
|
||||
});
|
||||
});
|
||||
|
||||
describe('estimateToolsTokens', () => {
|
||||
it('estimates from the JSON serialization of the tool list', () => {
|
||||
const tools: ToolDef[] = [{
|
||||
type: 'function',
|
||||
function: { name: 'Read', description: 'Read a file.', parameters: { type: 'object' } },
|
||||
}];
|
||||
expect(estimateToolsTokens(tools)).toBe(estimateTokensFromText(JSON.stringify(tools)));
|
||||
expect(estimateToolsTokens(tools)).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('costs a small but non-zero amount for an empty tool list ("[]" is 2 chars)', () => {
|
||||
expect(estimateToolsTokens([])).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('estimatePromptTokens', () => {
|
||||
it('is the sum of message and tool estimates', () => {
|
||||
const messages: Message[] = [{ role: 'system', content: 'You are helpful.' }];
|
||||
const tools: ToolDef[] = [{
|
||||
type: 'function',
|
||||
function: { name: 'X', description: 'd', parameters: {} },
|
||||
}];
|
||||
expect(estimatePromptTokens(messages, tools))
|
||||
.toBe(estimateMessagesTokens(messages) + estimateToolsTokens(tools));
|
||||
});
|
||||
|
||||
it('handles empty messages and tools', () => {
|
||||
expect(estimatePromptTokens([], [])).toBe(1); // only "[]" tools serialization
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,160 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { buildSystemPrompt, buildUserPrompt } from './reflection-prompt.js';
|
||||
import type { ReflectionInput } from './types.js';
|
||||
|
||||
function makeInput(overrides: Partial<ReflectionInput> = {}): ReflectionInput {
|
||||
return {
|
||||
originalJobId: 'job-1',
|
||||
userId: 'u-1',
|
||||
pieceName: 'chat',
|
||||
pieceSource: 'builtin',
|
||||
outcome: 'succeeded',
|
||||
taskTitle: 'Summarize the report',
|
||||
taskBody: 'Please summarize quarterly-report.pdf',
|
||||
activityLogSummary: 'ReadPdf -> Write summary.md (2 iterations)',
|
||||
postCompletionComments: [],
|
||||
feedback: { rating: null, comment: null, tags: [] },
|
||||
resultText: 'Wrote output/summary.md',
|
||||
observedRevisions: {},
|
||||
memoryIndex: '- [fact one](fact_one.md)',
|
||||
memoryEntries: [],
|
||||
pieceYaml: 'movements:\n - name: execute\n',
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe('buildSystemPrompt', () => {
|
||||
it('forces submit_reflection tool-call-only output', () => {
|
||||
const sys = buildSystemPrompt();
|
||||
expect(sys).toContain('submit_reflection');
|
||||
});
|
||||
|
||||
it('states the memory rules: non-trivial lessons, type tagging, 3-change cap, abstain path', () => {
|
||||
const sys = buildSystemPrompt();
|
||||
expect(sys).toContain('非自明');
|
||||
expect(sys).toContain('user | feedback | project | reference');
|
||||
expect(sys).toContain('最大 3 件');
|
||||
expect(sys).toContain('abstain_reason');
|
||||
expect(sys).toContain('should_edit=false');
|
||||
});
|
||||
|
||||
it('requires Why/How-to-apply lines for feedback/project entries', () => {
|
||||
const sys = buildSystemPrompt();
|
||||
expect(sys).toContain('**Why:**');
|
||||
expect(sys).toContain('**How to apply:**');
|
||||
});
|
||||
|
||||
it('hardens piece edits: full-YAML replacement and no engine sentinels in rules[].next', () => {
|
||||
const sys = buildSystemPrompt();
|
||||
expect(sys).toContain('完全置換');
|
||||
expect(sys).toContain('COMPLETE / ABORT / ASK');
|
||||
expect(sys).toContain('rules[].next');
|
||||
});
|
||||
|
||||
it('is deterministic (no timestamps or randomness)', () => {
|
||||
expect(buildSystemPrompt()).toBe(buildSystemPrompt());
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildUserPrompt', () => {
|
||||
it('includes task title, body, activity log summary, result and outcome', () => {
|
||||
const prompt = buildUserPrompt(makeInput());
|
||||
expect(prompt).toContain('title: Summarize the report');
|
||||
expect(prompt).toContain('body: Please summarize quarterly-report.pdf');
|
||||
expect(prompt).toContain('ReadPdf -> Write summary.md (2 iterations)');
|
||||
expect(prompt).toContain('status: succeeded');
|
||||
expect(prompt).toContain('result: Wrote output/summary.md');
|
||||
});
|
||||
|
||||
it('contains all section headers', () => {
|
||||
const prompt = buildUserPrompt(makeInput());
|
||||
for (const header of [
|
||||
'## 元タスク',
|
||||
'## 活動ログ (圧縮済み)',
|
||||
'## ジョブ後のユーザーコメント',
|
||||
'## 明示フィードバック',
|
||||
'## 結果',
|
||||
'## 現在の memory スナップショット',
|
||||
'## 現在の piece YAML',
|
||||
]) {
|
||||
expect(prompt).toContain(header);
|
||||
}
|
||||
});
|
||||
|
||||
it('renders post-completion comments with timestamp and author', () => {
|
||||
const prompt = buildUserPrompt(makeInput({
|
||||
postCompletionComments: [
|
||||
{ author: 'alice', body: 'wrong file was summarized', createdAt: '2026-06-10T00:00:00Z' },
|
||||
{ author: 'bob', body: 'please retry', createdAt: '2026-06-10T01:00:00Z' },
|
||||
],
|
||||
}));
|
||||
expect(prompt).toContain('- [2026-06-10T00:00:00Z] alice: wrong file was summarized');
|
||||
expect(prompt).toContain('- [2026-06-10T01:00:00Z] bob: please retry');
|
||||
expect(prompt).not.toContain('(なし)');
|
||||
});
|
||||
|
||||
it('shows (なし) when there are no post-completion comments', () => {
|
||||
const prompt = buildUserPrompt(makeInput({ postCompletionComments: [] }));
|
||||
expect(prompt).toContain('(なし)');
|
||||
});
|
||||
|
||||
it('shows "rating: none" when no explicit feedback exists', () => {
|
||||
const prompt = buildUserPrompt(makeInput());
|
||||
expect(prompt).toContain('rating: none');
|
||||
expect(prompt).not.toContain('rating: good');
|
||||
expect(prompt).not.toContain('rating: bad');
|
||||
});
|
||||
|
||||
it('renders rating, comment and tags when feedback exists', () => {
|
||||
const prompt = buildUserPrompt(makeInput({
|
||||
feedback: { rating: 'good', comment: 'nice work', tags: ['speed', 'quality'] },
|
||||
}));
|
||||
expect(prompt).toContain('rating: good');
|
||||
expect(prompt).toContain('comment: nice work');
|
||||
expect(prompt).toContain('tags: speed, quality');
|
||||
});
|
||||
|
||||
it('omits comment/tags lines when feedback has neither', () => {
|
||||
const prompt = buildUserPrompt(makeInput({
|
||||
feedback: { rating: 'good', comment: null, tags: [] },
|
||||
}));
|
||||
expect(prompt).toContain('rating: good');
|
||||
expect(prompt).not.toContain('comment:');
|
||||
expect(prompt).not.toContain('tags:');
|
||||
});
|
||||
|
||||
it('appends the bad-rating investigation directive only for rating=bad', () => {
|
||||
const bad = buildUserPrompt(makeInput({
|
||||
feedback: { rating: 'bad', comment: null, tags: [] },
|
||||
}));
|
||||
expect(bad).toContain('**低評価**');
|
||||
|
||||
const good = buildUserPrompt(makeInput({
|
||||
feedback: { rating: 'good', comment: null, tags: [] },
|
||||
}));
|
||||
expect(good).not.toContain('**低評価**');
|
||||
|
||||
const none = buildUserPrompt(makeInput());
|
||||
expect(none).not.toContain('**低評価**');
|
||||
});
|
||||
|
||||
it('embeds the memory index, or (空) when the user has no memory', () => {
|
||||
const withMemory = buildUserPrompt(makeInput());
|
||||
expect(withMemory).toContain('- [fact one](fact_one.md)');
|
||||
expect(withMemory).not.toContain('(空)');
|
||||
|
||||
const empty = buildUserPrompt(makeInput({ memoryIndex: '' }));
|
||||
expect(empty).toContain('(空)');
|
||||
});
|
||||
|
||||
it('wraps the current piece YAML in a yaml code fence', () => {
|
||||
const prompt = buildUserPrompt(makeInput());
|
||||
expect(prompt).toContain('```yaml\nmovements:\n - name: execute\n\n```');
|
||||
});
|
||||
|
||||
it('does not truncate the activity log summary (truncation happens upstream in load-inputs)', () => {
|
||||
const long = 'x'.repeat(20000);
|
||||
const prompt = buildUserPrompt(makeInput({ activityLogSummary: long }));
|
||||
expect(prompt).toContain(long);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,172 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import type { Job, Repository } from '../../db/repository.js';
|
||||
import type { AppConfig } from '../../config.js';
|
||||
|
||||
vi.mock('./load-inputs.js', () => ({ loadReflectionInputs: vi.fn() }));
|
||||
vi.mock('./reflection-prompt.js', () => ({
|
||||
buildSystemPrompt: vi.fn().mockReturnValue('SYSTEM'),
|
||||
buildUserPrompt: vi.fn().mockReturnValue('USER'),
|
||||
}));
|
||||
vi.mock('./llm-client.js', () => ({ callReflectionLlm: vi.fn() }));
|
||||
vi.mock('./applier.js', () => ({ applyReflectionUnlocked: vi.fn() }));
|
||||
vi.mock('./snapshot.js', () => ({ writeSnapshot: vi.fn() }));
|
||||
vi.mock('./user-lock.js', () => ({
|
||||
withUserLock: vi.fn(async (_dir: string, _user: string, fn: () => Promise<void>) => fn()),
|
||||
}));
|
||||
vi.mock('../piece-catalog.js', () => ({ PieceCatalog: vi.fn() }));
|
||||
|
||||
import { loadReflectionInputs } from './load-inputs.js';
|
||||
import { callReflectionLlm } from './llm-client.js';
|
||||
import { applyReflectionUnlocked } from './applier.js';
|
||||
import { writeSnapshot } from './snapshot.js';
|
||||
import { withUserLock } from './user-lock.js';
|
||||
import { runReflectionJob } from './reflection-runner.js';
|
||||
|
||||
const PAYLOAD = {
|
||||
originalJobId: 'orig-1',
|
||||
userId: 'user-1',
|
||||
pieceName: 'chat',
|
||||
outcome: 'succeeded' as const,
|
||||
};
|
||||
|
||||
function makeJob(payload: unknown = PAYLOAD): Job {
|
||||
return {
|
||||
id: 'refl-1',
|
||||
payload: payload === null ? null : JSON.stringify(payload),
|
||||
} as unknown as Job;
|
||||
}
|
||||
|
||||
function makeDeps() {
|
||||
const repo = {
|
||||
recordReflectionMetric: vi.fn(),
|
||||
} as unknown as Repository;
|
||||
return {
|
||||
repo,
|
||||
config: { reflection: {}, userFolderRoot: 'data/users' } as unknown as AppConfig,
|
||||
llmEndpoint: 'http://localhost:1',
|
||||
llmModel: 'test-model',
|
||||
};
|
||||
}
|
||||
|
||||
const INPUT = { memoryEntries: [], pieceYaml: 'yaml' };
|
||||
const LLM_RESULT = {
|
||||
parsed: { reasoning: 'because' },
|
||||
tokensIn: 100,
|
||||
tokensOut: 50,
|
||||
durationMs: 5,
|
||||
};
|
||||
|
||||
function applierResult(overrides: Record<string, unknown> = {}) {
|
||||
return {
|
||||
outcome: 'applied',
|
||||
memoryDecisions: [
|
||||
{ accepted: true, change: { op: 'add', name: 'fact-1', description: 'd', type: 'project', body: 'b' } },
|
||||
{ accepted: false, code: 'too_vague', change: { op: 'add', name: 'fact-2', description: 'd', type: 'project', body: 'b' } },
|
||||
],
|
||||
pieceApplied: false,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(loadReflectionInputs).mockResolvedValue(INPUT as never);
|
||||
vi.mocked(callReflectionLlm).mockResolvedValue(LLM_RESULT as never);
|
||||
vi.mocked(applyReflectionUnlocked).mockResolvedValue(applierResult() as never);
|
||||
vi.mocked(writeSnapshot).mockResolvedValue({ dir: '/snap/dir' } as never);
|
||||
});
|
||||
|
||||
describe('runReflectionJob', () => {
|
||||
it('returns failed for a job without payload and records no metric', async () => {
|
||||
const deps = makeDeps();
|
||||
const outcome = await runReflectionJob(deps, makeJob(null));
|
||||
expect(outcome).toBe('failed');
|
||||
expect(deps.repo.recordReflectionMetric).not.toHaveBeenCalled();
|
||||
expect(loadReflectionInputs).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('records a failed metric when input loading throws', async () => {
|
||||
vi.mocked(loadReflectionInputs).mockRejectedValue(new Error('db gone'));
|
||||
const deps = makeDeps();
|
||||
const outcome = await runReflectionJob(deps, makeJob());
|
||||
expect(outcome).toBe('failed');
|
||||
expect(deps.repo.recordReflectionMetric).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ outcome: 'failed', tokens_in: 0, tokens_out: 0 }),
|
||||
);
|
||||
expect(callReflectionLlm).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('records a failed metric when the LLM call throws', async () => {
|
||||
vi.mocked(callReflectionLlm).mockRejectedValue(new Error('timeout'));
|
||||
const deps = makeDeps();
|
||||
const outcome = await runReflectionJob(deps, makeJob());
|
||||
expect(outcome).toBe('failed');
|
||||
expect(deps.repo.recordReflectionMetric).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ outcome: 'failed', tokens_in: 0 }),
|
||||
);
|
||||
expect(applyReflectionUnlocked).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('applies, snapshots inside the user lock and records an applied metric', async () => {
|
||||
const deps = makeDeps();
|
||||
const outcome = await runReflectionJob(deps, makeJob());
|
||||
|
||||
expect(outcome).toBe('applied');
|
||||
expect(withUserLock).toHaveBeenCalledWith('data/users', 'user-1', expect.any(Function));
|
||||
expect(writeSnapshot).toHaveBeenCalledTimes(1);
|
||||
const snapMeta = vi.mocked(writeSnapshot).mock.calls[0]?.[3] as Record<string, unknown>;
|
||||
expect(snapMeta).toMatchObject({
|
||||
originalJobId: 'orig-1',
|
||||
userId: 'user-1',
|
||||
pieceName: 'chat',
|
||||
outcome: 'applied',
|
||||
memoryChanges: 1,
|
||||
rejections: [{ code: 'too_vague', name: 'fact-2' }],
|
||||
});
|
||||
expect(deps.repo.recordReflectionMetric).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
reflection_job_id: 'refl-1',
|
||||
original_job_id: 'orig-1',
|
||||
user_id: 'user-1',
|
||||
piece_name: 'chat',
|
||||
outcome: 'applied',
|
||||
memory_changes: 1,
|
||||
piece_edited: 0,
|
||||
tokens_in: 100,
|
||||
tokens_out: 50,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('propagates an abstained outcome from the applier', async () => {
|
||||
vi.mocked(applyReflectionUnlocked).mockResolvedValue(
|
||||
applierResult({ outcome: 'abstained', memoryDecisions: [] }) as never,
|
||||
);
|
||||
const deps = makeDeps();
|
||||
const outcome = await runReflectionJob(deps, makeJob());
|
||||
expect(outcome).toBe('abstained');
|
||||
expect(deps.repo.recordReflectionMetric).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ outcome: 'abstained', memory_changes: 0 }),
|
||||
);
|
||||
});
|
||||
|
||||
it('records a failed metric with LLM tokens when apply throws inside the lock', async () => {
|
||||
vi.mocked(applyReflectionUnlocked).mockRejectedValue(new Error('lock contention'));
|
||||
const deps = makeDeps();
|
||||
const outcome = await runReflectionJob(deps, makeJob());
|
||||
expect(outcome).toBe('failed');
|
||||
expect(deps.repo.recordReflectionMetric).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ outcome: 'failed', tokens_in: 100, tokens_out: 50 }),
|
||||
);
|
||||
});
|
||||
|
||||
it('treats a snapshot failure as non-fatal', async () => {
|
||||
vi.mocked(writeSnapshot).mockRejectedValue(new Error('disk full'));
|
||||
const deps = makeDeps();
|
||||
const outcome = await runReflectionJob(deps, makeJob());
|
||||
expect(outcome).toBe('applied');
|
||||
expect(deps.repo.recordReflectionMetric).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ outcome: 'applied' }),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,71 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { REFLECTION_TOOL_SCHEMA } from './reflection-schema.js';
|
||||
|
||||
// The schema is shipped verbatim to OpenAI-compatible endpoints as the tools[]
|
||||
// entry, so its exact shape is load-bearing: these tests pin the contract.
|
||||
describe('REFLECTION_TOOL_SCHEMA', () => {
|
||||
const fn = REFLECTION_TOOL_SCHEMA.function;
|
||||
const params = fn.parameters;
|
||||
|
||||
it('is an OpenAI tools-format function definition named submit_reflection', () => {
|
||||
expect(REFLECTION_TOOL_SCHEMA.type).toBe('function');
|
||||
expect(fn.name).toBe('submit_reflection');
|
||||
expect(typeof fn.description).toBe('string');
|
||||
expect(fn.description.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('is JSON-serializable (no functions / cycles)', () => {
|
||||
const roundTripped = JSON.parse(JSON.stringify(REFLECTION_TOOL_SCHEMA));
|
||||
expect(roundTripped).toEqual(REFLECTION_TOOL_SCHEMA);
|
||||
});
|
||||
|
||||
it('requires memory_changes, piece_changes and reasoning at the top level', () => {
|
||||
expect(params.type).toBe('object');
|
||||
expect(params.required).toEqual(['memory_changes', 'piece_changes', 'reasoning']);
|
||||
expect(params.additionalProperties).toBe(false);
|
||||
});
|
||||
|
||||
it('caps memory_changes at 3 items (matches the applier hard cap)', () => {
|
||||
const mc = params.properties.memory_changes;
|
||||
expect(mc.type).toBe('array');
|
||||
expect(mc.maxItems).toBe(3);
|
||||
});
|
||||
|
||||
it('memory change items require op/name/type/description/body and forbid extras', () => {
|
||||
const item = params.properties.memory_changes.items;
|
||||
expect(item.required).toEqual(['op', 'name', 'type', 'description', 'body']);
|
||||
expect(item.additionalProperties).toBe(false);
|
||||
// merge_target is optional but declared
|
||||
expect(item.properties.merge_target).toBeDefined();
|
||||
});
|
||||
|
||||
it('op enum matches ReflectionOp and type enum matches ReflectionMemoryType', () => {
|
||||
const item = params.properties.memory_changes.items;
|
||||
expect(item.properties.op.enum).toEqual(['add', 'update', 'merge_into', 'remove']);
|
||||
expect(item.properties.type.enum).toEqual(['user', 'feedback', 'project', 'reference']);
|
||||
});
|
||||
|
||||
it('enforces size ceilings: name<=96, description<=240, body<=16384', () => {
|
||||
const item = params.properties.memory_changes.items;
|
||||
expect(item.properties.name.minLength).toBe(1);
|
||||
expect(item.properties.name.maxLength).toBe(96);
|
||||
expect(item.properties.description.maxLength).toBe(240);
|
||||
expect(item.properties.body.maxLength).toBe(16384);
|
||||
expect(item.properties.merge_target.maxLength).toBe(96);
|
||||
});
|
||||
|
||||
it('piece_changes requires only should_edit and allows nullable new_yaml', () => {
|
||||
const pc = params.properties.piece_changes;
|
||||
expect(pc.type).toBe('object');
|
||||
expect(pc.required).toEqual(['should_edit']);
|
||||
expect(pc.additionalProperties).toBe(false);
|
||||
expect(pc.properties.should_edit.type).toBe('boolean');
|
||||
expect(pc.properties.new_yaml.type).toEqual(['string', 'null']);
|
||||
expect(pc.properties.diff_summary.maxLength).toBe(240);
|
||||
});
|
||||
|
||||
it('reasoning and abstain_reason have length ceilings', () => {
|
||||
expect(params.properties.reasoning.maxLength).toBe(2000);
|
||||
expect(params.properties.abstain_reason.maxLength).toBe(500);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,40 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { createHash } from 'crypto';
|
||||
import { bodyRevision } from './revisions.js';
|
||||
|
||||
describe('bodyRevision', () => {
|
||||
it('returns the sha1 hex digest of the body string', () => {
|
||||
const body = 'The user prefers markdown output.\n';
|
||||
const expected = createHash('sha1').update(body).digest('hex');
|
||||
expect(bodyRevision(body)).toBe(expected);
|
||||
});
|
||||
|
||||
it('is deterministic for the same input', () => {
|
||||
expect(bodyRevision('same input')).toBe(bodyRevision('same input'));
|
||||
});
|
||||
|
||||
it('produces different digests for different bodies', () => {
|
||||
expect(bodyRevision('body A')).not.toBe(bodyRevision('body B'));
|
||||
});
|
||||
|
||||
it('is sensitive to trailing whitespace (gray-matter newline normalization matters)', () => {
|
||||
// The doc comment in revisions.ts says the parsed body (which gray-matter
|
||||
// round-trips with a trailing newline) is the canonical input. A body with
|
||||
// and without the trailing newline must therefore hash differently.
|
||||
expect(bodyRevision('content')).not.toBe(bodyRevision('content\n'));
|
||||
});
|
||||
|
||||
it('returns a 40-char lowercase hex string', () => {
|
||||
expect(bodyRevision('anything')).toMatch(/^[0-9a-f]{40}$/);
|
||||
});
|
||||
|
||||
it('handles the empty string (well-known sha1)', () => {
|
||||
expect(bodyRevision('')).toBe('da39a3ee5e6b4b0d3255bfef95601890afd80709');
|
||||
});
|
||||
|
||||
it('handles multibyte (Japanese) content as UTF-8', () => {
|
||||
const body = '教訓: ユーザーは簡潔な回答を好む。';
|
||||
const expected = createHash('sha1').update(body, 'utf8').digest('hex');
|
||||
expect(bodyRevision(body)).toBe(expected);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,52 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { stripThinkingTokens } from './strip-thinking.js';
|
||||
|
||||
describe('stripThinkingTokens', () => {
|
||||
it('returns plain text unchanged (modulo trim)', () => {
|
||||
expect(stripThinkingTokens('hello world')).toBe('hello world');
|
||||
expect(stripThinkingTokens(' padded ')).toBe('padded');
|
||||
});
|
||||
|
||||
it('handles empty input', () => {
|
||||
expect(stripThinkingTokens('')).toBe('');
|
||||
});
|
||||
|
||||
it('strips a DeepSeek-style <think> block', () => {
|
||||
expect(stripThinkingTokens('<think>internal reasoning</think>answer')).toBe('answer');
|
||||
});
|
||||
|
||||
it('strips multiple <think> blocks non-greedily', () => {
|
||||
const input = '<think>a</think>first<think>b</think>second';
|
||||
expect(stripThinkingTokens(input)).toBe('firstsecond');
|
||||
});
|
||||
|
||||
it('strips multiline <think> content', () => {
|
||||
const input = '<think>line1\nline2\n</think>\nresult';
|
||||
expect(stripThinkingTokens(input)).toBe('result');
|
||||
});
|
||||
|
||||
it('leaves an unclosed <think> block intact', () => {
|
||||
const input = '<think>never closed... answer';
|
||||
expect(stripThinkingTokens(input)).toBe('<think>never closed... answer');
|
||||
});
|
||||
|
||||
it('strips generic <|thinking|> blocks', () => {
|
||||
expect(stripThinkingTokens('<|thinking|>hmm<|/thinking|>ok')).toBe('ok');
|
||||
});
|
||||
|
||||
it('strips Gemma-style thought + channel marker', () => {
|
||||
expect(stripThinkingTokens('thought\n<channel|>visible')).toBe('visible');
|
||||
});
|
||||
|
||||
it('strips paired <channel|> blocks', () => {
|
||||
expect(stripThinkingTokens('<channel|>internal<channel|>visible')).toBe('visible');
|
||||
});
|
||||
|
||||
it('preserves unicode content outside thinking blocks', () => {
|
||||
expect(stripThinkingTokens('<think>思考</think>日本語の回答')).toBe('日本語の回答');
|
||||
});
|
||||
|
||||
it('returns empty string when the whole response is a thinking block', () => {
|
||||
expect(stripThinkingTokens('<think>only thoughts</think>')).toBe('');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,152 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import type { ToolContext } from './core.js';
|
||||
import { executeTool, ensureKeepaGraphs, TOOL_DEFS } from './amazon.js';
|
||||
import type { AmazonProductData } from './structured-blocks.js';
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
const ctx = (toolsConfig: Record<string, unknown> = {}): ToolContext =>
|
||||
({ toolsConfig }) as unknown as ToolContext;
|
||||
|
||||
function productBlock(asin: string, title: string, opts: { price?: string; rating?: string; reviews?: string } = {}): string {
|
||||
return `
|
||||
<div data-asin="${asin}" data-component-type="s-search-result">
|
||||
<img class="s-image" src="https://m.media-amazon.com/images/${asin}.jpg" alt=""/>
|
||||
<h2 class="title"><a href="/dp/${asin}"><span>${title}</span></a></h2>
|
||||
${opts.price ? `<span class="a-price"><span class="a-offscreen">${opts.price}</span></span>` : ''}
|
||||
${opts.rating ? `<span class="a-icon-alt">5つ星のうち${opts.rating}</span>` : ''}
|
||||
${opts.reviews ? `<span aria-label="${opts.reviews}件の評価">${opts.reviews}</span>` : ''}
|
||||
</div>`;
|
||||
}
|
||||
|
||||
const SEARCH_HTML = `<html><body>
|
||||
${productBlock('B012345678', 'ワイヤレスイヤホン & ケース', { price: '¥12,980', rating: '4.5', reviews: '1,234' })}
|
||||
${productBlock('B087654321', 'モバイルバッテリー')}
|
||||
</body></html>`;
|
||||
|
||||
function htmlResponse(body: string, status = 200): Response {
|
||||
return new Response(body, { status, headers: { 'content-type': 'text/html' } });
|
||||
}
|
||||
|
||||
describe('TOOL_DEFS', () => {
|
||||
it('registers SearchAmazon with query required', () => {
|
||||
expect(TOOL_DEFS['SearchAmazon']?.function.name).toBe('SearchAmazon');
|
||||
expect(TOOL_DEFS['SearchAmazon']?.function.parameters?.['required']).toEqual(['query']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('SearchAmazon', () => {
|
||||
it('returns null for unknown tool names', async () => {
|
||||
expect(await executeTool('NotAmazon', {}, ctx())).toBeNull();
|
||||
});
|
||||
|
||||
it('requires a query', async () => {
|
||||
const res = await executeTool('SearchAmazon', {}, ctx());
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('query is required');
|
||||
});
|
||||
|
||||
it('parses products into markdown with Keepa graphs and structured blocks', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => htmlResponse(SEARCH_HTML)));
|
||||
const res = await executeTool('SearchAmazon', { query: 'イヤホン' }, ctx());
|
||||
|
||||
expect(res?.isError).toBe(false);
|
||||
expect(res?.output).toContain('## Amazon.co.jp 検索結果: 「イヤホン」');
|
||||
// Entity-decoded title, price, rating with review count.
|
||||
expect(res?.output).toContain('ワイヤレスイヤホン & ケース');
|
||||
expect(res?.output).toContain('- **価格**: ¥12,980');
|
||||
expect(res?.output).toContain('- **評価**: 4.5 (1,234件)');
|
||||
expect(res?.output).toContain('');
|
||||
expect(res?.output).toContain('graph.keepa.com/pricehistory.png?asin=B012345678&domain=co.jp');
|
||||
expect(res?.output).toContain('https://www.amazon.co.jp/dp/B012345678');
|
||||
expect(res?.output).toMatch(/\[\[embed:amazon-\d+\]\]/);
|
||||
|
||||
const blocks = res?.structuredBlocks ?? [];
|
||||
expect(blocks).toHaveLength(1);
|
||||
expect(blocks[0]?.type).toBe('amazon_products');
|
||||
const data = blocks[0]?.data as AmazonProductData;
|
||||
expect(data.products).toHaveLength(2);
|
||||
expect(data.products[0]).toMatchObject({
|
||||
asin: 'B012345678',
|
||||
rating: 4.5,
|
||||
reviewCount: 1234,
|
||||
});
|
||||
});
|
||||
|
||||
it('parses an integer rating from 5つ星のうち5', async () => {
|
||||
const html = `<html><body>${productBlock('B099999999', '満点商品', { rating: '5', reviews: '10' })}</body></html>`;
|
||||
vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => htmlResponse(html)));
|
||||
const res = await executeTool('SearchAmazon', { query: 'x' }, ctx());
|
||||
expect(res?.output).toContain('- **評価**: 5 (10件)');
|
||||
});
|
||||
|
||||
it('appends the affiliate tag from tools config to product URLs', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => htmlResponse(SEARCH_HTML)));
|
||||
const res = await executeTool(
|
||||
'SearchAmazon',
|
||||
{ query: 'イヤホン' },
|
||||
ctx({ amazonAffiliateTag: 'mytag-22' }),
|
||||
);
|
||||
expect(res?.output).toContain('https://www.amazon.co.jp/dp/B012345678?tag=mytag-22');
|
||||
});
|
||||
|
||||
it('caps max_results', async () => {
|
||||
const many = `<html><body>${Array.from({ length: 12 }, (_, i) =>
|
||||
productBlock(`B0000000${String(i).padStart(2, '0')}`.slice(0, 10), `商品${i}`),
|
||||
).join('\n')}</body></html>`;
|
||||
vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => htmlResponse(many)));
|
||||
const res = await executeTool('SearchAmazon', { query: 'x', max_results: 3 }, ctx());
|
||||
const data = (res?.structuredBlocks?.[0]?.data ?? { products: [] }) as AmazonProductData;
|
||||
expect(data.products).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('suggests BrowseWeb when parsing finds nothing (likely blocked)', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => htmlResponse('<html>captcha</html>')));
|
||||
const res = await executeTool('SearchAmazon', { query: 'イヤホン' }, ctx());
|
||||
expect(res?.isError).toBe(false);
|
||||
expect(res?.output).toContain('BrowseWeb');
|
||||
expect(res?.output).toContain('取得できませんでした');
|
||||
});
|
||||
|
||||
it('reports HTTP failures as tool errors with a fallback hint', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => htmlResponse('nope', 503)));
|
||||
const res = await executeTool('SearchAmazon', { query: 'イヤホン' }, ctx());
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('Amazon 検索に失敗しました');
|
||||
expect(res?.output).toContain('BrowseWeb');
|
||||
});
|
||||
});
|
||||
|
||||
describe('ensureKeepaGraphs', () => {
|
||||
it('appends missing Keepa graphs for ASINs referenced in the text', () => {
|
||||
const text = 'おすすめ: https://www.amazon.co.jp/dp/B012345678 です。';
|
||||
const out = ensureKeepaGraphs(text);
|
||||
expect(out).toContain('### 価格推移 (Keepa)');
|
||||
expect(out).toContain('pricehistory.png?asin=B012345678');
|
||||
});
|
||||
|
||||
it('leaves text alone when graphs are already present', () => {
|
||||
const text = [
|
||||
'https://www.amazon.co.jp/dp/B012345678',
|
||||
'',
|
||||
].join('\n');
|
||||
expect(ensureKeepaGraphs(text)).toBe(text);
|
||||
});
|
||||
|
||||
it('leaves text without ASINs untouched', () => {
|
||||
expect(ensureKeepaGraphs('no products here')).toBe('no products here');
|
||||
});
|
||||
|
||||
it('only appends graphs for the ASINs that are missing', () => {
|
||||
const text = [
|
||||
'https://www.amazon.co.jp/dp/B012345678',
|
||||
'https://www.amazon.co.jp/dp/B087654321',
|
||||
'',
|
||||
].join('\n');
|
||||
const out = ensureKeepaGraphs(text);
|
||||
expect(out.match(/pricehistory\.png\?asin=B087654321/g)).toHaveLength(1);
|
||||
expect(out.match(/pricehistory\.png\?asin=B012345678/g)).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
@@ -85,8 +85,9 @@ function parseProducts(html: string, maxResults: number): AmazonProduct[] {
|
||||
}
|
||||
|
||||
// Extract rating: <span class="a-icon-alt">5つ星のうち4.5</span>
|
||||
const ratingMatch = block.match(/<span class="a-icon-alt">([\d.]+つ星のうち[\d.]+)<\/span>/i)
|
||||
|| block.match(/(\d+(?:\.\d+)?)\s*つ星のうち/i);
|
||||
// The actual rating is the number AFTER のうち; the one before is the scale.
|
||||
const ratingMatch = block.match(/<span class="a-icon-alt">[\d.]+つ星のうち([\d.]+)<\/span>/i)
|
||||
|| block.match(/つ星のうち\s*(\d+(?:\.\d+)?)/i);
|
||||
if (ratingMatch) {
|
||||
const rVal = ratingMatch[1].match(/(\d+(?:\.\d+)?)/);
|
||||
if (rVal) product.rating = rVal[1];
|
||||
|
||||
@@ -0,0 +1,479 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { tmpdir } from 'os';
|
||||
import Database from 'better-sqlite3';
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
import { executeTool, TOOL_DEFS } from './data.js';
|
||||
import type { ToolContext } from './core.js';
|
||||
|
||||
function makeWorkspace(): string {
|
||||
return fs.mkdtempSync(path.join(tmpdir(), 'maestro-data-'));
|
||||
}
|
||||
|
||||
function makeContext(workspacePath: string, editAllowed = false): ToolContext {
|
||||
return { workspacePath, editAllowed };
|
||||
}
|
||||
|
||||
/** Create a fixture DB with a users table inside the workspace. */
|
||||
function createFixtureDb(workspacePath: string, fileName = 'fixture.db'): string {
|
||||
const dbPath = path.join(workspacePath, fileName);
|
||||
const db = new Database(dbPath);
|
||||
db.exec(`
|
||||
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER);
|
||||
INSERT INTO users (name, age) VALUES ('alice', 30), ('bob', 25), ('carol', 35);
|
||||
`);
|
||||
db.close();
|
||||
return fileName;
|
||||
}
|
||||
|
||||
describe('SQLite tool', () => {
|
||||
let workspacePath = '';
|
||||
|
||||
afterEach(() => {
|
||||
if (workspacePath) {
|
||||
fs.rmSync(workspacePath, { recursive: true, force: true });
|
||||
workspacePath = '';
|
||||
}
|
||||
});
|
||||
|
||||
it('exposes only the SQLite tool def', () => {
|
||||
expect(Object.keys(TOOL_DEFS)).toEqual(['SQLite']);
|
||||
expect(TOOL_DEFS['SQLite']!.function.name).toBe('SQLite');
|
||||
});
|
||||
|
||||
it('returns null for unknown tool names', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const result = await executeTool('NotATool', { query: 'SELECT 1' }, makeContext(workspacePath));
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
describe('SELECT happy path', () => {
|
||||
it('returns a formatted table for a SELECT in read-only mode', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'SELECT name, age FROM users ORDER BY age', db_path: dbFile },
|
||||
makeContext(workspacePath, false),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('name');
|
||||
expect(result?.output).toContain('age');
|
||||
expect(result?.output).toContain('bob');
|
||||
expect(result?.output).toContain('alice');
|
||||
expect(result?.output).toContain('carol');
|
||||
expect(result?.output).toContain('(3 rows)');
|
||||
});
|
||||
|
||||
it('formats an empty result set as (0 rows)', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: "SELECT * FROM users WHERE name = 'nobody'", db_path: dbFile },
|
||||
makeContext(workspacePath, false),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toBe('(0 rows)');
|
||||
});
|
||||
|
||||
it('uses singular "row" for a single-row result', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: "SELECT name FROM users WHERE name = 'alice'", db_path: dbFile },
|
||||
makeContext(workspacePath, false),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('(1 row)');
|
||||
});
|
||||
|
||||
it('renders NULL values as NULL', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
const db = new Database(path.join(workspacePath, dbFile));
|
||||
db.exec("INSERT INTO users (name, age) VALUES ('dave', NULL)");
|
||||
db.close();
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: "SELECT age FROM users WHERE name = 'dave'", db_path: dbFile },
|
||||
makeContext(workspacePath, false),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('NULL');
|
||||
});
|
||||
|
||||
it('executes multiple SELECT statements and joins their outputs', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{
|
||||
query: "SELECT name FROM users WHERE name = 'alice'; SELECT name FROM users WHERE name = 'bob';",
|
||||
db_path: dbFile,
|
||||
},
|
||||
makeContext(workspacePath, false),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('alice');
|
||||
expect(result?.output).toContain('bob');
|
||||
});
|
||||
});
|
||||
|
||||
describe('write guards (editAllowed=false)', () => {
|
||||
it.each(['INSERT', 'UPDATE', 'DELETE', 'REPLACE'])('rejects %s in read-only mode', async (kw) => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
const queries: Record<string, string> = {
|
||||
INSERT: "INSERT INTO users (name, age) VALUES ('mallory', 1)",
|
||||
UPDATE: "UPDATE users SET age = 99 WHERE name = 'alice'",
|
||||
DELETE: 'DELETE FROM users',
|
||||
REPLACE: "REPLACE INTO users (id, name, age) VALUES (1, 'evil', 0)",
|
||||
};
|
||||
|
||||
const result = await executeTool('SQLite', { query: queries[kw]!, db_path: dbFile }, makeContext(workspacePath, false));
|
||||
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('Only SELECT queries are allowed');
|
||||
});
|
||||
|
||||
it('rejects a write smuggled after a SELECT in a multi-statement query', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'SELECT * FROM users; DELETE FROM users', db_path: dbFile },
|
||||
makeContext(workspacePath, false),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(true);
|
||||
// Guard fires before anything executes; data must be intact
|
||||
const db = new Database(path.join(workspacePath, dbFile), { readonly: true });
|
||||
const count = db.prepare('SELECT COUNT(*) AS c FROM users').get() as { c: number };
|
||||
db.close();
|
||||
expect(count.c).toBe(3);
|
||||
});
|
||||
|
||||
it('rejects CREATE TABLE in read-only mode', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'CREATE TABLE extra (id INTEGER)', db_path: dbFile },
|
||||
makeContext(workspacePath, false),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('Only SELECT queries are allowed');
|
||||
});
|
||||
});
|
||||
|
||||
describe('writes allowed (editAllowed=true)', () => {
|
||||
it('supports CREATE TABLE + INSERT + SELECT in one call', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{
|
||||
query: "CREATE TABLE t (v TEXT); INSERT INTO t (v) VALUES ('x'), ('y'); SELECT COUNT(*) AS c FROM t",
|
||||
db_path: 'new.db',
|
||||
},
|
||||
makeContext(workspacePath, true),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('OK');
|
||||
expect(result?.output).toContain('2 row(s) affected');
|
||||
expect(result?.output).toContain('2');
|
||||
expect(fs.existsSync(path.join(workspacePath, 'new.db'))).toBe(true);
|
||||
});
|
||||
|
||||
it('defaults db_path to temp.db inside the workspace', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'CREATE TABLE t (v TEXT)' },
|
||||
makeContext(workspacePath, true),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(fs.existsSync(path.join(workspacePath, 'temp.db'))).toBe(true);
|
||||
});
|
||||
|
||||
it('reports affected row count for UPDATE', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'UPDATE users SET age = age + 1', db_path: dbFile },
|
||||
makeContext(workspacePath, true),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('3 row(s) affected');
|
||||
});
|
||||
});
|
||||
|
||||
describe('always-blocked DDL', () => {
|
||||
it.each([
|
||||
['DROP TABLE users', 'DROP'],
|
||||
['ALTER TABLE users ADD COLUMN extra TEXT', 'ALTER'],
|
||||
["ATTACH DATABASE '/etc/passwd' AS evil", 'ATTACH'],
|
||||
['DETACH DATABASE evil', 'DETACH'],
|
||||
['REINDEX', 'REINDEX'],
|
||||
['VACUUM', 'VACUUM'],
|
||||
])('blocks "%s" even with editAllowed=true', async (query, kw) => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
|
||||
const result = await executeTool('SQLite', { query, db_path: dbFile }, makeContext(workspacePath, true));
|
||||
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('Forbidden SQL');
|
||||
expect(result?.output).toContain(kw);
|
||||
});
|
||||
|
||||
it.each([
|
||||
'CREATE INDEX idx ON users (name)',
|
||||
'CREATE UNIQUE INDEX idx ON users (name)',
|
||||
'CREATE TRIGGER trg AFTER INSERT ON users BEGIN SELECT 1; END',
|
||||
])('blocks "%s"', async (query) => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
|
||||
const result = await executeTool('SQLite', { query, db_path: dbFile }, makeContext(workspacePath, true));
|
||||
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('Forbidden SQL');
|
||||
});
|
||||
|
||||
it('is case-insensitive for blocked keywords', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'drop table users', db_path: dbFile },
|
||||
makeContext(workspacePath, true),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('Forbidden SQL');
|
||||
});
|
||||
|
||||
it('blocks DDL hidden behind a SELECT in a multi-statement query', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'SELECT 1; DROP TABLE users', db_path: dbFile },
|
||||
makeContext(workspacePath, true),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('Forbidden SQL');
|
||||
});
|
||||
});
|
||||
|
||||
describe('PRAGMA handling', () => {
|
||||
it('allows PRAGMA table_info when edit is enabled', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'PRAGMA table_info(users)', db_path: dbFile },
|
||||
makeContext(workspacePath, true),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('name');
|
||||
expect(result?.output).toContain('age');
|
||||
});
|
||||
|
||||
it('blocks PRAGMA other than table_info/table_list', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'PRAGMA journal_mode = WAL', db_path: dbFile },
|
||||
makeContext(workspacePath, true),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('PRAGMA');
|
||||
expect(result?.output).toContain('not allowed');
|
||||
});
|
||||
|
||||
// Documents current behavior: PRAGMA table_info survives the DDL block list
|
||||
// but the read-only gate only whitelists SELECT, so it is rejected when
|
||||
// editAllowed=false. Schema inspection therefore requires edit mode.
|
||||
it('rejects PRAGMA table_info in read-only mode (current behavior)', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'PRAGMA table_info(users)', db_path: dbFile },
|
||||
makeContext(workspacePath, false),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('Only SELECT queries are allowed');
|
||||
});
|
||||
});
|
||||
|
||||
describe('errors', () => {
|
||||
it('rejects an empty query', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
|
||||
const result = await executeTool('SQLite', { query: ' ;; ' }, makeContext(workspacePath, true));
|
||||
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toBe('Empty query');
|
||||
});
|
||||
|
||||
it('returns a Query error for SQL referencing a missing table', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'SELECT * FROM no_such_table', db_path: dbFile },
|
||||
makeContext(workspacePath, false),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('Query error');
|
||||
expect(result?.output).toContain('no_such_table');
|
||||
});
|
||||
|
||||
it('returns a syntax error as Query error', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbFile = createFixtureDb(workspacePath);
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'SELECT FROM WHERE', db_path: dbFile },
|
||||
makeContext(workspacePath, false),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('Query error');
|
||||
});
|
||||
|
||||
it('fails to open a missing DB file in read-only mode', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'SELECT 1', db_path: 'does-not-exist.db' },
|
||||
makeContext(workspacePath, false),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('Failed to open database');
|
||||
});
|
||||
|
||||
it('returns a Query error when the file is not a SQLite database', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
fs.writeFileSync(path.join(workspacePath, 'not-a-db.db'), 'this is plain text, not sqlite');
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'SELECT 1', db_path: 'not-a-db.db' },
|
||||
makeContext(workspacePath, false),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('path traversal guard', () => {
|
||||
it.each([
|
||||
'../outside.db',
|
||||
'../../etc/evil.db',
|
||||
'sub/../../escape.db',
|
||||
])('rejects db_path "%s" outside the workspace', async (dbPath) => {
|
||||
workspacePath = makeWorkspace();
|
||||
|
||||
const result = await executeTool('SQLite', { query: 'SELECT 1', db_path: dbPath }, makeContext(workspacePath, true));
|
||||
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('Path traversal');
|
||||
});
|
||||
|
||||
it('rejects an absolute db_path outside the workspace', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'SELECT 1', db_path: '/tmp/evil-absolute.db' },
|
||||
makeContext(workspacePath, true),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('Path traversal');
|
||||
});
|
||||
|
||||
it('allows a relative path inside a workspace subdirectory', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
fs.mkdirSync(path.join(workspacePath, 'output'), { recursive: true });
|
||||
createFixtureDb(workspacePath, path.join('output', 'nested.db'));
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'SELECT COUNT(*) AS c FROM users', db_path: 'output/nested.db' },
|
||||
makeContext(workspacePath, false),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('3');
|
||||
});
|
||||
});
|
||||
|
||||
describe('large result sets', () => {
|
||||
// Documents current behavior: the tool has no output truncation, so a
|
||||
// large SELECT returns every row verbatim.
|
||||
it('returns all rows of a large SELECT (no truncation in the tool itself)', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const dbPath = path.join(workspacePath, 'big.db');
|
||||
const db = new Database(dbPath);
|
||||
db.exec('CREATE TABLE big (id INTEGER PRIMARY KEY, payload TEXT)');
|
||||
const insert = db.prepare('INSERT INTO big (payload) VALUES (?)');
|
||||
const tx = db.transaction(() => {
|
||||
for (let i = 0; i < 2000; i++) insert.run(`row-payload-${i}-${'x'.repeat(50)}`);
|
||||
});
|
||||
tx();
|
||||
db.close();
|
||||
|
||||
const result = await executeTool(
|
||||
'SQLite',
|
||||
{ query: 'SELECT * FROM big', db_path: 'big.db' },
|
||||
makeContext(workspacePath, false),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('(2000 rows)');
|
||||
expect(result?.output).toContain('row-payload-0-');
|
||||
expect(result?.output).toContain('row-payload-1999-');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,566 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { tmpdir } from 'os';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { executeTool } from './maps.js';
|
||||
import type { ToolContext } from './core.js';
|
||||
|
||||
function makeWorkspace(): string {
|
||||
return fs.mkdtempSync(path.join(tmpdir(), 'maestro-maps-'));
|
||||
}
|
||||
|
||||
function makeContext(workspacePath: string, opts: { apiKey?: string } = {}): ToolContext {
|
||||
return {
|
||||
workspacePath,
|
||||
editAllowed: false,
|
||||
toolsConfig: {
|
||||
...(opts.apiKey ? { googleMapsApiKey: opts.apiKey } : {}),
|
||||
mapsTimeout: 5,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function jsonResponse(body: unknown, status = 200): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
});
|
||||
}
|
||||
|
||||
const NOMINATIM_PLACE = {
|
||||
place_id: 1,
|
||||
lat: '35.658584',
|
||||
lon: '139.745433',
|
||||
display_name: '東京タワー, 4, 芝公園, 港区, 東京都, 105-0011, 日本',
|
||||
address: { country: '日本' },
|
||||
type: 'attraction',
|
||||
class: 'tourism',
|
||||
importance: 0.7,
|
||||
};
|
||||
|
||||
const OSRM_ROUTE = {
|
||||
code: 'Ok',
|
||||
routes: [
|
||||
{
|
||||
distance: 3456.7,
|
||||
duration: 754,
|
||||
legs: [
|
||||
{
|
||||
steps: [
|
||||
{ maneuver: { type: 'depart' }, distance: 100.4, duration: 30, name: '桜田通り' },
|
||||
{ maneuver: { type: 'turn', modifier: 'left' }, distance: 200, duration: 60, name: '' },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
describe('maps tools', () => {
|
||||
let workspacePath = '';
|
||||
|
||||
afterEach(() => {
|
||||
if (workspacePath) {
|
||||
fs.rmSync(workspacePath, { recursive: true, force: true });
|
||||
workspacePath = '';
|
||||
}
|
||||
vi.unstubAllGlobals();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('returns null for unknown tool name', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const result = await executeTool('NotAMapsTool', {}, makeContext(workspacePath));
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// SearchPlaces
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
describe('SearchPlaces', () => {
|
||||
it('rejects missing query', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const result = await executeTool('SearchPlaces', {}, makeContext(workspacePath));
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('query は必須です');
|
||||
});
|
||||
|
||||
it('rejects whitespace-only query', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const result = await executeTool('SearchPlaces', { query: ' ' }, makeContext(workspacePath));
|
||||
expect(result?.isError).toBe(true);
|
||||
});
|
||||
|
||||
it('searches via Nominatim when no API key is configured', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const fetchMock = vi.fn().mockResolvedValue(jsonResponse([NOMINATIM_PLACE]));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const result = await executeTool('SearchPlaces', { query: '東京タワー' }, makeContext(workspacePath));
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('## 地図検索結果: 東京タワー');
|
||||
expect(result?.output).toContain('1件の場所が見つかりました');
|
||||
expect(result?.output).toContain('### 1. 東京タワー');
|
||||
expect(result?.output).toContain('**座標**: 35.658584, 139.745433');
|
||||
expect(result?.output).toContain('**種別**: attraction');
|
||||
expect(result?.output).toContain('https://www.openstreetmap.org/?mlat=35.658584&mlon=139.745433&zoom=17');
|
||||
// embed marker + structured block
|
||||
expect(result?.output).toMatch(/\[\[embed:map-\d+\]\]/);
|
||||
expect(result?.structuredBlocks).toHaveLength(1);
|
||||
const block = result?.structuredBlocks?.[0];
|
||||
expect(block?.type).toBe('map_places');
|
||||
expect(block?.data).toMatchObject({
|
||||
query: '東京タワー',
|
||||
places: [
|
||||
expect.objectContaining({
|
||||
name: '東京タワー',
|
||||
lat: 35.658584,
|
||||
lon: 139.745433,
|
||||
type: 'attraction',
|
||||
}),
|
||||
],
|
||||
});
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1);
|
||||
const url = String(fetchMock.mock.calls[0]?.[0]);
|
||||
expect(url).toContain('nominatim.openstreetmap.org/search');
|
||||
expect(url).toContain('limit=5'); // default limit
|
||||
expect(url).toContain('accept-language=ja'); // default lang
|
||||
});
|
||||
|
||||
it('clamps limit to 1..20 and passes lang through', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const fetchMock = vi.fn().mockResolvedValue(jsonResponse([NOMINATIM_PLACE]));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
await executeTool('SearchPlaces', { query: 'tower', limit: 50, lang: 'en' }, makeContext(workspacePath));
|
||||
expect(String(fetchMock.mock.calls[0]?.[0])).toContain('limit=20');
|
||||
expect(String(fetchMock.mock.calls[0]?.[0])).toContain('accept-language=en');
|
||||
|
||||
await executeTool('SearchPlaces', { query: 'tower', limit: 0 }, makeContext(workspacePath));
|
||||
expect(String(fetchMock.mock.calls[1]?.[0])).toContain('limit=1');
|
||||
});
|
||||
|
||||
it('returns error on Nominatim HTTP failure', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse({}, 500)));
|
||||
|
||||
const result = await executeTool('SearchPlaces', { query: 'tower' }, makeContext(workspacePath));
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('Nominatim API エラー: HTTP 500');
|
||||
});
|
||||
|
||||
it('returns non-error message when no places match', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse([])));
|
||||
|
||||
const result = await executeTool('SearchPlaces', { query: 'nowhere-xyz' }, makeContext(workspacePath));
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('「nowhere-xyz」に一致する場所が見つかりませんでした');
|
||||
});
|
||||
|
||||
it('returns error when fetch throws', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('network down')));
|
||||
|
||||
const result = await executeTool('SearchPlaces', { query: 'tower' }, makeContext(workspacePath));
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('地図検索エラー: network down');
|
||||
});
|
||||
|
||||
it('uses Google Places API when API key is configured', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({
|
||||
status: 'OK',
|
||||
results: [
|
||||
{
|
||||
name: 'Tokyo Tower',
|
||||
formatted_address: '4 Chome-2-8 Shibakoen, Minato City',
|
||||
geometry: { location: { lat: 35.6586, lng: 139.7454 } },
|
||||
types: ['tourist_attraction'],
|
||||
rating: 4.5,
|
||||
opening_hours: { open_now: true },
|
||||
},
|
||||
],
|
||||
}));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const result = await executeTool(
|
||||
'SearchPlaces',
|
||||
{ query: 'Tokyo Tower' },
|
||||
makeContext(workspacePath, { apiKey: 'test-google-key' }),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('### 1. Tokyo Tower');
|
||||
expect(result?.output).toContain('**詳細**: 評価: 4.5, 営業中');
|
||||
expect(result?.output).toContain('**種別**: tourist_attraction');
|
||||
// only the Google endpoint was hit; no Nominatim fallback
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1);
|
||||
const url = String(fetchMock.mock.calls[0]?.[0]);
|
||||
expect(url).toContain('maps.googleapis.com/maps/api/place/textsearch/json');
|
||||
expect(url).toContain('key=test-google-key');
|
||||
});
|
||||
|
||||
it('falls back to Nominatim when Google returns no results', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const fetchMock = vi.fn()
|
||||
.mockResolvedValueOnce(jsonResponse({ status: 'ZERO_RESULTS', results: [] }))
|
||||
.mockResolvedValueOnce(jsonResponse([NOMINATIM_PLACE]));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const result = await executeTool(
|
||||
'SearchPlaces',
|
||||
{ query: '東京タワー' },
|
||||
makeContext(workspacePath, { apiKey: 'test-google-key' }),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('### 1. 東京タワー');
|
||||
expect(fetchMock).toHaveBeenCalledTimes(2);
|
||||
expect(String(fetchMock.mock.calls[1]?.[0])).toContain('nominatim.openstreetmap.org');
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// GetDirections
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
describe('GetDirections', () => {
|
||||
it('rejects missing origin / destination', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const ctx = makeContext(workspacePath);
|
||||
const noOrigin = await executeTool('GetDirections', { destination: 'B' }, ctx);
|
||||
expect(noOrigin?.isError).toBe(true);
|
||||
expect(noOrigin?.output).toContain('origin は必須です');
|
||||
|
||||
const noDest = await executeTool('GetDirections', { origin: 'A' }, ctx);
|
||||
expect(noDest?.isError).toBe(true);
|
||||
expect(noDest?.output).toContain('destination は必須です');
|
||||
});
|
||||
|
||||
it('routes via OSRM with "lat,lon" inputs (no geocoding round-trip)', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const fetchMock = vi.fn().mockResolvedValue(jsonResponse(OSRM_ROUTE));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const result = await executeTool(
|
||||
'GetDirections',
|
||||
{ origin: '35.6586,139.7454', destination: '35.681236, 139.767125' },
|
||||
makeContext(workspacePath),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('## 経路情報');
|
||||
expect(result?.output).toContain('**移動手段**: 車');
|
||||
expect(result?.output).toContain('**距離**: 3.5 km');
|
||||
expect(result?.output).toContain('**所要時間**: 13 分');
|
||||
expect(result?.output).toContain('### 経路ステップ');
|
||||
expect(result?.output).toContain('1. depart (桜田通り) — 100m');
|
||||
expect(result?.output).toContain('2. turn left — 200m');
|
||||
|
||||
// single OSRM call, lat/lon parsed directly (lon,lat order in URL)
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1);
|
||||
const url = String(fetchMock.mock.calls[0]?.[0]);
|
||||
expect(url).toContain('router.project-osrm.org/route/v1/car/');
|
||||
expect(url).toContain('139.7454,35.6586;139.767125,35.681236');
|
||||
});
|
||||
|
||||
it('maps walking/cycling modes to OSRM profiles', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const fetchMock = vi.fn().mockImplementation(async () => jsonResponse(OSRM_ROUTE));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
const ctx = makeContext(workspacePath);
|
||||
|
||||
const walking = await executeTool(
|
||||
'GetDirections',
|
||||
{ origin: '1,2', destination: '3,4', mode: 'walking' },
|
||||
ctx,
|
||||
);
|
||||
expect(walking?.output).toContain('**移動手段**: 徒歩');
|
||||
expect(String(fetchMock.mock.calls[0]?.[0])).toContain('/route/v1/foot/');
|
||||
|
||||
const cycling = await executeTool(
|
||||
'GetDirections',
|
||||
{ origin: '1,2', destination: '3,4', mode: 'cycling' },
|
||||
ctx,
|
||||
);
|
||||
expect(cycling?.output).toContain('**移動手段**: 自転車');
|
||||
expect(String(fetchMock.mock.calls[1]?.[0])).toContain('/route/v1/bike/');
|
||||
});
|
||||
|
||||
it('geocodes address inputs through Nominatim before calling OSRM', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const fetchMock = vi.fn().mockImplementation(async (rawUrl: string | URL) => {
|
||||
const url = String(rawUrl);
|
||||
if (url.includes('nominatim.openstreetmap.org/search')) {
|
||||
if (url.includes(encodeURIComponent('東京駅'))) {
|
||||
return jsonResponse([{ ...NOMINATIM_PLACE, lat: '35.681236', lon: '139.767125' }]);
|
||||
}
|
||||
return jsonResponse([NOMINATIM_PLACE]);
|
||||
}
|
||||
return jsonResponse(OSRM_ROUTE);
|
||||
});
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const result = await executeTool(
|
||||
'GetDirections',
|
||||
{ origin: '東京タワー', destination: '東京駅' },
|
||||
makeContext(workspacePath),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('**出発地**: 東京タワー');
|
||||
expect(result?.output).toContain('**目的地**: 東京駅');
|
||||
// 2 geocode calls + 1 OSRM call
|
||||
expect(fetchMock).toHaveBeenCalledTimes(3);
|
||||
const osrmCall = fetchMock.mock.calls.map((c) => String(c[0])).find((u) => u.includes('router.project-osrm.org'));
|
||||
expect(osrmCall).toContain('139.745433,35.658584;139.767125,35.681236');
|
||||
});
|
||||
|
||||
it('returns error when origin cannot be geocoded', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse([])));
|
||||
|
||||
const result = await executeTool(
|
||||
'GetDirections',
|
||||
{ origin: '存在しない場所xyz', destination: '1,2' },
|
||||
makeContext(workspacePath),
|
||||
);
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('出発地「存在しない場所xyz」の座標を取得できませんでした');
|
||||
});
|
||||
|
||||
it('returns error on OSRM HTTP failure', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse({}, 502)));
|
||||
|
||||
const result = await executeTool(
|
||||
'GetDirections',
|
||||
{ origin: '1,2', destination: '3,4' },
|
||||
makeContext(workspacePath),
|
||||
);
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('OSRM 経路取得エラー: HTTP 502');
|
||||
});
|
||||
|
||||
it('returns non-error message when OSRM finds no route', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse({ code: 'NoRoute', routes: [] })));
|
||||
|
||||
const result = await executeTool(
|
||||
'GetDirections',
|
||||
{ origin: '1,2', destination: '3,4' },
|
||||
makeContext(workspacePath),
|
||||
);
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('経路が見つかりませんでした');
|
||||
});
|
||||
|
||||
it('writes a Leaflet HTML file when output_html=true', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse(OSRM_ROUTE)));
|
||||
|
||||
const result = await executeTool(
|
||||
'GetDirections',
|
||||
{ origin: '1,2', destination: '3,4', output_html: true, filename: 'my_route' },
|
||||
makeContext(workspacePath),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
// .html is appended when missing
|
||||
expect(result?.output).toContain('output/maps/my_route.html');
|
||||
const filePath = path.join(workspacePath, 'output', 'maps', 'my_route.html');
|
||||
expect(fs.existsSync(filePath)).toBe(true);
|
||||
const html = fs.readFileSync(filePath, 'utf-8');
|
||||
expect(html).toContain('leaflet');
|
||||
expect(html).toContain('3.5 km');
|
||||
expect(html).toContain('13 分');
|
||||
});
|
||||
|
||||
it('escapes HTML in generated route file names', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const fetchMock = vi.fn().mockImplementation(async (rawUrl: string | URL) => {
|
||||
const url = String(rawUrl);
|
||||
if (url.includes('nominatim')) return jsonResponse([NOMINATIM_PLACE]);
|
||||
return jsonResponse(OSRM_ROUTE);
|
||||
});
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const result = await executeTool(
|
||||
'GetDirections',
|
||||
{ origin: '<script>x</script>', destination: '1,2', output_html: true, filename: 'esc.html' },
|
||||
makeContext(workspacePath),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
const html = fs.readFileSync(path.join(workspacePath, 'output', 'maps', 'esc.html'), 'utf-8');
|
||||
expect(html).not.toContain('<script>x</script>');
|
||||
expect(html).toContain('<script>x</script>');
|
||||
});
|
||||
|
||||
it('uses Google Directions API when API key is configured', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({
|
||||
status: 'OK',
|
||||
routes: [
|
||||
{
|
||||
legs: [
|
||||
{
|
||||
distance: { text: '3.4 km' },
|
||||
duration: { text: '12分' },
|
||||
steps: [
|
||||
{ html_instructions: '<b>南</b>に進む', distance: { text: '0.2 km' } },
|
||||
],
|
||||
start_address: '日本、東京都港区芝公園',
|
||||
end_address: '日本、東京都千代田区丸の内',
|
||||
start_location: { lat: 35.6586, lng: 139.7454 },
|
||||
end_location: { lat: 35.6812, lng: 139.7671 },
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const result = await executeTool(
|
||||
'GetDirections',
|
||||
{ origin: '東京タワー', destination: '東京駅' },
|
||||
makeContext(workspacePath, { apiKey: 'test-google-key' }),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('**距離**: 3.4 km');
|
||||
expect(result?.output).toContain('**所要時間**: 12分');
|
||||
expect(result?.output).toContain('**出発地**: 日本、東京都港区芝公園');
|
||||
// HTML tags stripped from step instructions
|
||||
expect(result?.output).toContain('南に進む (0.2 km)');
|
||||
expect(result?.output).not.toContain('<b>');
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1);
|
||||
expect(String(fetchMock.mock.calls[0]?.[0])).toContain('maps.googleapis.com/maps/api/directions/json');
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// ReverseGeocode
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
describe('ReverseGeocode', () => {
|
||||
it('rejects non-numeric coordinates', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const result = await executeTool('ReverseGeocode', { lat: 'abc', lon: 139 }, makeContext(workspacePath));
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('lat と lon は数値で指定してください');
|
||||
});
|
||||
|
||||
it('rejects out-of-range coordinates', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const ctx = makeContext(workspacePath);
|
||||
const badLat = await executeTool('ReverseGeocode', { lat: 91, lon: 0 }, ctx);
|
||||
expect(badLat?.isError).toBe(true);
|
||||
expect(badLat?.output).toContain('座標の範囲が無効です');
|
||||
|
||||
const badLon = await executeTool('ReverseGeocode', { lat: 0, lon: -181 }, ctx);
|
||||
expect(badLon?.isError).toBe(true);
|
||||
});
|
||||
|
||||
it('returns formatted address components on success', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({
|
||||
place_id: 1,
|
||||
lat: '35.658584',
|
||||
lon: '139.745433',
|
||||
display_name: '東京タワー, 4, 芝公園, 港区, 東京都, 105-0011, 日本',
|
||||
address: {
|
||||
amenity: '東京タワー',
|
||||
house_number: '4',
|
||||
road: '都道301号',
|
||||
suburb: '芝公園',
|
||||
city: '港区',
|
||||
state: '東京都',
|
||||
postcode: '105-0011',
|
||||
country: '日本',
|
||||
country_code: 'jp',
|
||||
},
|
||||
boundingbox: ['35.657', '35.660', '139.744', '139.747'],
|
||||
}));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const result = await executeTool(
|
||||
'ReverseGeocode',
|
||||
{ lat: 35.658584, lon: 139.745433 },
|
||||
makeContext(workspacePath),
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('## 逆ジオコーディング結果');
|
||||
expect(result?.output).toContain('**座標**: 35.658584, 139.745433');
|
||||
expect(result?.output).toContain('**住所(全体)**: 東京タワー, 4, 芝公園, 港区, 東京都, 105-0011, 日本');
|
||||
expect(result?.output).toContain('- **郵便番号**: 105-0011');
|
||||
expect(result?.output).toContain('- **国**: 日本');
|
||||
expect(result?.output).toContain('- **都道府県**: 東京都');
|
||||
expect(result?.output).toContain('- **市区町村**: 港区');
|
||||
expect(result?.output).toContain('- **地区**: 芝公園');
|
||||
expect(result?.output).toContain('- **道路/通り**: 都道301号');
|
||||
expect(result?.output).toContain('- **番地**: 4');
|
||||
expect(result?.output).toContain('- **施設**: 東京タワー');
|
||||
expect(result?.output).toContain('https://www.openstreetmap.org/?mlat=35.658584&mlon=139.745433&zoom=17');
|
||||
|
||||
const url = String(fetchMock.mock.calls[0]?.[0]);
|
||||
expect(url).toContain('nominatim.openstreetmap.org/reverse');
|
||||
expect(url).toContain('lat=35.658584');
|
||||
expect(url).toContain('lon=139.745433');
|
||||
expect(url).toContain('accept-language=ja');
|
||||
});
|
||||
|
||||
it('accepts numeric strings for lat/lon', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
const fetchMock = vi.fn().mockResolvedValue(jsonResponse({
|
||||
place_id: 1,
|
||||
lat: '35.0',
|
||||
lon: '139.0',
|
||||
display_name: 'どこか',
|
||||
address: {},
|
||||
boundingbox: [],
|
||||
}));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const result = await executeTool(
|
||||
'ReverseGeocode',
|
||||
{ lat: '35.0', lon: '139.0' },
|
||||
makeContext(workspacePath),
|
||||
);
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('**住所(全体)**: どこか');
|
||||
});
|
||||
|
||||
it('returns error on Nominatim HTTP failure', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse({}, 404)));
|
||||
|
||||
const result = await executeTool('ReverseGeocode', { lat: 35, lon: 139 }, makeContext(workspacePath));
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('Nominatim API エラー: HTTP 404');
|
||||
});
|
||||
|
||||
it('returns non-error message when address is not found', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(jsonResponse({ error: 'Unable to geocode' })));
|
||||
|
||||
const result = await executeTool('ReverseGeocode', { lat: 0, lon: 0 }, makeContext(workspacePath));
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('座標 (0, 0) の住所が見つかりませんでした');
|
||||
});
|
||||
|
||||
it('returns error when fetch throws', async () => {
|
||||
workspacePath = makeWorkspace();
|
||||
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('boom')));
|
||||
|
||||
const result = await executeTool('ReverseGeocode', { lat: 35, lon: 139 }, makeContext(workspacePath));
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('逆ジオコーディングエラー: boom');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,244 @@
|
||||
import { mkdtempSync, rmSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { tmpdir } from 'os';
|
||||
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
|
||||
import type { ToolContext, ToolResult } from './core.js';
|
||||
|
||||
// The cache DB path is resolved from process.cwd() at module load, so stub
|
||||
// cwd BEFORE importing the module to keep the sqlite cache in a temp dir.
|
||||
const tmpRoot = mkdtempSync(join(tmpdir(), 'ms-learn-test-'));
|
||||
let executeTool: (n: string, i: Record<string, unknown>, c: ToolContext) => Promise<ToolResult | null>;
|
||||
let TOOL_DEFS: Record<string, unknown>;
|
||||
|
||||
beforeAll(async () => {
|
||||
vi.spyOn(process, 'cwd').mockReturnValue(tmpRoot);
|
||||
const mod = await import('./ms-learn.js');
|
||||
executeTool = mod.executeTool;
|
||||
TOOL_DEFS = mod.TOOL_DEFS;
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
rmSync(tmpRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
const ctx = {} as ToolContext;
|
||||
|
||||
function htmlResponse(body: string, status = 200): Response {
|
||||
return new Response(body, { status, headers: { 'content-type': 'text/html' } });
|
||||
}
|
||||
|
||||
function jsonResponse(body: unknown, status = 200): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
});
|
||||
}
|
||||
|
||||
const LEARN_HTML = `<!doctype html>
|
||||
<html><head>
|
||||
<title>Durable Functions overview | Microsoft Learn</title>
|
||||
</head><body>
|
||||
<nav>site nav chrome</nav>
|
||||
<main>
|
||||
<article>
|
||||
<h1>Durable Functions overview</h1>
|
||||
<p>Durable Functions is an & extension of Azure Functions.</p>
|
||||
<h2>Patterns</h2>
|
||||
<ul><li>Function chaining</li><li>Fan-out/fan-in</li></ul>
|
||||
<pre><code class="lang-csharp">var x = 1;</code></pre>
|
||||
<p>See <a href="https://learn.microsoft.com/en-us/azure/other">related docs</a>.</p>
|
||||
</article>
|
||||
</main>
|
||||
<footer>footer chrome</footer>
|
||||
</body></html>`;
|
||||
|
||||
describe('TOOL_DEFS', () => {
|
||||
it('registers the four Learn tools', () => {
|
||||
expect(Object.keys(TOOL_DEFS).sort()).toEqual([
|
||||
'FetchMicrosoftLearn',
|
||||
'RefreshMicrosoftLearnCache',
|
||||
'SearchMicrosoftLearn',
|
||||
'SearchMicrosoftLearnCache',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('executeTool', () => {
|
||||
it('returns null for unknown tool names', async () => {
|
||||
expect(await executeTool('NotALearnTool', {}, ctx)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('FetchMicrosoftLearn', () => {
|
||||
it('requires a url', async () => {
|
||||
const res = await executeTool('FetchMicrosoftLearn', {}, ctx);
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('url is required');
|
||||
});
|
||||
|
||||
it('rejects URLs outside learn.microsoft.com', async () => {
|
||||
const fetchMock = vi.fn();
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
const res = await executeTool('FetchMicrosoftLearn', { url: 'https://evil.example/docs' }, ctx);
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('not on learn.microsoft.com');
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('fetches a page, converts to markdown, and caches it', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => htmlResponse(LEARN_HTML)));
|
||||
const res = await executeTool(
|
||||
'FetchMicrosoftLearn',
|
||||
{ url: 'https://learn.microsoft.com/en-us/azure/durable?view=latest#anchor' },
|
||||
ctx,
|
||||
);
|
||||
expect(res?.isError).toBe(false);
|
||||
expect(res?.output).toContain('Fetched and cached');
|
||||
// Title cleaned of the "| Microsoft Learn" suffix.
|
||||
expect(res?.output).toContain('# Durable Functions overview');
|
||||
// Entity decoding, headings, lists, fenced code, links survive.
|
||||
expect(res?.output).toContain('an & extension');
|
||||
expect(res?.output).toContain('## Patterns');
|
||||
expect(res?.output).toContain('- Function chaining');
|
||||
expect(res?.output).toContain('```csharp\nvar x = 1;\n```');
|
||||
expect(res?.output).toContain('[related docs](https://learn.microsoft.com/en-us/azure/other)');
|
||||
// Chrome outside <article> is dropped.
|
||||
expect(res?.output).not.toContain('site nav chrome');
|
||||
expect(res?.output).not.toContain('footer chrome');
|
||||
});
|
||||
|
||||
it('serves the cached copy (query/hash canonicalized away) without refetching', async () => {
|
||||
const fetchMock = vi.fn();
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
const res = await executeTool(
|
||||
'FetchMicrosoftLearn',
|
||||
{ url: 'https://learn.microsoft.com/en-us/azure/durable?other=param' },
|
||||
ctx,
|
||||
);
|
||||
expect(res?.isError).toBe(false);
|
||||
expect(res?.output).toContain('Cached (age=');
|
||||
expect(res?.output).toContain('Durable Functions overview');
|
||||
expect(fetchMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('reports HTTP errors as tool errors', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => htmlResponse('nope', 404)));
|
||||
const res = await executeTool(
|
||||
'FetchMicrosoftLearn',
|
||||
{ url: 'https://learn.microsoft.com/en-us/azure/missing' },
|
||||
ctx,
|
||||
);
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('HTTP 404');
|
||||
});
|
||||
});
|
||||
|
||||
describe('SearchMicrosoftLearnCache', () => {
|
||||
it('requires a query', async () => {
|
||||
const res = await executeTool('SearchMicrosoftLearnCache', {}, ctx);
|
||||
expect(res?.isError).toBe(true);
|
||||
});
|
||||
|
||||
it('finds previously cached pages via FTS', async () => {
|
||||
const res = await executeTool('SearchMicrosoftLearnCache', { query: 'durable functions' }, ctx);
|
||||
expect(res?.isError).toBe(false);
|
||||
expect(res?.output).toContain('Cache hits');
|
||||
expect(res?.output).toContain('https://learn.microsoft.com/en-us/azure/durable');
|
||||
});
|
||||
|
||||
it('reports zero hits without error', async () => {
|
||||
const res = await executeTool('SearchMicrosoftLearnCache', { query: 'zzz-no-such-term' }, ctx);
|
||||
expect(res?.isError).toBe(false);
|
||||
expect(res?.output).toContain('No cache hits');
|
||||
});
|
||||
});
|
||||
|
||||
describe('RefreshMicrosoftLearnCache', () => {
|
||||
it('force-refetches and overwrites the cache', async () => {
|
||||
const updated = LEARN_HTML.replace('an & extension', 'a refreshed extension');
|
||||
vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => htmlResponse(updated)));
|
||||
const res = await executeTool(
|
||||
'RefreshMicrosoftLearnCache',
|
||||
{ url: 'https://learn.microsoft.com/en-us/azure/durable' },
|
||||
ctx,
|
||||
);
|
||||
expect(res?.isError).toBe(false);
|
||||
expect(res?.output).toContain('Refreshed');
|
||||
|
||||
const cached = await executeTool(
|
||||
'FetchMicrosoftLearn',
|
||||
{ url: 'https://learn.microsoft.com/en-us/azure/durable' },
|
||||
ctx,
|
||||
);
|
||||
expect(cached?.output).toContain('a refreshed extension');
|
||||
});
|
||||
|
||||
it('surfaces refresh failures', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('socket hang up')));
|
||||
const res = await executeTool(
|
||||
'RefreshMicrosoftLearnCache',
|
||||
{ url: 'https://learn.microsoft.com/en-us/azure/durable' },
|
||||
ctx,
|
||||
);
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('socket hang up');
|
||||
});
|
||||
});
|
||||
|
||||
describe('SearchMicrosoftLearn', () => {
|
||||
it('requires a query', async () => {
|
||||
const res = await executeTool('SearchMicrosoftLearn', {}, ctx);
|
||||
expect(res?.isError).toBe(true);
|
||||
});
|
||||
|
||||
it('merges online results with cache hits and clamps top to 25', async () => {
|
||||
const fetchMock = vi.fn().mockImplementation(async () =>
|
||||
jsonResponse({
|
||||
results: [
|
||||
{ title: 'Durable Functions overview', url: 'https://learn.microsoft.com/en-us/azure/durable', description: 'desc' },
|
||||
{ title: 'broken', description: 'no url, filtered out' },
|
||||
],
|
||||
}),
|
||||
);
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
const res = await executeTool(
|
||||
'SearchMicrosoftLearn',
|
||||
{ query: 'durable functions', top: 100, products: ['azure'] },
|
||||
ctx,
|
||||
);
|
||||
expect(res?.isError).toBe(false);
|
||||
expect(res?.output).toContain('## Online results (1)');
|
||||
// The cached page is flagged.
|
||||
expect(res?.output).toContain('[cached]');
|
||||
expect(res?.output).toContain('## Cache hits');
|
||||
const calledUrl = String(fetchMock.mock.calls[0]?.[0]);
|
||||
expect(calledUrl).toContain('%24top=25');
|
||||
expect(calledUrl).toContain('products=azure');
|
||||
});
|
||||
|
||||
it('falls back to cache-only results when the online API fails', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => jsonResponse({}, 503)));
|
||||
const res = await executeTool('SearchMicrosoftLearn', { query: 'durable functions' }, ctx);
|
||||
expect(res?.isError).toBe(false);
|
||||
expect(res?.output).toContain('## Cache hits');
|
||||
expect(res?.output).toContain('online search failed');
|
||||
});
|
||||
|
||||
it('errors when online fails and the cache has nothing', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => jsonResponse({}, 503)));
|
||||
const res = await executeTool('SearchMicrosoftLearn', { query: 'zzz-no-such-term' }, ctx);
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('Search failed');
|
||||
});
|
||||
|
||||
it('reports no results without error when both sources are empty', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => jsonResponse({ results: [] })));
|
||||
const res = await executeTool('SearchMicrosoftLearn', { query: 'zzz-no-such-term' }, ctx);
|
||||
expect(res?.isError).toBe(false);
|
||||
expect(res?.output).toContain('No results');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,118 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { mkdtempSync, rmSync, writeFileSync, mkdirSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { tmpdir } from 'os';
|
||||
import type { ToolContext } from './core.js';
|
||||
import { TOOL_DEFS, executeTool } from './orchestration.js';
|
||||
|
||||
let tmp: string;
|
||||
|
||||
beforeEach(() => {
|
||||
tmp = mkdtempSync(join(tmpdir(), 'orchestration-test-'));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const makeCtx = (overrides: Partial<ToolContext> = {}): ToolContext =>
|
||||
({
|
||||
workspacePath: tmp,
|
||||
spawnSubTask: vi.fn().mockResolvedValue({
|
||||
subtaskIndex: 1,
|
||||
jobId: 42,
|
||||
workspacePath: join(tmp, 'subtasks', '1'),
|
||||
}),
|
||||
...overrides,
|
||||
}) as unknown as ToolContext;
|
||||
|
||||
describe('TOOL_DEFS', () => {
|
||||
it('registers SpawnSubTask with title and instruction required', () => {
|
||||
const def = TOOL_DEFS['SpawnSubTask'];
|
||||
expect(def?.function.name).toBe('SpawnSubTask');
|
||||
expect(def?.function.parameters?.['required']).toEqual(['title', 'instruction']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('executeTool', () => {
|
||||
it('returns null for unknown tool names', async () => {
|
||||
expect(await executeTool('NotATool', {}, makeCtx())).toBeNull();
|
||||
});
|
||||
|
||||
it('errors when spawnSubTask is unavailable in this context', async () => {
|
||||
const res = await executeTool(
|
||||
'SpawnSubTask',
|
||||
{ title: 't', instruction: 'i' },
|
||||
makeCtx({ spawnSubTask: undefined }),
|
||||
);
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('使用できません');
|
||||
});
|
||||
|
||||
it.each([
|
||||
[{ instruction: 'i' }],
|
||||
[{ title: 't' }],
|
||||
[{ title: ' ', instruction: 'i' }],
|
||||
[{ title: 't', instruction: '' }],
|
||||
[{ title: 42, instruction: 'i' }],
|
||||
])('rejects missing/blank title or instruction: %j', async input => {
|
||||
const res = await executeTool('SpawnSubTask', input as Record<string, unknown>, makeCtx());
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('必須');
|
||||
});
|
||||
|
||||
it('spawns with the default piece and reports job details', async () => {
|
||||
const ctx = makeCtx();
|
||||
const res = await executeTool('SpawnSubTask', { title: ' t ', instruction: ' do it ' }, ctx);
|
||||
expect(res?.isError).toBe(false);
|
||||
expect(res?.output).toContain('サブタスク #1');
|
||||
expect(res?.output).toContain('ジョブ ID: 42');
|
||||
expect(ctx.spawnSubTask).toHaveBeenCalledWith({ title: 't', instruction: 'do it', piece: 'general' });
|
||||
});
|
||||
|
||||
it('rejects a piece that exists neither builtin nor custom', async () => {
|
||||
const ctx = makeCtx();
|
||||
const res = await executeTool(
|
||||
'SpawnSubTask',
|
||||
{ title: 't', instruction: 'i', piece: 'no-such-piece' },
|
||||
ctx,
|
||||
);
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('no-such-piece');
|
||||
expect(ctx.spawnSubTask).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('accepts a piece found in a custom pieces dir', async () => {
|
||||
const customDir = join(tmp, 'custom-pieces');
|
||||
mkdirSync(customDir, { recursive: true });
|
||||
writeFileSync(join(customDir, 'my-piece.yaml'), 'name: my-piece\n');
|
||||
const ctx = makeCtx({ customPiecesDir: customDir } as Partial<ToolContext>);
|
||||
const res = await executeTool(
|
||||
'SpawnSubTask',
|
||||
{ title: 't', instruction: 'i', piece: 'my-piece' },
|
||||
ctx,
|
||||
);
|
||||
expect(res?.isError).toBe(false);
|
||||
expect(ctx.spawnSubTask).toHaveBeenCalledWith({ title: 't', instruction: 'i', piece: 'my-piece' });
|
||||
});
|
||||
|
||||
it('accepts custom pieces dirs given as an array', async () => {
|
||||
const customDir = join(tmp, 'pieces-a');
|
||||
mkdirSync(customDir, { recursive: true });
|
||||
writeFileSync(join(customDir, 'arr-piece.yaml'), 'name: arr-piece\n');
|
||||
const ctx = makeCtx({ customPiecesDir: [join(tmp, 'empty'), customDir] } as Partial<ToolContext>);
|
||||
const res = await executeTool(
|
||||
'SpawnSubTask',
|
||||
{ title: 't', instruction: 'i', piece: 'arr-piece' },
|
||||
ctx,
|
||||
);
|
||||
expect(res?.isError).toBe(false);
|
||||
});
|
||||
|
||||
it('surfaces spawn failures as tool errors', async () => {
|
||||
const ctx = makeCtx({ spawnSubTask: vi.fn().mockRejectedValue(new Error('queue full')) });
|
||||
const res = await executeTool('SpawnSubTask', { title: 't', instruction: 'i' }, ctx);
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('queue full');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,157 @@
|
||||
import { mkdtempSync, rmSync, writeFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { tmpdir } from 'os';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import type { ToolContext } from './core.js';
|
||||
import { executeTool, TOOL_DEFS } from './speech.js';
|
||||
|
||||
let ws: string;
|
||||
|
||||
beforeEach(() => {
|
||||
ws = mkdtempSync(join(tmpdir(), 'speech-test-'));
|
||||
writeFileSync(join(ws, 'meeting.mp3'), Buffer.from('fake-mp3-bytes'));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(ws, { recursive: true, force: true });
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
const ctx = (toolsConfig: Record<string, unknown> | undefined = { speechServerUrl: 'http://stt.local/v1/' }): ToolContext =>
|
||||
({ workspacePath: ws, toolsConfig }) as unknown as ToolContext;
|
||||
|
||||
function jsonResponse(body: unknown, status = 200): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
});
|
||||
}
|
||||
|
||||
describe('TranscribeAudio', () => {
|
||||
it('returns null for unknown tool names', async () => {
|
||||
expect(await executeTool('NotSpeech', {}, ctx())).toBeNull();
|
||||
});
|
||||
|
||||
it('registers TranscribeAudio with file_path required', () => {
|
||||
expect(TOOL_DEFS['TranscribeAudio']?.function.parameters?.['required']).toEqual(['file_path']);
|
||||
});
|
||||
|
||||
it('requires file_path', async () => {
|
||||
const res = await executeTool('TranscribeAudio', {}, ctx());
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('file_path は必須');
|
||||
});
|
||||
|
||||
it('errors when speech_server_url is not configured', async () => {
|
||||
const res = await executeTool('TranscribeAudio', { file_path: 'meeting.mp3' }, ctx({}));
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('speech_server_url');
|
||||
});
|
||||
|
||||
it('rejects unsupported extensions', async () => {
|
||||
writeFileSync(join(ws, 'video.mp4'), 'x');
|
||||
const res = await executeTool('TranscribeAudio', { file_path: 'video.mp4' }, ctx());
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('対応フォーマット');
|
||||
});
|
||||
|
||||
it('errors for a missing file', async () => {
|
||||
const res = await executeTool('TranscribeAudio', { file_path: 'ghost.wav' }, ctx());
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('ファイルが見つかりません');
|
||||
});
|
||||
|
||||
it('blocks paths escaping the workspace', async () => {
|
||||
await expect(
|
||||
executeTool('TranscribeAudio', { file_path: '../outside.mp3' }, ctx()),
|
||||
).rejects.toThrow();
|
||||
});
|
||||
|
||||
it('posts the file and formats diarized segments grouped by speaker', async () => {
|
||||
const fetchMock = vi.fn().mockImplementation(async () =>
|
||||
jsonResponse({
|
||||
segments: [
|
||||
{ speaker: 'A', text: 'こんにちは。' },
|
||||
{ speaker: 'A', text: '今日の議題です。' },
|
||||
{ speaker: 'B', text: 'はい。' },
|
||||
],
|
||||
}),
|
||||
);
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const res = await executeTool('TranscribeAudio', { file_path: 'meeting.mp3', prompt: '議題' }, ctx());
|
||||
|
||||
expect(res?.isError).toBe(false);
|
||||
expect(res?.output).toContain('## 文字起こし結果: meeting.mp3');
|
||||
expect(res?.output).toContain('[A] こんにちは。今日の議題です。');
|
||||
expect(res?.output).toContain('[B] はい。');
|
||||
|
||||
// URL is normalized (no double slash) and diarization header is sent.
|
||||
expect(String(fetchMock.mock.calls[0]?.[0])).toBe('http://stt.local/v1/audio/transcriptions');
|
||||
const init = fetchMock.mock.calls[0]?.[1] as RequestInit;
|
||||
expect((init.headers as Record<string, string>)['X-Diarize']).toBe('true');
|
||||
const form = init.body as FormData;
|
||||
expect(form.get('language')).toBe('ja');
|
||||
expect(form.get('prompt')).toBe('議題');
|
||||
expect(form.get('response_format')).toBe('verbose_json');
|
||||
});
|
||||
|
||||
it('joins segments without speaker labels when diarize=false', async () => {
|
||||
const fetchMock = vi.fn().mockImplementation(async () =>
|
||||
jsonResponse({ segments: [{ text: 'ひとつ。' }, { text: 'ふたつ。' }] }),
|
||||
);
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
|
||||
const res = await executeTool(
|
||||
'TranscribeAudio',
|
||||
{ file_path: 'meeting.mp3', diarize: false },
|
||||
ctx(),
|
||||
);
|
||||
expect(res?.isError).toBe(false);
|
||||
expect(res?.output).toContain('ひとつ。ふたつ。');
|
||||
expect(res?.output).not.toContain('[Unknown]');
|
||||
const init = fetchMock.mock.calls[0]?.[1] as RequestInit;
|
||||
expect((init.headers as Record<string, string>)['X-Diarize']).toBeUndefined();
|
||||
});
|
||||
|
||||
it('falls back to plain text when no segments are returned', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => jsonResponse({ text: '全文テキスト' })));
|
||||
const res = await executeTool('TranscribeAudio', { file_path: 'meeting.mp3' }, ctx());
|
||||
expect(res?.isError).toBe(false);
|
||||
expect(res?.output).toContain('全文テキスト');
|
||||
});
|
||||
|
||||
it('uses the configured speech language as default', async () => {
|
||||
const fetchMock = vi.fn().mockImplementation(async () => jsonResponse({ text: 'hi' }));
|
||||
vi.stubGlobal('fetch', fetchMock);
|
||||
await executeTool(
|
||||
'TranscribeAudio',
|
||||
{ file_path: 'meeting.mp3' },
|
||||
ctx({ speechServerUrl: 'http://stt.local', speechLanguage: 'en' }),
|
||||
);
|
||||
const form = (fetchMock.mock.calls[0]?.[1] as RequestInit).body as FormData;
|
||||
expect(form.get('language')).toBe('en');
|
||||
});
|
||||
|
||||
it('reports server errors with status code', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => new Response('boom', { status: 500 })));
|
||||
const res = await executeTool('TranscribeAudio', { file_path: 'meeting.mp3' }, ctx());
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('音声認識サーバーエラー (500)');
|
||||
});
|
||||
|
||||
it('reports an empty transcription as an error', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockImplementation(async () => jsonResponse({})));
|
||||
const res = await executeTool('TranscribeAudio', { file_path: 'meeting.mp3' }, ctx());
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('文字起こし結果が空です');
|
||||
});
|
||||
|
||||
it('reports connection failures with the server URL', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('ECONNREFUSED')));
|
||||
const res = await executeTool('TranscribeAudio', { file_path: 'meeting.mp3' }, ctx());
|
||||
expect(res?.isError).toBe(true);
|
||||
expect(res?.output).toContain('音声認識サーバーに接続できません');
|
||||
expect(res?.output).toContain('ECONNREFUSED');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { mkdtempSync, rmSync, readFileSync, existsSync, readdirSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { tmpdir } from 'os';
|
||||
import { saveStructuredBlocks, type StructuredBlock } from './structured-blocks.js';
|
||||
|
||||
let ws: string;
|
||||
|
||||
beforeEach(() => {
|
||||
ws = mkdtempSync(join(tmpdir(), 'structured-blocks-test-'));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(ws, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const block = (refId: string, overrides: Partial<StructuredBlock> = {}): StructuredBlock => ({
|
||||
refId,
|
||||
type: 'x_posts',
|
||||
title: 'sample',
|
||||
data: { query: 'q', posts: [] },
|
||||
...overrides,
|
||||
});
|
||||
|
||||
describe('saveStructuredBlocks', () => {
|
||||
it('writes one JSON file per block under logs/structured', () => {
|
||||
saveStructuredBlocks(ws, [block('ref-1'), block('ref-2', { type: 'map_places' })]);
|
||||
const dir = join(ws, 'logs', 'structured');
|
||||
expect(readdirSync(dir).sort()).toEqual(['ref-1.json', 'ref-2.json']);
|
||||
const parsed = JSON.parse(readFileSync(join(dir, 'ref-1.json'), 'utf-8')) as StructuredBlock;
|
||||
expect(parsed.refId).toBe('ref-1');
|
||||
expect(parsed.type).toBe('x_posts');
|
||||
expect(parsed.data).toEqual({ query: 'q', posts: [] });
|
||||
});
|
||||
|
||||
it('does nothing for an empty block list', () => {
|
||||
saveStructuredBlocks(ws, []);
|
||||
expect(existsSync(join(ws, 'logs', 'structured'))).toBe(false);
|
||||
});
|
||||
|
||||
it('round-trips unicode data intact', () => {
|
||||
saveStructuredBlocks(ws, [block('jp', { title: '日本語タイトル', data: { text: '絵文字🎉' } })]);
|
||||
const parsed = JSON.parse(
|
||||
readFileSync(join(ws, 'logs', 'structured', 'jp.json'), 'utf-8'),
|
||||
) as StructuredBlock;
|
||||
expect(parsed.title).toBe('日本語タイトル');
|
||||
expect(parsed.data).toEqual({ text: '絵文字🎉' });
|
||||
});
|
||||
|
||||
it('swallows write failures instead of throwing', () => {
|
||||
// Point the workspace at a path whose parent is a regular file.
|
||||
const bogus = join(ws, 'not-a-dir');
|
||||
saveStructuredBlocks(ws, [block('pre')]); // creates logs/structured
|
||||
expect(() =>
|
||||
saveStructuredBlocks(join(ws, 'logs', 'structured', 'pre.json'), [block('x')]),
|
||||
).not.toThrow();
|
||||
expect(existsSync(bogus)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,410 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import type { ToolContext } from './core.js';
|
||||
import { executeTool } from './youtube.js';
|
||||
|
||||
const ctx: ToolContext = {
|
||||
workspacePath: '/tmp/maestro-youtube-test',
|
||||
editAllowed: false,
|
||||
};
|
||||
|
||||
const INNERTUBE_PLAYER_PREFIX = 'https://www.youtube.com/youtubei/v1/player';
|
||||
const INNERTUBE_SEARCH_PREFIX = 'https://www.youtube.com/youtubei/v1/search';
|
||||
const WATCH_PAGE_PREFIX = 'https://www.youtube.com/watch?v=';
|
||||
const RESULTS_PAGE_PREFIX = 'https://www.youtube.com/results?search_query=';
|
||||
|
||||
function jsonResponse(obj: unknown, status = 200): Response {
|
||||
return new Response(JSON.stringify(obj), {
|
||||
status,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
}
|
||||
|
||||
function stubFetch(impl: (url: string, init?: RequestInit) => Response | Promise<Response>) {
|
||||
const mock = vi.fn(async (input: unknown, init?: RequestInit) => impl(String(input), init));
|
||||
vi.stubGlobal('fetch', mock);
|
||||
return mock;
|
||||
}
|
||||
|
||||
function makePlayerResponse(overrides: Record<string, unknown> = {}): Record<string, unknown> {
|
||||
return {
|
||||
videoDetails: { title: 'Test Video', lengthSeconds: '125' },
|
||||
captions: {
|
||||
playerCaptionsTracklistRenderer: {
|
||||
captionTracks: [
|
||||
{ languageCode: 'ja', name: { simpleText: '日本語' }, baseUrl: 'https://yt.example/timedtext?lang=ja' },
|
||||
{ languageCode: 'en', name: { simpleText: 'English' }, baseUrl: 'https://yt.example/timedtext?lang=en' },
|
||||
],
|
||||
},
|
||||
},
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
const XML_FORMAT_1 = [
|
||||
'<?xml version="1.0" encoding="utf-8"?><timedtext><body>',
|
||||
'<p t="0" d="1500"><s>Hello </s><s>& world</s></p>',
|
||||
'<p t="61000" d="2000">二行目 あ</p>',
|
||||
'</body></timedtext>',
|
||||
].join('');
|
||||
|
||||
const XML_FORMAT_2 = '<transcript><text start="1.5" dur="2.0">Fallback line</text></transcript>';
|
||||
|
||||
interface SearchVideoFixture {
|
||||
id: string;
|
||||
title: string;
|
||||
channel?: string;
|
||||
views?: string;
|
||||
published?: string;
|
||||
length?: string;
|
||||
desc?: string;
|
||||
}
|
||||
|
||||
function makeSearchData(videos: SearchVideoFixture[]): Record<string, unknown> {
|
||||
return {
|
||||
contents: {
|
||||
twoColumnSearchResultsRenderer: {
|
||||
primaryContents: {
|
||||
sectionListRenderer: {
|
||||
contents: [
|
||||
{
|
||||
itemSectionRenderer: {
|
||||
contents: videos.map((v) => ({
|
||||
videoRenderer: {
|
||||
videoId: v.id,
|
||||
title: { runs: [{ text: v.title }] },
|
||||
ownerText: { runs: [{ text: v.channel ?? 'Chan' }] },
|
||||
...(v.views ? { viewCountText: { simpleText: v.views } } : {}),
|
||||
...(v.published ? { publishedTimeText: { simpleText: v.published } } : {}),
|
||||
...(v.length ? { lengthText: { simpleText: v.length } } : {}),
|
||||
...(v.desc
|
||||
? { detailedMetadataSnippets: [{ snippetText: { runs: [{ text: v.desc }] } }] }
|
||||
: {}),
|
||||
},
|
||||
})),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe('youtube executeTool dispatch', () => {
|
||||
it('returns null for unknown tool names', async () => {
|
||||
const result = await executeTool('NotATool', {}, ctx);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('GetYouTubeTranscript', () => {
|
||||
it('rejects input that contains no extractable video id', async () => {
|
||||
const mock = stubFetch(() => {
|
||||
throw new Error('should not fetch');
|
||||
});
|
||||
const result = await executeTool('GetYouTubeTranscript', { url: 'not a youtube link' }, ctx);
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('YouTube 動画 ID を抽出できません');
|
||||
expect(mock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('fetches transcript via InnerTube and formats timestamped segments', async () => {
|
||||
const mock = stubFetch((url) => {
|
||||
if (url.startsWith(INNERTUBE_PLAYER_PREFIX)) return jsonResponse(makePlayerResponse());
|
||||
if (url.startsWith('https://yt.example/timedtext')) return new Response(XML_FORMAT_1);
|
||||
throw new Error(`unexpected fetch: ${url}`);
|
||||
});
|
||||
|
||||
const result = await executeTool(
|
||||
'GetYouTubeTranscript',
|
||||
{ url: 'https://www.youtube.com/watch?v=abcdefghijk' },
|
||||
ctx,
|
||||
);
|
||||
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('# Test Video');
|
||||
expect(result?.output).toContain('URL: https://www.youtube.com/watch?v=abcdefghijk');
|
||||
expect(result?.output).toContain('動画時間: 2:05');
|
||||
expect(result?.output).toContain('言語: ja');
|
||||
expect(result?.output).toContain('利用可能な言語: ja(日本語), en(English)');
|
||||
expect(result?.output).toContain('字幕セグメント数: 2');
|
||||
// entity decoding + <s> tag concatenation + timestamp formatting
|
||||
expect(result?.output).toContain('[0:00] Hello & world');
|
||||
expect(result?.output).toContain('[1:01] 二行目 あ');
|
||||
|
||||
// InnerTube call carries the extracted video id
|
||||
const body = JSON.parse(String((mock.mock.calls[0][1] as RequestInit).body)) as Record<string, unknown>;
|
||||
expect(body.videoId).toBe('abcdefghijk');
|
||||
});
|
||||
|
||||
it('accepts a bare 11-character video id', async () => {
|
||||
const mock = stubFetch((url) => {
|
||||
if (url.startsWith(INNERTUBE_PLAYER_PREFIX)) return jsonResponse(makePlayerResponse());
|
||||
return new Response(XML_FORMAT_1);
|
||||
});
|
||||
const result = await executeTool('GetYouTubeTranscript', { url: 'dQw4w9WgXcQ' }, ctx);
|
||||
expect(result?.isError).toBe(false);
|
||||
const body = JSON.parse(String((mock.mock.calls[0][1] as RequestInit).body)) as Record<string, unknown>;
|
||||
expect(body.videoId).toBe('dQw4w9WgXcQ');
|
||||
});
|
||||
|
||||
it('accepts youtu.be short URLs', async () => {
|
||||
const mock = stubFetch((url) => {
|
||||
if (url.startsWith(INNERTUBE_PLAYER_PREFIX)) return jsonResponse(makePlayerResponse());
|
||||
return new Response(XML_FORMAT_1);
|
||||
});
|
||||
const result = await executeTool('GetYouTubeTranscript', { url: 'https://youtu.be/abc_def-123' }, ctx);
|
||||
expect(result?.isError).toBe(false);
|
||||
const body = JSON.parse(String((mock.mock.calls[0][1] as RequestInit).body)) as Record<string, unknown>;
|
||||
expect(body.videoId).toBe('abc_def-123');
|
||||
});
|
||||
|
||||
it('selects the requested language track when available', async () => {
|
||||
const mock = stubFetch((url) => {
|
||||
if (url.startsWith(INNERTUBE_PLAYER_PREFIX)) return jsonResponse(makePlayerResponse());
|
||||
if (url === 'https://yt.example/timedtext?lang=en') return new Response(XML_FORMAT_1);
|
||||
throw new Error(`unexpected fetch: ${url}`);
|
||||
});
|
||||
const result = await executeTool(
|
||||
'GetYouTubeTranscript',
|
||||
{ url: 'abcdefghijk', lang: 'en' },
|
||||
ctx,
|
||||
);
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('言語: en');
|
||||
expect(mock).toHaveBeenCalledWith('https://yt.example/timedtext?lang=en', expect.anything());
|
||||
});
|
||||
|
||||
it('errors with the available language list when the requested language is missing', async () => {
|
||||
stubFetch((url) => {
|
||||
if (url.startsWith(INNERTUBE_PLAYER_PREFIX)) return jsonResponse(makePlayerResponse());
|
||||
throw new Error(`unexpected fetch: ${url}`);
|
||||
});
|
||||
const result = await executeTool(
|
||||
'GetYouTubeTranscript',
|
||||
{ url: 'abcdefghijk', lang: 'fr' },
|
||||
ctx,
|
||||
);
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('言語 "fr" の字幕は利用できません');
|
||||
expect(result?.output).toContain('ja(日本語)');
|
||||
expect(result?.output).toContain('en(English)');
|
||||
});
|
||||
|
||||
it('returns an error when the InnerTube API responds non-OK', async () => {
|
||||
stubFetch(() => new Response('', { status: 403, statusText: 'Forbidden' }));
|
||||
const result = await executeTool('GetYouTubeTranscript', { url: 'abcdefghijk' }, ctx);
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('字幕の取得に失敗しました');
|
||||
expect(result?.output).toContain('403');
|
||||
});
|
||||
|
||||
it('returns an error when the transcript XML cannot be parsed', async () => {
|
||||
stubFetch((url) => {
|
||||
if (url.startsWith(INNERTUBE_PLAYER_PREFIX)) return jsonResponse(makePlayerResponse());
|
||||
return new Response('<garbage/>');
|
||||
});
|
||||
const result = await executeTool('GetYouTubeTranscript', { url: 'abcdefghijk' }, ctx);
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('この動画の字幕を解析できませんでした');
|
||||
});
|
||||
|
||||
it('falls back to the watch page when InnerTube returns no caption tracks', async () => {
|
||||
const pagePlayerResponse = {
|
||||
captions: {
|
||||
playerCaptionsTracklistRenderer: {
|
||||
captionTracks: [
|
||||
{ languageCode: 'en', baseUrl: 'https://yt.example/fallback-track' },
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
const html = `<html><script>var ytInitialPlayerResponse = ${JSON.stringify(pagePlayerResponse)};</script></html>`;
|
||||
stubFetch((url) => {
|
||||
if (url.startsWith(INNERTUBE_PLAYER_PREFIX)) {
|
||||
return jsonResponse({ videoDetails: { title: 'Test Video', lengthSeconds: '125' } });
|
||||
}
|
||||
if (url.startsWith(WATCH_PAGE_PREFIX)) return new Response(html);
|
||||
if (url === 'https://yt.example/fallback-track') return new Response(XML_FORMAT_2);
|
||||
throw new Error(`unexpected fetch: ${url}`);
|
||||
});
|
||||
|
||||
const result = await executeTool('GetYouTubeTranscript', { url: 'abcdefghijk' }, ctx);
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('# Test Video');
|
||||
expect(result?.output).toContain('言語: en');
|
||||
expect(result?.output).toContain('字幕セグメント数: 1');
|
||||
// format-2 XML: start="1.5" → 0:01
|
||||
expect(result?.output).toContain('[0:01] Fallback line');
|
||||
});
|
||||
|
||||
it('reports missing captions when the watch page has no ytInitialPlayerResponse', async () => {
|
||||
stubFetch((url) => {
|
||||
if (url.startsWith(INNERTUBE_PLAYER_PREFIX)) return jsonResponse({ videoDetails: { title: 'T' } });
|
||||
if (url.startsWith(WATCH_PAGE_PREFIX)) return new Response('<html><body>no data</body></html>');
|
||||
throw new Error(`unexpected fetch: ${url}`);
|
||||
});
|
||||
const result = await executeTool('GetYouTubeTranscript', { url: 'abcdefghijk' }, ctx);
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('字幕情報が見つかりませんでした');
|
||||
});
|
||||
|
||||
it('reports CAPTCHA when YouTube serves a recaptcha page', async () => {
|
||||
stubFetch((url) => {
|
||||
if (url.startsWith(INNERTUBE_PLAYER_PREFIX)) return jsonResponse({});
|
||||
if (url.startsWith(WATCH_PAGE_PREFIX)) return new Response('<div class="g-recaptcha"></div>');
|
||||
throw new Error(`unexpected fetch: ${url}`);
|
||||
});
|
||||
const result = await executeTool('GetYouTubeTranscript', { url: 'abcdefghijk' }, ctx);
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('CAPTCHA');
|
||||
});
|
||||
|
||||
it('reports no captions when the watch page player data has an empty track list', async () => {
|
||||
const html = `<html><script>var ytInitialPlayerResponse = ${JSON.stringify({ captions: { playerCaptionsTracklistRenderer: { captionTracks: [] } } })};</script></html>`;
|
||||
stubFetch((url) => {
|
||||
if (url.startsWith(INNERTUBE_PLAYER_PREFIX)) return jsonResponse({});
|
||||
if (url.startsWith(WATCH_PAGE_PREFIX)) return new Response(html);
|
||||
throw new Error(`unexpected fetch: ${url}`);
|
||||
});
|
||||
const result = await executeTool('GetYouTubeTranscript', { url: 'abcdefghijk' }, ctx);
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('この動画には字幕がありません');
|
||||
});
|
||||
});
|
||||
|
||||
describe('SearchYouTube', () => {
|
||||
it('formats InnerTube search results and emits structured blocks', async () => {
|
||||
const data = makeSearchData([
|
||||
{
|
||||
id: 'aaaaaaaaaaa',
|
||||
title: 'First Video',
|
||||
channel: 'Chan A',
|
||||
views: '1万 回視聴',
|
||||
published: '1 日前',
|
||||
length: '10:00',
|
||||
desc: 'An interesting clip',
|
||||
},
|
||||
{ id: 'bbbbbbbbbbb', title: 'Second Video', channel: 'Chan B' },
|
||||
]);
|
||||
stubFetch((url) => {
|
||||
if (url.startsWith(INNERTUBE_SEARCH_PREFIX)) return jsonResponse(data);
|
||||
throw new Error(`unexpected fetch: ${url}`);
|
||||
});
|
||||
|
||||
const result = await executeTool('SearchYouTube', { query: 'cats' }, ctx);
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('YouTube 検索結果: "cats" (2件)');
|
||||
expect(result?.output).toContain('1. First Video');
|
||||
expect(result?.output).toContain('URL: https://www.youtube.com/watch?v=aaaaaaaaaaa');
|
||||
expect(result?.output).toContain('チャンネル: Chan A');
|
||||
expect(result?.output).toContain('動画時間: 10:00');
|
||||
expect(result?.output).toContain('再生回数: 1万 回視聴');
|
||||
expect(result?.output).toContain('投稿日: 1 日前');
|
||||
expect(result?.output).toContain('概要: An interesting clip');
|
||||
expect(result?.output).toContain('2. Second Video');
|
||||
expect(result?.output).toMatch(/\[\[embed:youtube-\d+\]\]/);
|
||||
|
||||
expect(result?.structuredBlocks).toHaveLength(1);
|
||||
const block = result!.structuredBlocks![0];
|
||||
expect(block.type).toBe('youtube_videos');
|
||||
const blockData = block.data as { query: string; videos: Array<Record<string, unknown>> };
|
||||
expect(blockData.query).toBe('cats');
|
||||
expect(blockData.videos).toHaveLength(2);
|
||||
expect(blockData.videos[0]).toMatchObject({
|
||||
videoId: 'aaaaaaaaaaa',
|
||||
title: 'First Video',
|
||||
channelName: 'Chan A',
|
||||
thumbnailUrl: 'https://i.ytimg.com/vi/aaaaaaaaaaa/mqdefault.jpg',
|
||||
videoUrl: 'https://www.youtube.com/watch?v=aaaaaaaaaaa',
|
||||
duration: '10:00',
|
||||
});
|
||||
});
|
||||
|
||||
it('respects the limit parameter', async () => {
|
||||
const data = makeSearchData([
|
||||
{ id: 'aaaaaaaaaaa', title: 'One' },
|
||||
{ id: 'bbbbbbbbbbb', title: 'Two' },
|
||||
{ id: 'ccccccccccc', title: 'Three' },
|
||||
]);
|
||||
stubFetch(() => jsonResponse(data));
|
||||
|
||||
const result = await executeTool('SearchYouTube', { query: 'cats', limit: 1 }, ctx);
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('(1件)');
|
||||
expect(result?.output).toContain('1. One');
|
||||
expect(result?.output).not.toContain('Two');
|
||||
const blockData = result!.structuredBlocks![0].data as { videos: unknown[] };
|
||||
expect(blockData.videos).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('returns a non-error message when the response has no result sections', async () => {
|
||||
stubFetch(() => jsonResponse({ contents: {} }));
|
||||
const result = await executeTool('SearchYouTube', { query: 'nothing' }, ctx);
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('"nothing" の検索結果が見つかりませんでした');
|
||||
});
|
||||
|
||||
it('returns a non-error message when sections contain no videoRenderer items', async () => {
|
||||
const data = {
|
||||
contents: {
|
||||
twoColumnSearchResultsRenderer: {
|
||||
primaryContents: {
|
||||
sectionListRenderer: {
|
||||
contents: [{ itemSectionRenderer: { contents: [{ shelfRenderer: {} }] } }],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
stubFetch(() => jsonResponse(data));
|
||||
const result = await executeTool('SearchYouTube', { query: 'nothing' }, ctx);
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('"nothing" の動画検索結果が見つかりませんでした');
|
||||
expect(result?.structuredBlocks).toBeUndefined();
|
||||
});
|
||||
|
||||
it('falls back to HTML scraping when the InnerTube API fails', async () => {
|
||||
const data = makeSearchData([{ id: 'aaaaaaaaaaa', title: 'Scraped Video', channel: 'HTML Chan' }]);
|
||||
const html = `<html><script>var ytInitialData = ${JSON.stringify(data)};</script></html>`;
|
||||
stubFetch((url) => {
|
||||
if (url.startsWith(INNERTUBE_SEARCH_PREFIX)) return new Response('', { status: 500 });
|
||||
if (url.startsWith(RESULTS_PAGE_PREFIX)) return new Response(html);
|
||||
throw new Error(`unexpected fetch: ${url}`);
|
||||
});
|
||||
|
||||
const result = await executeTool('SearchYouTube', { query: 'cats' }, ctx);
|
||||
expect(result?.isError).toBe(false);
|
||||
expect(result?.output).toContain('1. Scraped Video');
|
||||
expect(result?.output).toContain('チャンネル: HTML Chan');
|
||||
// HTML fallback path does not emit structured blocks
|
||||
expect(result?.structuredBlocks).toBeUndefined();
|
||||
});
|
||||
|
||||
it('errors when the fallback HTML has no ytInitialData', async () => {
|
||||
stubFetch((url) => {
|
||||
if (url.startsWith(INNERTUBE_SEARCH_PREFIX)) return new Response('', { status: 500 });
|
||||
return new Response('<html><body>blocked</body></html>');
|
||||
});
|
||||
const result = await executeTool('SearchYouTube', { query: 'cats' }, ctx);
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('YouTube 検索結果の解析に失敗しました');
|
||||
});
|
||||
|
||||
it('errors when both the API and the fallback page request fail', async () => {
|
||||
stubFetch((url) => {
|
||||
if (url.startsWith(INNERTUBE_SEARCH_PREFIX)) return new Response('', { status: 500 });
|
||||
return new Response('', { status: 429 });
|
||||
});
|
||||
const result = await executeTool('SearchYouTube', { query: 'cats' }, ctx);
|
||||
expect(result?.isError).toBe(true);
|
||||
expect(result?.output).toContain('YouTube 検索に失敗しました');
|
||||
expect(result?.output).toContain('429');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user