@trans-ai/trans-code 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,89 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import { submitSearchApiKey } from './search-setup.js';
3
+ import type { WebSearchCheck, WebSearchConfig } from '../provider/web-search.js';
4
+
5
+ interface Captured {
6
+ checked?: WebSearchConfig;
7
+ saved?: { provider: string; apiKey: string };
8
+ outcomes: string[];
9
+ notices: string[];
10
+ stale: boolean;
11
+ }
12
+
13
+ function makeDeps(overrides?: {
14
+ checkResult?: WebSearchCheck;
15
+ saveThrows?: boolean;
16
+ saved?: WebSearchConfig | null;
17
+ }) {
18
+ const cap: Captured = { outcomes: [], notices: [], stale: false };
19
+ return {
20
+ cap,
21
+ deps: {
22
+ savedConfig: () => overrides?.saved ?? null,
23
+ check: async (cfg: WebSearchConfig) => {
24
+ cap.checked = cfg;
25
+ return overrides?.checkResult ?? { ok: true, firstTitle: 'Alive' };
26
+ },
27
+ save: (provider: string, apiKey: string) => {
28
+ if (overrides?.saveThrows) throw new Error('EACCES: disk full');
29
+ cap.saved = { provider, apiKey };
30
+ },
31
+ notify: (output: string) => cap.notices.push(output),
32
+ onOutcome: (phase: 'idle' | 'search_apikey') => cap.outcomes.push(phase),
33
+ isStale: () => cap.stale,
34
+ },
35
+ };
36
+ }
37
+
38
+ describe('submitSearchApiKey', () => {
39
+ test('空输入 + 已存同商 key → 回车沿用 saved key 发起测试', async () => {
40
+ const { cap, deps } = makeDeps({ saved: { provider: 'tavily', apiKey: 'tvly-old' } });
41
+ await submitSearchApiKey('', 'tavily', deps);
42
+ expect(cap.checked).toEqual({ provider: 'tavily', apiKey: 'tvly-old' });
43
+ });
44
+
45
+ test('空输入 + 无已存 → 错误提示,不发起测试,phase 不动', async () => {
46
+ const { cap, deps } = makeDeps();
47
+ await submitSearchApiKey(' ', 'brave', deps);
48
+ expect(cap.checked).toBeUndefined();
49
+ expect(cap.notices[0]).toContain('不能为空');
50
+ expect(cap.outcomes).toEqual([]);
51
+ });
52
+
53
+ test('成功 → 写配置 + 成功提示 + 回 idle', async () => {
54
+ const { cap, deps } = makeDeps();
55
+ await submitSearchApiKey('tvly-new', 'tavily', deps);
56
+ expect(cap.saved).toEqual({ provider: 'tavily', apiKey: 'tvly-new' });
57
+ expect(cap.notices.join('\n')).toContain('已启用');
58
+ expect(cap.outcomes).toEqual(['idle']);
59
+ });
60
+
61
+ test('测试失败 → 不写配置 + 错误提示 + 回 search_apikey 可重输', async () => {
62
+ const { cap, deps } = makeDeps({ checkResult: { ok: false, error: 'brave HTTP 401: bad key' } });
63
+ await submitSearchApiKey('k', 'brave', deps);
64
+ expect(cap.saved).toBeUndefined();
65
+ expect(cap.notices.join('\n')).toContain('401');
66
+ expect(cap.outcomes).toEqual(['search_apikey']);
67
+ });
68
+
69
+ test('Esc 后代次失效(isStale)→ 不写配置、不动 phase、不发迟到的成功消息', async () => {
70
+ const { cap, deps } = makeDeps();
71
+ // Esc fires while the check is in flight: mark stale before completion.
72
+ deps.check = async (cfg: WebSearchConfig) => {
73
+ cap.checked = cfg;
74
+ cap.stale = true;
75
+ return { ok: true, firstTitle: 'late' };
76
+ };
77
+ await submitSearchApiKey('k', 'tavily', deps);
78
+ expect(cap.saved).toBeUndefined();
79
+ expect(cap.outcomes).toEqual([]);
80
+ expect(cap.notices.join('\n')).not.toContain('已启用');
81
+ });
82
+
83
+ test('saveConfig 抛异常 → 错误提示 + 回 search_apikey,不向上抛', async () => {
84
+ const { cap, deps } = makeDeps({ saveThrows: true });
85
+ await submitSearchApiKey('k', 'tavily', deps);
86
+ expect(cap.notices.join('\n')).toContain('EACCES');
87
+ expect(cap.outcomes).toEqual(['search_apikey']);
88
+ });
89
+ });
@@ -0,0 +1,57 @@
1
+ import type { WebSearchCheck, WebSearchConfig } from '../provider/web-search.js';
2
+
3
+ /**
4
+ * /search key-step orchestration, extracted so the flow is testable without
5
+ * the REPL: reuse a saved key on empty input, run a live check, persist only
6
+ * on success, and abandon the outcome entirely when the run went stale
7
+ * (the user pressed Esc while the check was in flight).
8
+ */
9
+
10
+ export interface SearchSetupDeps {
11
+ /** Current persisted config (for the "Enter keeps the saved key" path). */
12
+ savedConfig: () => WebSearchConfig | null;
13
+ /** Live connectivity check (never throws). */
14
+ check: (cfg: WebSearchConfig) => Promise<WebSearchCheck>;
15
+ /** Persist provider+key. */
16
+ save: (provider: WebSearchConfig['provider'], apiKey: string) => void;
17
+ /** Append a message to the chat history. */
18
+ notify: (output: string) => void;
19
+ /** Phase transition after the outcome: idle on success, key prompt on failure. */
20
+ onOutcome: (phase: 'idle' | 'search_apikey') => void;
21
+ /** True when this run was cancelled (Esc) before the check returned. */
22
+ isStale: () => boolean;
23
+ }
24
+
25
+ export async function submitSearchApiKey(
26
+ value: string,
27
+ provider: WebSearchConfig['provider'],
28
+ deps: SearchSetupDeps,
29
+ ): Promise<void> {
30
+ const saved = deps.savedConfig();
31
+ const apiKey = value.trim() || (saved?.provider === provider ? saved.apiKey : '');
32
+ if (!apiKey) {
33
+ deps.notify('API key 不能为空。(Esc 取消)');
34
+ return;
35
+ }
36
+
37
+ deps.notify(`正在测试 ${provider} key…`);
38
+ const result = await deps.check({ provider, apiKey });
39
+ // Esc during the check: write nothing, touch no phase, show no late result.
40
+ if (deps.isStale()) return;
41
+
42
+ if (result.ok) {
43
+ try {
44
+ deps.save(provider, apiKey);
45
+ } catch (error) {
46
+ deps.notify(`✗ 配置写入失败:${error instanceof Error ? error.message : String(error)}\n请重试或手改 ~/.transcode.json`);
47
+ deps.onOutcome('search_apikey');
48
+ return;
49
+ }
50
+ deps.notify(`✓ ${provider} 连通正常(首条结果:${result.firstTitle})。web_search 已启用。`);
51
+ deps.onOutcome('idle');
52
+ return;
53
+ }
54
+
55
+ deps.notify(`✗ 测试失败:${result.error}\n请重新输入 key(Esc 取消)`);
56
+ deps.onOutcome('search_apikey');
57
+ }
@@ -21,3 +21,11 @@ describe('resolveSubmitInput', () => {
21
21
  expect(resolveSubmitInput('idle', 'hello')).toBe('hello');
22
22
  });
23
23
  });
24
+
25
+ describe('search_apikey empty submit', () => {
26
+ test('empty Enter at the search_apikey prompt submits (reuses the saved key)', () => {
27
+ // The /search prompt advertises "(Enter 沿用已存 key)" — a bare Enter
28
+ // must reach the search key handler, which falls back to the saved key.
29
+ expect(resolveSubmitInput('search_apikey', '')).toBe('');
30
+ });
31
+ });
@@ -3,7 +3,7 @@
3
3
  * prompt the hint reads "apikey (回车沿用 sk-…xxxx)> " — a bare Enter means
4
4
  * "reuse the saved key", and handleApiKey falls back to it for empty input.
5
5
  */
6
- const EMPTY_SUBMIT_PHASES = new Set(['model_apikey']);
6
+ const EMPTY_SUBMIT_PHASES = new Set(['model_apikey', 'search_apikey']);
7
7
 
8
8
  /**
9
9
  * Decide whether a submitted line should be dispatched. Returns the trimmed
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // Auto-generated from package.json by scripts/sync-version.ts (npm version hook)
2
- export const VERSION = '0.9.0';
2
+ export const VERSION = '0.10.0';