@mobius-os/mobius 0.3.42 → 0.3.43

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.
@@ -1,114 +0,0 @@
1
- /**
2
- * Integration test — exercises the real Mobius backend end-to-end.
3
- *
4
- * login (passwordless) → getMe → listModels → createProject → createIssue →
5
- * createSession → sendMessage → open SSE → assert assistant entries stream in.
6
- *
7
- * Run: npm run test:integration
8
- * Target: a local Mobius backend by default; set MOBIUS_TUI_SERVER /
9
- * MOBIUS_TUI_USER to point at a specific server.
10
- */
11
- import { login, getMe, MobiusClient, ApiError } from '../src/api.js'
12
- import { SseConnection } from '../src/sse.js'
13
- import { assistantEntryText, isHiddenNoise } from '../src/lib/entry-view.js'
14
- import type { AnyEntry } from '../src/types.js'
15
-
16
- const SERVER = process.env.MOBIUS_TUI_SERVER || 'http://127.0.0.1:45616'
17
- const USERNAME = process.env.MOBIUS_TUI_USER || 'admin'
18
- const WAIT_MS = Number(process.env.MOBIUS_TUI_WAIT_MS || 90000)
19
-
20
- let pass = 0, fail = 0
21
- function ok(cond: boolean, msg: string) {
22
- if (cond) { pass++; console.log(` ✓ ${msg}`) }
23
- else { fail++; console.error(` ✗ ${msg}`) }
24
- }
25
-
26
- async function main() {
27
- console.log(`\n[1/7] login → ${SERVER} as ${USERNAME}`)
28
- const lr = await login(SERVER, USERNAME)
29
- ok(!!lr.token && lr.token.length > 20, `got token (len ${lr.token.length})`)
30
- ok(lr.user.id === USERNAME, `user.id = ${lr.user.id} (${lr.user.display_name})`)
31
-
32
- console.log('\n[2/7] getMe validates token')
33
- const me = await getMe(SERVER, lr.token)
34
- ok(me.id === USERNAME, `getMe ok, role=${me.role}, work_dir=${me.work_dir}`)
35
-
36
- const client = new MobiusClient(SERVER, lr.token)
37
-
38
- console.log('\n[3/7] model options')
39
- const models = await client.modelOptions()
40
- const keys = models.map(m => m.key)
41
- ok(models.length > 0, `${models.length} models available`)
42
- ok(keys.includes('codex') || models.some(m => /codex|claude|deepseek/i.test(m.key)),
43
- `a usable model present (sample: ${keys.slice(0, 5).join(', ')})`)
44
- const modelKey = keys.includes('codex') ? 'codex' : models[0].key
45
-
46
- console.log('\n[4/7] create test project + issue')
47
- const stamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19)
48
- const proj = await client.createProject({ name: `tui-itest-${stamp}`, description: 'TUI integration test (auto)', bindPath: `tui-itest-${stamp}`, defaultUseWorktree: false })
49
- ok(!!proj.id, `project created: ${proj.name} (${proj.id})`)
50
- const issue = await client.createIssue(proj.id, { title: 'auto-test', description: 'integration', use_worktree: false })
51
- ok(!!issue.id, `issue created: ${issue.title} (${issue.id})`)
52
-
53
- console.log('\n[5/7] create session with preferences')
54
- const session = await client.createSession(issue.id, {
55
- name: `tui-itest-${stamp}`, model: modelKey, language: 'zh',
56
- excluded_skill_ids: [], excluded_memory_ids: [],
57
- })
58
- ok(!!session.session_id, `session created (${session.session_id})`)
59
-
60
- console.log('\n[6/7] send message + open SSE, wait for assistant reply')
61
- await client.sendMessage(session.session_id, '请只回复两个字:成功。不要调用任何工具。')
62
-
63
- const entries: AnyEntry[] = []
64
- let gotAssistant = false
65
- let firstEntryMs = 0
66
- const t0 = Date.now()
67
- await new Promise<void>((resolve) => {
68
- const url = `${SERVER}/api/sessions/${encodeURIComponent(session.session_id)}/events?token=${encodeURIComponent(lr.token)}`
69
- const conn = new SseConnection(url, {
70
- onEntry: (entry) => {
71
- if (!firstEntryMs) firstEntryMs = Date.now() - t0
72
- entries.push(entry)
73
- if (!isHiddenNoise(entry) && assistantEntryText(entry)) gotAssistant = true
74
- if (gotAssistant) { setTimeout(resolve, 1500) } // grab trailing entries
75
- },
76
- onError: (m) => console.error(' (sse error)', m),
77
- })
78
- conn.start()
79
- const deadline = setTimeout(() => { conn.close(); resolve() }, WAIT_MS)
80
- // also resolve promptly once we clearly have a finalized assistant msg
81
- const poll = setInterval(() => {
82
- if (gotAssistant && Date.now() - (entries[entries.length - 1]?.__ts ?? 0) > 3000) {
83
- // no-op; rely on the 1.5s resolve above
84
- }
85
- if (Date.now() - t0 > WAIT_MS) { clearInterval(poll); clearTimeout(deadline) }
86
- }, 1000)
87
- })
88
-
89
- ok(entries.length > 0, `received ${entries.length} SSE jsonl_entry events`)
90
- ok(firstEntryMs > 0, `first entry after ${firstEntryMs}ms`)
91
- ok(gotAssistant, 'received an assistant text entry')
92
-
93
- // Show a sample of the transcript the TUI would render.
94
- console.log('\n —— transcript sample ——')
95
- for (const e of entries.slice(0, 8)) {
96
- const t = assistantEntryText(e)
97
- if (t) console.log(' • ' + t.slice(0, 120).replace(/\n/g, '\n '))
98
- }
99
-
100
- console.log('\n[7/7] cleanup')
101
- try { await client.stopSession(session.session_id) } catch { /* ignore */ }
102
- for (const p of [`/api/sessions/${session.session_id}`, `/api/issues/${issue.id}`, `/api/projects/${proj.id}`]) {
103
- try { await fetch(`${SERVER}${p}`, { method: 'DELETE', headers: { Authorization: `Bearer ${lr.token}` } }) } catch { /* ignore */ }
104
- }
105
- console.log(' (best-effort cleanup done)')
106
-
107
- console.log(`\n==== RESULT: ${pass} passed, ${fail} failed ====\n`)
108
- process.exit(fail === 0 ? 0 : 1)
109
- }
110
-
111
- main().catch((e) => {
112
- console.error('\nFATAL:', e instanceof ApiError ? `${e.message} (HTTP ${e.status})` : e)
113
- process.exit(2)
114
- })
package/tests/preview.tsx DELETED
@@ -1,104 +0,0 @@
1
- /**
2
- * 一次性预览: 把典型 jsonl entry 喂进新的 mergeToolCalls → viewsForBlock,
3
- * 按 Chat.tsx 的 ViewLine 格式打印, 直观验证"对齐 web"后的渲染效果.
4
- * 跑: npx tsx tests/preview.tsx (看完可删)
5
- */
6
- import { mergeToolCalls, viewsForBlock, toolLabel } from '../src/lib/entry-view.js'
7
-
8
- // ── 典型 entry 构造 (Claude SDK 形态) ──────────────────────────────────────
9
- const entries: any[] = [
10
- // 1. 用户提问
11
- { type: 'user', uuid: 'u1', message: { content: '帮我把 getData 改成 async 实现' } },
12
-
13
- // 2. assistant 文本回复 (full 完整展示)
14
- { type: 'assistant', uuid: 'a1', message: { content: [{ type: 'text', text: '好的,改成 async/await 实现。\n\n主要改动:\n1. 加 async 关键字\n2. await 替换 .then()\n3. try/catch 错误处理' }] } },
15
-
16
- // 3. Read 命令 + 结果 (compact, 合并成一块, ≤2 行)
17
- { type: 'assistant', uuid: 'a2', message: { content: [{ type: 'tool_use', id: 't1', name: 'Read', input: { file_path: '/src/foo.ts' } }] } },
18
- { type: 'user', uuid: 'u2', message: { content: [{ type: 'tool_result', tool_use_id: 't1', content: 'export function getData() {\n return fetch(url);\n}\n\n// ... 还有 200 行省略' }] } },
19
-
20
- // 4. Edit 代码修改 (full: old−/new+ 完整展示)
21
- { type: 'assistant', uuid: 'a3', message: { content: [{ type: 'tool_use', id: 't2', name: 'Edit', input: { file_path: '/src/foo.ts', old_string: 'export function getData() {\n return fetch(url);\n}', new_string: 'export async function getData() {\n const res = await fetch(url);\n return res.json();\n}' } }] } },
22
- { type: 'user', uuid: 'u3', message: { content: [{ type: 'tool_result', tool_use_id: 't2', content: 'The file /src/foo.ts has been updated.' }] } },
23
-
24
- // 5. Bash 命令 + 长结果 (compact, 合并, ≤2 行)
25
- { type: 'assistant', uuid: 'a4', message: { content: [{ type: 'tool_use', id: 't3', name: 'Bash', input: { command: 'npm run typecheck' } }] } },
26
- { type: 'user', uuid: 'u4', message: { content: [{ type: 'tool_result', tool_use_id: 't3', content: '> @mobius-os/mobius typecheck\n> tsc --noEmit\n\nAll good. 还有非常多非常多的编译输出行全部应该被压成一行省略号...' }] } },
27
-
28
- // 6. reasoning 思考 (compact, ≤2 行)
29
- { type: 'assistant', uuid: 'a5', message: { content: [{ type: 'thinking', thinking: '用户要改异步实现,先读代码理解同步逻辑,再用 async/await 重写。要注意错误处理和返回值结构。这段思考过程非常非常长,必须被 clampLines 硬截断到最多两行,超出部分末尾加省略号。' }] } },
30
-
31
- // 7. 噪声 (应被隐藏, 不显示)
32
- { type: 'event_msg', uuid: 'n1', payload: { type: 'token_count', input_tokens: 1234, output_tokens: 567 } },
33
- { type: 'session_meta', uuid: 'n2', payload: { cwd: '/repo', model: 'gpt-5' } },
34
- { type: 'turn_context', uuid: 'n4', payload: { turn_id: 't0' } },
35
-
36
- // 8. context_compacted (对齐 web: 不再隐藏, 显示成 system 行)
37
- { type: 'event_msg', uuid: 'n3', payload: { type: 'context_compacted' } },
38
-
39
- // 9. error (full 完整展示)
40
- { type: 'event_msg', uuid: 'e1', payload: { type: 'error', message: '模型连接超时,请检查网络后重试' } },
41
- ]
42
-
43
- // ── 模拟 Chat.tsx ViewLine 的格式 (带 ANSI 颜色) ─────────────────────────
44
- const WIDTH = 76
45
- const C = { red: '\x1b[31m', green: '\x1b[32m', cyan: '\x1b[36m', magenta: '\x1b[35m', yellow: '\x1b[33m', dim: '\x1b[2m', bold: '\x1b[1m', reset: '\x1b[0m' }
46
-
47
- function clamp(text: string, width: number, max: number): string[] {
48
- if (!text) return ['']
49
- const paras = text.replace(/\r\n/g, '\n').split('\n')
50
- const wrapped: string[] = []
51
- for (const p of paras) {
52
- if (p === '') { wrapped.push(''); continue }
53
- for (let i = 0; i < p.length; i += width) wrapped.push(p.slice(i, i + width))
54
- }
55
- if (wrapped.length <= max) return wrapped
56
- const t = wrapped.slice(0, max)
57
- const last = t[max - 1]
58
- t[max - 1] = last.length >= width ? last.slice(0, width - 1) + '…' : last + '…'
59
- return t
60
- }
61
-
62
- console.log(`${C.dim}═══ 合并后渲染预览 (width=76, 对齐 web 过滤) ═══${C.reset}\n`)
63
- const blocks = mergeToolCalls(entries)
64
- console.log(`${C.dim}[原始 ${entries.length} 条 entry → 合并后 ${blocks.length} 个 block]${C.reset}\n`)
65
-
66
- for (const block of blocks) {
67
- for (const view of viewsForBlock(block)) {
68
- switch (view.kind) {
69
- case 'skip':
70
- break
71
- case 'user':
72
- console.log(`\n${C.bold}› ${view.text}${C.reset}`)
73
- break
74
- case 'assistant':
75
- console.log(`${C.bold}•${C.reset} ${view.text}`)
76
- break
77
- case 'tool_call': {
78
- const head = clamp(`${toolLabel(view.toolName)} ${view.summary}`.trim(), WIDTH - 2, 1)[0]
79
- console.log(`${C.cyan}• ${head}${C.reset}`)
80
- if (view.result) console.log(`${C.dim} └ ${clamp(view.result.text, WIDTH - 4, 1)[0] || '(无输出)'}${C.reset}`)
81
- break
82
- }
83
- case 'code_edit':
84
- console.log(`${C.magenta}✎ 编辑 ${view.filePath || '(未指定文件)'}${C.reset}`)
85
- for (const l of view.oldString.split('\n')) console.log(` ${C.red}−${C.reset} ${l}`)
86
- for (const l of view.newString.split('\n')) console.log(` ${C.green}+${C.reset} ${l}`)
87
- break
88
- case 'write_file':
89
- console.log(`${C.magenta}✎ 写入 ${view.filePath || '(未指定文件)'}${C.reset}`)
90
- for (const l of view.content.split('\n')) console.log(` ${C.green}+${C.reset} ${l}`)
91
- break
92
- case 'reasoning':
93
- clamp(view.text, WIDTH - 4, 2).forEach((l, i) => console.log(`${C.dim}${C.magenta} ◇ ${l}${C.reset}`))
94
- break
95
- case 'system':
96
- console.log(`${C.dim}${C.yellow} ${clamp(view.text, WIDTH - 2, 2)[0]}${C.reset}`)
97
- break
98
- case 'error':
99
- console.log(`${C.red}⚠ ${view.text}${C.reset}`)
100
- break
101
- }
102
- }
103
- }
104
- console.log(`\n${C.dim}═══ 预览结束 ═══${C.reset}`)
@@ -1,170 +0,0 @@
1
- /**
2
- * Reconnect regression — "TUI 输入经常显示两次" (input often renders twice).
3
- *
4
- * Root cause: the optimistic `pendingUser` placeholder is cleared on the live
5
- * `jsonl_entry` path but NOT when the same message is delivered via a reconnect's
6
- * `jsonl_history` replay. Behind a reverse proxy (nginx idle timeout) the SSE
7
- * stream drops mid-turn and auto-reconnects; the reconnect replays the session
8
- * tail — which now contains the just-sent user message — so it lands in `entries`
9
- * while `pendingUser` is still set, and the user's input is shown twice. If the
10
- * whole turn finished while disconnected, no live entry ever arrives to clear the
11
- * placeholder, so the duplication persists until the next send.
12
- *
13
- * This test drops the SSE stream right after the user sends "good" and has the
14
- * reconnect replay history containing that user entry, then asserts "good" is
15
- * rendered exactly once. Without the fix it renders twice.
16
- *
17
- * Run: npm run test:reconnect
18
- */
19
- import os from 'node:os'
20
- import path from 'node:path'
21
- import fs from 'node:fs'
22
-
23
- const TMP_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'mobius-tui-reconnect-'))
24
- process.env.MOBIUS_TUI_HOME = TMP_HOME
25
-
26
- import React from 'react'
27
- import { render } from 'ink-testing-library'
28
- import { App } from '../src/App.js'
29
-
30
- const delay = (ms: number) => new Promise<void>((r) => setTimeout(r, ms))
31
- const RS: any = (globalThis as any).ReadableStream
32
- const enc = new TextEncoder()
33
- let sseController: any = null
34
-
35
- function json(body: unknown, status = 200) {
36
- return new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } })
37
- }
38
-
39
- let pass = 0, fail = 0
40
- function ok(c: boolean, m: string) { c ? (pass++, console.log(` ✓ ${m}`)) : (fail++, console.error(` ✗ ${m}`)) }
41
-
42
- const PID = 'proj-1', IID = 'issue-1', SID = 'sess-1'
43
- let connectCount = 0
44
- // Entries the next SSE connect should replay as jsonl_history (simulates the
45
- // server's tail snapshot on reconnect containing the just-persisted user turn).
46
- let pendingHistory: any[] = []
47
-
48
- function mockFetch(url: string, init?: RequestInit): Response {
49
- // ── SSE: a fresh stream per connect. Reconnects (connect >= 2) replay history. ─
50
- if (url.includes('/events')) {
51
- const n = ++connectCount
52
- return new Response(new RS({
53
- start(c: any) {
54
- sseController = c
55
- c.enqueue(enc.encode('event: subscribed\ndata: {"event":"subscribed"}\n\n'))
56
- if (n >= 2 && pendingHistory.length) {
57
- const payload = JSON.stringify({ event: 'jsonl_history', entries: pendingHistory, done: true })
58
- c.enqueue(enc.encode(`event: jsonl_history\ndata: ${payload}\n\n`))
59
- }
60
- },
61
- }), { status: 200, headers: { 'content-type': 'text/event-stream' } })
62
- }
63
- const method = init?.method ?? 'GET'
64
- if (url.endsWith('/api/auth/config')) return json({ password_required: false })
65
- if (url.endsWith('/api/auth/me')) return json({ id: 'tester', display_name: 'Test User', role: 'admin', work_dir: '/tmp' })
66
- if (url.endsWith('/api/auth/login')) return json({ token: 'mock-jwt-token', user: { id: 'tester', display_name: 'Test User', role: 'admin' } })
67
- // aimux bridge probe — satisfy it so ensureSession doesn't loop for 8s.
68
- if (url.includes('/aimux_bridge/api/remotes/') && url.includes('/connection')) {
69
- const m = url.match(/remotes\/([^/]+)\/connection/)
70
- return json({ identifier: m ? decodeURIComponent(m[1]) : 'x', event_stream_connected: true })
71
- }
72
- if (url.includes('/sessions') && url.includes('/issues') && method === 'POST') return json({ session_id: SID })
73
- if (url.includes('/sessions') && url.includes('/issues') && method === 'GET') return json([])
74
- // Sending a message: persist it, then DROP the live stream so the only delivery
75
- // path is the reconnect's history replay (the exact scenario that double-rendered).
76
- if (url.endsWith('/messages') && method === 'POST') {
77
- pendingHistory = [{ type: 'user', uuid: 'user-good-1', message: { role: 'user', content: 'good' } }]
78
- setTimeout(() => { try { sseController?.close() } catch { /* ignore */ } }, 150)
79
- return json({ ok: true, session_id: SID, turn_number: 1 })
80
- }
81
- if (url.endsWith(`/api/sessions/${SID}/status`)) return json({ session_id: SID, alive: true, working: false })
82
- if (url.includes('/api/projects/') && url.includes('/issues') && method === 'POST') return json({ id: IID, project_id: PID, title: '命令行任务' })
83
- if (url.includes('/api/projects/') && url.includes('/issues') && method === 'GET') return json([])
84
- if (url.includes('/api/projects') && method === 'GET') return json([{ id: PID, name: '已有项目甲' }])
85
- if (url.endsWith('/api/projects') && method === 'POST') return json({ id: PID, name: '测试项目PTY' })
86
- if (url.includes('/sessions/model-options')) return json([{ key: 'codex', label: 'GPT-5.5', title: 'GPT-5.5', sub: 'Codex', backend: 'tmux-codex' }])
87
- if (url.includes('/sessions/default-model')) return json({ model: 'codex' })
88
- if (url.includes('/skills')) return json([])
89
- if (url.includes('/memories')) return json([])
90
- return json({ error: `unmocked ${method} ${url}` }, 404)
91
- }
92
-
93
- async function waitFor(lastFrame: () => string | undefined, needle: string, timeoutMs = 6000) {
94
- for (let i = 0; i < timeoutMs / 50; i++) {
95
- if ((lastFrame() ?? '').includes(needle)) return true
96
- await delay(50)
97
- }
98
- return false
99
- }
100
-
101
- /** Count non-overlapping occurrences of needle in s (after stripping ANSI codes). */
102
- function countOccur(s: string, needle: string): number {
103
- const clean = s.replace(/\x1b\[[0-9;?]*[a-zA-Z]/g, '')
104
- let n = 0, i = 0
105
- while ((i = clean.indexOf(needle, i)) !== -1) { n++; i += needle.length }
106
- return n
107
- }
108
-
109
- async function main() {
110
- fs.writeFileSync(path.join(TMP_HOME, 'login.json'), JSON.stringify({
111
- server: 'http://mock.local', username: 'tester', token: 'mock-jwt-token',
112
- user: { id: 'tester', display_name: 'Test User', role: 'admin' },
113
- }))
114
- const realFetch = globalThis.fetch
115
- globalThis.fetch = ((u: any, init?: any) => mockFetch(String(u), init)) as unknown as typeof fetch
116
-
117
- console.log('\n[RECONNECT] input-not-duplicated regression (mocked backend)\n')
118
- const { stdin, lastFrame, unmount } = render(React.createElement(App))
119
-
120
- try {
121
- // ── boot through the prep wizard into chat (same drive as flow.test) ─────────
122
- ok(await waitFor(lastFrame, '选择当前路径的绑定项目'), 'booted into project picker')
123
- stdin.write('\r'); await delay(120)
124
- ok(await waitFor(lastFrame, '项目名称'), 'project create wizard opened')
125
- stdin.write('测试项目PTY'); await delay(120)
126
- stdin.write('\r'); await delay(300)
127
- ok(await waitFor(lastFrame, '创建新任务'), 'issue picker shown')
128
- stdin.write('\r'); await delay(120)
129
- ok(await waitFor(lastFrame, '输入任务名称'), 'issue name wizard opened')
130
- stdin.write('命令行任务'); await delay(120)
131
- stdin.write('\r'); await delay(300)
132
- ok(await waitFor(lastFrame, '选择模型'), 'model picker shown')
133
- stdin.write('\r'); await delay(250)
134
- ok(await waitFor(lastFrame, '选择回复语言'), 'language picker shown')
135
- stdin.write('\r'); await delay(400)
136
- ok(await waitFor(lastFrame, '输入问题'), 'entered chat')
137
-
138
- // ── the bug: send "good", SSE drops, reconnect replays it via history ───────
139
- connectCount = 0 // reset so the post-send reconnect is "connect 2"
140
- stdin.write('good'); await delay(120)
141
- stdin.write('\r')
142
- // The user's message shows instantly via the optimistic placeholder, so do NOT
143
- // gate on "good" appearing — gate on the reconnect actually firing (connect 2),
144
- // which is what replays history and (without the fix) double-renders the input.
145
- let reconnected = false
146
- for (let i = 0; i < 8000 / 50; i++) {
147
- if (connectCount >= 2) { reconnected = true; break }
148
- await delay(50)
149
- }
150
- ok(reconnected, 'SSE dropped and reconnected after the send')
151
- await delay(500) // let the reconnect's history replay render
152
-
153
- const frame = lastFrame() ?? ''
154
- const occurrences = countOccur(frame, 'good')
155
- console.log(` i occurrences of "good" in frame: ${occurrences}`)
156
- ok(occurrences === 1, `user input rendered exactly once (got ${occurrences}; would be 2 without the fix)`)
157
- if (occurrences !== 1) {
158
- console.log('── frame ──\n' + frame.replace(/\x1b\[[0-9;?]*[a-zA-Z]/g, '').replace(/\n{3,}/g, '\n\n').trim())
159
- }
160
- } finally {
161
- unmount()
162
- globalThis.fetch = realFetch
163
- }
164
-
165
- try { fs.rmSync(TMP_HOME, { recursive: true, force: true }) } catch { /* ignore */ }
166
- console.log(`\n==== RECONNECT RESULT: ${pass} passed, ${fail} failed ====\n`)
167
- process.exit(fail === 0 ? 0 : 1)
168
- }
169
-
170
- main().catch((e) => { console.error('FATAL', e); process.exit(2) })
@@ -1,73 +0,0 @@
1
- /**
2
- * Regression test: when ~/.mobius already has a bound project + an issue whose
3
- * preferences are all configured, the app must boot STRAIGHT into chat (not get
4
- * stuck on a bare prep header, and not need to re-run the wizard).
5
- *
6
- * Run: npm run test:resume
7
- */
8
- import os from 'node:os'
9
- import path from 'node:path'
10
- import fs from 'node:fs'
11
-
12
- const TMP_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'mobius-tui-resume-'))
13
- process.env.MOBIUS_TUI_HOME = TMP_HOME
14
-
15
- import React from 'react'
16
- import { render } from 'ink-testing-library'
17
- import { App } from '../src/App.js'
18
-
19
- const delay = (ms: number) => new Promise<void>((r) => setTimeout(r, ms))
20
- const CWD = process.cwd()
21
- const PID = 'proj-r', IID = 'issue-r'
22
- let pass = 0, fail = 0
23
- function ok(c: boolean, m: string) { c ? (pass++, console.log(` ✓ ${m}`)) : (fail++, console.error(` ✗ ${m}`)) }
24
-
25
- function json(body: unknown, status = 200) {
26
- return new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } })
27
- }
28
- function mockFetch(url: string, init?: RequestInit): Response {
29
- const method = init?.method ?? 'GET'
30
- if (url.endsWith('/api/auth/me')) return json({ id: 'tester', display_name: 'x', role: 'admin' })
31
- if (url.includes('/api/projects') && method === 'GET' && !url.includes('issues')) return json([{ id: PID, name: '绑定项目' }])
32
- if (url.includes('/issues') && method === 'GET') return json([{ id: IID, project_id: PID, title: '已配置任务' }])
33
- if (url.includes('/sessions/model-options')) return json([{ key: 'codex', label: 'GPT', title: 'GPT', sub: '', backend: 'x' }])
34
- return json({ error: `unmocked ${method} ${url}` }, 404)
35
- }
36
-
37
- async function main() {
38
- // Pre-seed: token + cwd→project binding + issue with all prefs done.
39
- fs.writeFileSync(path.join(TMP_HOME, 'login.json'), JSON.stringify({
40
- server: 'http://mock.local', username: 'tester', token: 'tok', user: { id: 'tester', display_name: 'x', role: 'admin' },
41
- }))
42
- fs.writeFileSync(path.join(TMP_HOME, 'dir2project.json'), JSON.stringify({ [CWD]: PID }))
43
- fs.writeFileSync(path.join(TMP_HOME, 'dir2project_preference.json'), JSON.stringify({
44
- [CWD]: { issueId: IID, issueTitle: '已配置任务', prefs: { [IID]: {
45
- model: 'codex', language: 'zh', excluded_skill_ids: [], excluded_memory_ids: [],
46
- done: ['model', 'language', 'skills', 'memories'],
47
- } } },
48
- }))
49
-
50
- const realFetch = globalThis.fetch
51
- globalThis.fetch = ((u: any, init?: any) => mockFetch(String(u), init)) as unknown as typeof fetch
52
-
53
- console.log('\n[RESUME] boot with saved prefs → straight to chat\n')
54
- let reachedChat = false
55
- const { lastFrame, unmount } = render(React.createElement(App))
56
- for (let i = 0; i < 80; i++) { // up to ~4s
57
- if ((lastFrame() ?? '').includes('输入问题')) { reachedChat = true; break }
58
- await delay(50)
59
- }
60
- const frame = lastFrame() ?? ''
61
- unmount()
62
- globalThis.fetch = realFetch
63
-
64
- ok(reachedChat, 'booted straight into chat (skipped wizard)')
65
- ok(frame.includes('绑定项目') && frame.includes('已配置任务'), 'chat header shows the saved project + issue')
66
- ok(!frame.includes('选择当前路径的绑定项目') && !frame.includes('选择模型'), 'did NOT show project/model picker')
67
-
68
- try { fs.rmSync(TMP_HOME, { recursive: true, force: true }) } catch { /* ignore */ }
69
- console.log(`\n==== RESUME RESULT: ${pass} passed, ${fail} failed ====\n`)
70
- process.exit(fail === 0 ? 0 : 1)
71
- }
72
-
73
- main().catch((e) => { console.error('FATAL', e); process.exit(2) })
@@ -1,118 +0,0 @@
1
- /**
2
- * Screen / picker residue regression — "选择项目/Issue/会话时屏幕不残留其他东西".
3
- *
4
- * Root cause: Ink renders inline and, per frame, erases only as many lines as the
5
- * previous frame occupied. A frame taller than the terminal window scrolls, so
6
- * Ink can no longer reach the real top of that frame to erase it — stale lines
7
- * from the previous screen stay behind as residue (worst when a tall list picker
8
- * gives way to a shorter screen).
9
- *
10
- * The fix has two halves, both asserted here:
11
- * 1. <Screen> pins every route to exactly the terminal height (overflow:hidden)
12
- * so no frame ever scrolls → no residue on any transition.
13
- * 2. Select reserves enough chrome rows that a long list window never exceeds
14
- * the terminal, so nothing is clipped/garbled at the bottom.
15
- *
16
- * Run: npm run test:screen
17
- */
18
- import React from 'react'
19
- import { Box, Text } from 'ink'
20
- import { render } from 'ink-testing-library'
21
- import { Screen } from '../src/components/Screen.js'
22
- import { Select } from '../src/components/primitives.js'
23
- import { entryScreenRows } from '../src/lib/screen-text.js'
24
- import { viewsForEntry } from '../src/lib/entry-view.js'
25
- import { createRowAccess, sliceViewport, tailAnchor } from '../src/lib/transcript-viewport.js'
26
-
27
- const delay = (ms: number) => new Promise<void>(r => setTimeout(r, ms))
28
- const strip = (s: string) => s.replace(/\x1b\[[0-9;?]*[a-zA-Z]/g, '')
29
- const lineCount = (s: string) => s.split('\n').length
30
-
31
- let pass = 0, fail = 0
32
- function ok(c: boolean, m: string) { c ? (pass++, console.log(` ✓ ${m}`)) : (fail++, console.error(` ✗ ${m}`)) }
33
-
34
- // ink-testing-library's stdout reports no `rows`, so Screen falls back to 24.
35
- const ROWS = 24
36
-
37
- async function main() {
38
- console.log('\n[SCREEN] no-residue picker transitions\n')
39
-
40
- // A partial message is now represented by the same styled ScreenRow as a
41
- // complete message. Virtual slicing must preserve its ANSI foreground bytes.
42
- const styledEntries = [
43
- { type: 'assistant', uuid: 'styled-old', message: { role: 'assistant', content: [{ type: 'text', text: '[彩色链接](https://example.com)' }] } },
44
- { type: 'assistant', uuid: 'styled-new', message: { role: 'assistant', content: [{ type: 'text', text: '最新消息' }] } },
45
- ]
46
- const rowAccess = createRowAccess(styledEntries, entry => entry.uuid, entry => entryScreenRows(viewsForEntry(entry), 80))
47
- const viewport = sliceViewport(rowAccess, tailAnchor(rowAccess, 3), 3)
48
- const partial = viewport.rows.find(item => item.entryId === 'styled-old')?.row
49
- ok(partial !== undefined, 'small viewport exposes a row from the partial older message')
50
- ok(!!partial?.styled.includes('\x1b[') && partial.styled.includes('彩色链接'), 'partial older row keeps its ANSI foreground styling')
51
-
52
- // ── 1. Without Screen, a tall frame overflows the terminal (the bug). ───────
53
- const tall = render(
54
- <Box flexDirection="column">
55
- {Array.from({ length: 40 }, (_, i) => <Text key={i}>item {i}</Text>)}
56
- </Box>,
57
- )
58
- await delay(30)
59
- const tallLines = lineCount(tall.lastFrame() ?? '')
60
- ok(tallLines > ROWS, `uncapped tall frame overflows terminal (rendered ${tallLines} > ${ROWS})`)
61
-
62
- // ── 2. Screen caps the same tall content to exactly the terminal height. ───
63
- const capped = render(
64
- <Screen>
65
- <Box flexDirection="column">
66
- {Array.from({ length: 40 }, (_, i) => <Text key={i}>item {i}</Text>)}
67
- </Box>
68
- </Screen>,
69
- )
70
- await delay(30)
71
- const capFrame = capped.lastFrame() ?? ''
72
- ok(lineCount(capFrame) === ROWS, `Screen caps frame to terminal height (${lineCount(capFrame)} === ${ROWS})`)
73
-
74
- // ── 3. Realistic picker: AIMUX line + header + a 40-item Select + footer,
75
- // wrapped in Screen, must fit within the terminal with the footer
76
- // visible and the list windowed (overflow items hidden, not spilled). ─
77
- const items = Array.from({ length: 40 }, (_, i) => ({ label: `项目 ${i}`, value: `p${i}`, desc: `desc ${i}` }))
78
- const picker = render(
79
- <Screen>
80
- <Box flexDirection="column" paddingX={2} paddingY={1}>
81
- <Text dimColor>AIMUX · 已连接</Text>
82
- <Text bold color="cyan">选择当前路径的绑定项目</Text>
83
- <Text color="gray">/some/path</Text>
84
- <Box marginTop={1}>
85
- <Select items={items} />
86
- </Box>
87
- <Text color="gray">↑↓ 选择 · 回车确认 · Esc 退出</Text>
88
- </Box>
89
- </Screen>,
90
- )
91
- await delay(30)
92
- const pf = strip(picker.lastFrame() ?? '')
93
- ok(lineCount(picker.lastFrame() ?? '') === ROWS, `picker frame pinned to terminal height (${lineCount(picker.lastFrame() ?? '')} === ${ROWS})`)
94
- ok(pf.includes('Esc 退出'), 'picker footer visible (list did not push it off / clip it)')
95
- ok(!pf.includes('项目 39'), 'list is windowed — tail item not spilled onto screen')
96
- ok(/还有 \d+ 项/.test(pf), 'windowed overflow shows a "还有 N 项" hint')
97
-
98
- // ── 4. Transition tall-picker → short screen leaves no residue: the new frame
99
- // is still exactly terminal height (constant → Ink erase realigns) and
100
- // contains none of the old picker's lines. ─────────────────────────────
101
- picker.rerender(
102
- <Screen>
103
- <Box paddingX={2} paddingY={1}>
104
- <Text color="green">准备就绪,进入对话…</Text>
105
- </Box>
106
- </Screen>,
107
- )
108
- await delay(30)
109
- const after = strip(picker.lastFrame() ?? '')
110
- ok(lineCount(picker.lastFrame() ?? '') === ROWS, `post-transition frame still terminal height (${lineCount(picker.lastFrame() ?? '')} === ${ROWS})`)
111
- ok(!after.includes('选择当前路径'), 'previous picker heading gone after transition (no residue)')
112
- ok(after.includes('准备就绪'), 'new screen content rendered')
113
-
114
- console.log(`\n==== SCREEN RESULT: ${pass} passed, ${fail} failed ====\n`)
115
- process.exit(fail === 0 ? 0 : 1)
116
- }
117
-
118
- main().catch(e => { console.error('FATAL', e); process.exit(2) })