@allbluecn/web-app 0.28.0 → 0.28.1
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.
- package/package.json +4 -3
- package/src/components/harness/__tests__/dock-registry.test.ts +43 -0
- package/src/components/harness/conversation-panel/messages/harness-chain/__tests__/use-harness-chat-adapter.test.ts +20 -0
- package/src/components/harness/conversation-panel/messages/harness-chain/use-agent-terminals.ts +116 -0
- package/src/components/harness/conversation-panel/messages/harness-chain/use-harness-chat-adapter.ts +17 -5
- package/src/components/harness/conversation-panel/messages/harness-chain/use-harness-session.ts +4 -1
- package/src/components/harness/conversation-panel/messages/tools/__tests__/tool-run.test.ts +26 -1
- package/src/components/harness/conversation-panel/messages/tools/tool-run.ts +11 -0
- package/src/components/harness/extension-panel/dock/__tests__/tab-strip.test.tsx +45 -0
- package/src/components/harness/extension-panel/dock/dock-registry.ts +34 -0
- package/src/components/harness/extension-panel/dock/dock-surface.tsx +33 -0
- package/src/components/harness/extension-panel/dock/guide-page.tsx +4 -1
- package/src/components/harness/extension-panel/dock/tab-strip.tsx +9 -1
- package/src/components/harness/extension-panel/panels/terminal/__tests__/kind.test.tsx +219 -2
- package/src/components/harness/extension-panel/panels/terminal/__tests__/terminal-instance.test.tsx +322 -19
- package/src/components/harness/extension-panel/panels/terminal/__tests__/use-agent-terminal-roster.test.tsx +393 -0
- package/src/components/harness/extension-panel/panels/terminal/__tests__/ws-transport.test.ts +25 -0
- package/src/components/harness/extension-panel/panels/terminal/kind.tsx +55 -1
- package/src/components/harness/extension-panel/panels/terminal/shell-pref.ts +7 -4
- package/src/components/harness/extension-panel/panels/terminal/sse-transport.ts +26 -6
- package/src/components/harness/extension-panel/panels/terminal/tab-control-state.tsx +114 -0
- package/src/components/harness/extension-panel/panels/terminal/terminal-instance.tsx +190 -96
- package/src/components/harness/extension-panel/panels/terminal/terminal-theme.ts +113 -0
- package/src/components/harness/extension-panel/panels/terminal/transport.ts +28 -6
- package/src/components/harness/extension-panel/panels/terminal/use-agent-terminal-roster.ts +107 -0
- package/src/components/harness/extension-panel/panels/terminal/ws-transport.ts +7 -5
- package/src/components/harness/extension-panel/panels/terminal/xterm-terminal.tsx +5 -67
- package/src/lib/changelog/sdk-versions.ts +2 -2
- package/src/lib/queries/harness-terminal.ts +77 -0
- package/src/paraglide/messages/_index.js +48 -0
- package/src/paraglide/messages/en.js +15 -0
- package/src/paraglide/messages/zh.js +15 -0
- package/src/routes/api/harness/$.ts +19 -2
- package/src/routes/api/harness/terminal/$.ts +37 -6
- package/src/routes/api/harness/terminal/__tests__/route.test.ts +120 -0
- package/src/server/harness/__tests__/agent-terminal.server.test.ts +448 -0
- package/src/server/harness/__tests__/builtin-tools-invariant.test.ts +22 -12
- package/src/server/harness/__tests__/first-contact-fakes.test.ts +103 -3
- package/src/server/harness/__tests__/terminal-manager.server.test.ts +761 -2
- package/src/server/harness/agent-terminal.server.ts +631 -0
- package/src/server/harness/first-contact-fakes.ts +115 -4
- package/src/server/harness/host.server.ts +28 -1
- package/src/server/harness/persistence/__tests__/session-index-store.server.test.ts +83 -0
- package/src/server/harness/persistence/prisma-persistence.server.ts +12 -3
- package/src/server/harness/persistence/session-index-store.server.ts +26 -0
- package/src/server/harness/plugins/__tests__/assemble-disable.test.ts +25 -17
- package/src/server/harness/plugins/__tests__/tool-risk-audit.test.ts +16 -6
- package/src/server/harness/plugins/assemble-app.ts +12 -0
- package/src/server/harness/plugins/base-patch.ts +9 -2
- package/src/server/harness/plugins/builtin/__tests__/agent-terminal.plugin.test.ts +702 -0
- package/src/server/harness/plugins/builtin/agent-terminal.plugin.ts +518 -0
- package/src/server/harness/terminal-frame-builder.server.ts +5 -3
- package/src/server/harness/terminal-manager.server.ts +772 -35
- package/src/server/harness/terminal-ws-handler.server.ts +8 -2
- package/src/server/harness/tool-risk.ts +20 -4
- package/src/components/harness/extension-panel/panels/terminal/__tests__/xterm-webgl.test.ts +0 -68
|
@@ -20,9 +20,12 @@
|
|
|
20
20
|
// TerminalInstance 的 xterm/SSE 已在 terminal-instance.test.tsx mock——此处只验 kind 装配面。
|
|
21
21
|
|
|
22
22
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
|
|
23
|
-
import {
|
|
24
|
-
import { createDockRegistry, dockRegistry } from "../../../dock/dock-registry"
|
|
23
|
+
import { act, render } from "@testing-library/react"
|
|
24
|
+
import { createDockRegistry, dockRegistry, type DockTab } from "../../../dock/dock-registry"
|
|
25
25
|
import { createTerminalKind } from "../kind"
|
|
26
|
+
import { createTerminalControlStore, TerminalTabTitle } from "../tab-control-state"
|
|
27
|
+
import { reconcileAgentTerminalTabs } from "../use-agent-terminal-roster"
|
|
28
|
+
import { m } from "@/paraglide/messages.js"
|
|
26
29
|
|
|
27
30
|
// host 门控:测试环境默认浏览器形态(hostEnabled=false → availability disabled)。
|
|
28
31
|
// 这里显式开 host,使 availability/multiple/maxInstances 路径可达。
|
|
@@ -113,3 +116,217 @@ describe("terminal kind 装配(multiple / maxInstances / keepMounted)", () =
|
|
|
113
116
|
expect(container.querySelector('[data-testid="dock-terminal"]')).toBeNull()
|
|
114
117
|
})
|
|
115
118
|
})
|
|
119
|
+
|
|
120
|
+
// -------------------------------------------------------------------------
|
|
121
|
+
// 生命周期(任务 3):onTabClose 关 tab 主动杀 PTY 进程 + 清 binding。
|
|
122
|
+
// DSH 对标:DSH 关终端即杀进程;本端服务端另有 idle 回收兜底(kill 失败不泄漏)。
|
|
123
|
+
// -------------------------------------------------------------------------
|
|
124
|
+
|
|
125
|
+
/** binding localStorage 键(与 terminal-instance.tsx BINDING_PREFIX 同一拼接规则)。 */
|
|
126
|
+
function bindingKey(threadId: string, tabId: string): string {
|
|
127
|
+
return `allblue.harness.terminal-binding.v1.${threadId}.${tabId}`
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
describe("terminal kind 生命周期(onTabClose:关闭杀进程 + 清 binding)", () => {
|
|
131
|
+
/** fetch mock:onTabClose 不消费响应体,plain object 即可。 */
|
|
132
|
+
function stubFetch() {
|
|
133
|
+
const fetchMock = vi.fn((url: string, init?: RequestInit) => Promise.resolve({ ok: true }))
|
|
134
|
+
vi.stubGlobal("fetch", fetchMock)
|
|
135
|
+
return fetchMock
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
afterEach(() => {
|
|
139
|
+
vi.unstubAllGlobals()
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
it("closeTab → onTabClose 以该 tab 调用,kill fetch 带 instanceId + threadId + keepalive", () => {
|
|
143
|
+
const fetchMock = stubFetch()
|
|
144
|
+
const kind = createTerminalKind(makeDeps())
|
|
145
|
+
// spy 包一层原实现:既断言钩子以被关 tab 调用,又保留 kill fetch 副作用
|
|
146
|
+
const originalOnTabClose = kind.onTabClose
|
|
147
|
+
const onTabCloseSpy = vi.fn((tab: DockTab) => originalOnTabClose?.call(kind, tab))
|
|
148
|
+
kind.onTabClose = onTabCloseSpy
|
|
149
|
+
const reg = createDockRegistry()
|
|
150
|
+
reg.registerKind(kind)
|
|
151
|
+
reg.switchThread("th-close")
|
|
152
|
+
expect(reg.openKind("terminal", { instanceId: "term-abc" })).toBe(true)
|
|
153
|
+
const tab = Object.values(reg.getState().tabs).find((t) => t.kind === "terminal")!
|
|
154
|
+
|
|
155
|
+
reg.closeTab(tab.id)
|
|
156
|
+
|
|
157
|
+
expect(onTabCloseSpy).toHaveBeenCalledTimes(1)
|
|
158
|
+
expect(onTabCloseSpy.mock.calls[0][0]).toMatchObject({ id: tab.id, kind: "terminal" })
|
|
159
|
+
expect(fetchMock).toHaveBeenCalledTimes(1)
|
|
160
|
+
const [url, init] = fetchMock.mock.calls[0]
|
|
161
|
+
expect(url).toContain("/api/harness/terminal/term-abc/kill")
|
|
162
|
+
expect(url).toContain("threadId=th1")
|
|
163
|
+
expect(init?.method).toBe("POST")
|
|
164
|
+
expect(init?.keepalive).toBe(true)
|
|
165
|
+
// tab 已从 state 移除
|
|
166
|
+
expect(reg.getState().tabs[tab.id]).toBeUndefined()
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
it("switchThread 不触发 onTabClose(终端随 binding 存活,换线程不杀进程)", () => {
|
|
170
|
+
const fetchMock = stubFetch()
|
|
171
|
+
const kind = createTerminalKind(makeDeps())
|
|
172
|
+
const spy = vi.fn()
|
|
173
|
+
kind.onTabClose = spy
|
|
174
|
+
const reg = createDockRegistry()
|
|
175
|
+
reg.registerKind(kind)
|
|
176
|
+
reg.switchThread("th-switch-a")
|
|
177
|
+
reg.openKind("terminal", { instanceId: "term-sw" })
|
|
178
|
+
|
|
179
|
+
reg.switchThread("th-switch-b")
|
|
180
|
+
|
|
181
|
+
expect(spy).not.toHaveBeenCalled()
|
|
182
|
+
expect(fetchMock).not.toHaveBeenCalled()
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
it("onTabClose 后 binding key 从 localStorage 移除;无 instanceId 回落 binding 读取", () => {
|
|
186
|
+
const fetchMock = stubFetch()
|
|
187
|
+
const reg = createDockRegistry()
|
|
188
|
+
reg.registerKind(createTerminalKind(makeDeps()))
|
|
189
|
+
reg.switchThread("th-binding")
|
|
190
|
+
reg.openKind("terminal")
|
|
191
|
+
const tab = Object.values(reg.getState().tabs).find((t) => t.kind === "terminal")!
|
|
192
|
+
const key = bindingKey("th1", tab.id)
|
|
193
|
+
window.localStorage.setItem(key, "term-xyz")
|
|
194
|
+
|
|
195
|
+
reg.closeTab(tab.id)
|
|
196
|
+
|
|
197
|
+
expect(window.localStorage.getItem(key)).toBeNull()
|
|
198
|
+
// 回落路径:kill URL 用 binding 里存的 terminalId(现网 openKind 不传 instanceId)
|
|
199
|
+
const [url] = fetchMock.mock.calls[0]
|
|
200
|
+
expect(url).toContain("/api/harness/terminal/term-xyz/kill")
|
|
201
|
+
})
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
// -------------------------------------------------------------------------
|
|
205
|
+
// T14:agent 终端 tab 标题徽章 + kind 接线(renderTabTitle / initialTerminalId /
|
|
206
|
+
// onControlStateChange)。TerminalTabTitle 经闭包 store 自订阅(useSyncExternalStore),
|
|
207
|
+
// state 帧到达只重渲染标题节点——tab-strip 保持通用。
|
|
208
|
+
// -------------------------------------------------------------------------
|
|
209
|
+
|
|
210
|
+
const AGENT_TAB: DockTab = { id: "terminal-9", kind: "terminal", title: "zsh", instanceId: "term_a1" }
|
|
211
|
+
|
|
212
|
+
describe("TerminalTabTitle 徽章(origin/control 驱动)", () => {
|
|
213
|
+
it("store 空(无 state 帧)→ 纯标题无徽章", () => {
|
|
214
|
+
const store = createTerminalControlStore()
|
|
215
|
+
const { container } = render(<TerminalTabTitle tab={AGENT_TAB} store={store} />)
|
|
216
|
+
expect(container.querySelector('[data-testid="dock-tab-agent-badge"]')).toBeNull()
|
|
217
|
+
expect(container.textContent).toContain("zsh")
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
it("origin=agent ∧ control=agent → 「Agent」徽章节点(即 tooltip 触发器)", () => {
|
|
221
|
+
const store = createTerminalControlStore()
|
|
222
|
+
const { container } = render(<TerminalTabTitle tab={AGENT_TAB} store={store} />)
|
|
223
|
+
act(() => store.set(AGENT_TAB.id, { origin: "agent", control: "agent" }))
|
|
224
|
+
const badge = container.querySelector('[data-testid="dock-tab-agent-badge"]')
|
|
225
|
+
expect(badge).not.toBeNull()
|
|
226
|
+
expect(badge!.textContent).toBe(m.harness_panel_terminal_agentBadge())
|
|
227
|
+
// tooltip 文案挂在 badge(trigger)内由 Base UI Tooltip 承载——触发器 slot 校验
|
|
228
|
+
expect(badge!.getAttribute("data-slot")).toBe("tooltip-trigger")
|
|
229
|
+
// tooltip 文案断言(悬停 portal 渲染,jsdom 直验消息函数产物)
|
|
230
|
+
expect(m.harness_panel_terminal_agentBadgeTooltip()).toContain("Agent")
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
it("接管翻转 control=user → 徽章消失(标题保留)", () => {
|
|
234
|
+
const store = createTerminalControlStore()
|
|
235
|
+
const { container } = render(<TerminalTabTitle tab={AGENT_TAB} store={store} />)
|
|
236
|
+
act(() => store.set(AGENT_TAB.id, { origin: "agent", control: "agent" }))
|
|
237
|
+
expect(container.querySelector('[data-testid="dock-tab-agent-badge"]')).not.toBeNull()
|
|
238
|
+
act(() => store.set(AGENT_TAB.id, { origin: "agent", control: "user" }))
|
|
239
|
+
expect(container.querySelector('[data-testid="dock-tab-agent-badge"]')).toBeNull()
|
|
240
|
+
expect(container.textContent).toContain("zsh")
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
it("user origin 终端恒无徽章(用户终端语义)", () => {
|
|
244
|
+
const store = createTerminalControlStore()
|
|
245
|
+
const { container } = render(<TerminalTabTitle tab={AGENT_TAB} store={store} />)
|
|
246
|
+
act(() => store.set(AGENT_TAB.id, { origin: "user", control: "user" }))
|
|
247
|
+
expect(container.querySelector('[data-testid="dock-tab-agent-badge"]')).toBeNull()
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
it("store.set 同值幂等(不重复通知订阅者)", () => {
|
|
251
|
+
const store = createTerminalControlStore()
|
|
252
|
+
const listener = vi.fn()
|
|
253
|
+
store.subscribe(listener)
|
|
254
|
+
act(() => {
|
|
255
|
+
store.set(AGENT_TAB.id, { origin: "agent", control: "agent" })
|
|
256
|
+
store.set(AGENT_TAB.id, { origin: "agent", control: "agent" })
|
|
257
|
+
})
|
|
258
|
+
expect(listener).toHaveBeenCalledTimes(1)
|
|
259
|
+
})
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
describe("terminal kind T14 接线(renderTabTitle / initialTerminalId / onControlStateChange)", () => {
|
|
263
|
+
function stubFetch() {
|
|
264
|
+
const fetchMock = vi.fn((url: string, init?: RequestInit) => Promise.resolve({ ok: true }))
|
|
265
|
+
vi.stubGlobal("fetch", fetchMock)
|
|
266
|
+
return fetchMock
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
afterEach(() => {
|
|
270
|
+
vi.unstubAllGlobals()
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
it("render(tab) 传 initialTerminalId=tab.instanceId;无 instanceId → undefined(用户 tab 恒不带)", () => {
|
|
274
|
+
const kind = createTerminalKind(makeDeps())
|
|
275
|
+
const withId = kind.render({ id: "t1", kind: "terminal", title: "zsh", instanceId: "term_a2" }) as React.ReactElement<Record<string, unknown>>
|
|
276
|
+
expect(withId.props.initialTerminalId).toBe("term_a2")
|
|
277
|
+
const withoutId = kind.render({ id: "t2", kind: "terminal", title: "zsh" }) as React.ReactElement<Record<string, unknown>>
|
|
278
|
+
expect(withoutId.props.initialTerminalId).toBeUndefined()
|
|
279
|
+
})
|
|
280
|
+
|
|
281
|
+
it("onControlStateChange 写 store → renderTabTitle 徽章联动;onTabClose 清 store", () => {
|
|
282
|
+
stubFetch()
|
|
283
|
+
const kind = createTerminalKind(makeDeps())
|
|
284
|
+
const tab: DockTab = { id: "terminal-3", kind: "terminal", title: "zsh", instanceId: "term_a3" }
|
|
285
|
+
// 初始:无徽章
|
|
286
|
+
let view = render(<div>{kind.renderTabTitle!(tab)}</div>)
|
|
287
|
+
expect(view.container.querySelector('[data-testid="dock-tab-agent-badge"]')).toBeNull()
|
|
288
|
+
view.unmount()
|
|
289
|
+
// state 帧到达(经 render 元素 props 的回调注入,与 TerminalInstance 实链同形)
|
|
290
|
+
const el = kind.render(tab) as React.ReactElement<{
|
|
291
|
+
onControlStateChange?: (s: { origin: "user" | "agent"; control: "user" | "agent" }) => void
|
|
292
|
+
}>
|
|
293
|
+
act(() => el.props.onControlStateChange?.({ origin: "agent", control: "agent" }))
|
|
294
|
+
view = render(<div>{kind.renderTabTitle!(tab)}</div>)
|
|
295
|
+
expect(view.container.querySelector('[data-testid="dock-tab-agent-badge"]')).not.toBeNull()
|
|
296
|
+
// 关 tab → onTabClose 清 store → 徽章消失
|
|
297
|
+
kind.onTabClose?.(tab)
|
|
298
|
+
view.unmount()
|
|
299
|
+
view = render(<div>{kind.renderTabTitle!(tab)}</div>)
|
|
300
|
+
expect(view.container.querySelector('[data-testid="dock-tab-agent-badge"]')).toBeNull()
|
|
301
|
+
view.unmount()
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
// I-1 reopen 接缝:agent tab 重开必须回归「用户 tab 无 instanceId」不变量——否则
|
|
305
|
+
// tab 元数据残留死 agent id,T13 reconcile 下一 tick 强制关掉用户刚重开的新终端。
|
|
306
|
+
// 用 dockRegistry 单例(kind 的 onReopen 闭包接的是单例,与现网 dock-surface 同构;
|
|
307
|
+
// 独立 createDockRegistry 实例收不到清除——见 dock-registry.test.ts 的 API 级单测)。
|
|
308
|
+
it("onReopen → registry 清 tab.instanceId → reconcile 对该 tab 无 close 指令", () => {
|
|
309
|
+
stubFetch()
|
|
310
|
+
dockRegistry.registerKind(createTerminalKind(makeDeps()))
|
|
311
|
+
dockRegistry.switchThread("th-reopen-seam")
|
|
312
|
+
expect(dockRegistry.openKind("terminal", { instanceId: "term_dead", title: "agent 终端" })).toBe(true)
|
|
313
|
+
const tab = Object.values(dockRegistry.getState().tabs).find((t) => t.kind === "terminal")!
|
|
314
|
+
expect(tab.instanceId).toBe("term_dead")
|
|
315
|
+
// TerminalInstance reopen → kind 注入的 onReopen → registry 清元数据(实链同形)
|
|
316
|
+
const el = dockRegistry.getKind("terminal")!.render(tab) as React.ReactElement<{ onReopen?: () => void }>
|
|
317
|
+
expect(el.props.onReopen).toBeTypeOf("function")
|
|
318
|
+
act(() => el.props.onReopen?.())
|
|
319
|
+
// registry 状态断言:tab 仍在(没被关)且 instanceId 已清
|
|
320
|
+
const after = dockRegistry.getState().tabs[tab.id]
|
|
321
|
+
expect(after).toBeDefined()
|
|
322
|
+
expect(after.instanceId).toBeUndefined()
|
|
323
|
+
// reconcile 纯函数:死 agent id 不在 roster(exit 后移除)→ 清后无 close 指令;
|
|
324
|
+
// 对照组:元数据未清的形状会被强制关(缺陷回归哨兵)
|
|
325
|
+
const plan = reconcileAgentTerminalTabs([], Object.values(dockRegistry.getState().tabs))
|
|
326
|
+
expect(plan.close).not.toContain(tab.id)
|
|
327
|
+
const staleShape: DockTab = { ...after, instanceId: "term_dead" }
|
|
328
|
+
const stalePlan = reconcileAgentTerminalTabs([], [staleShape])
|
|
329
|
+
expect(stalePlan.close).toContain(tab.id)
|
|
330
|
+
dockRegistry.switchThread(null)
|
|
331
|
+
})
|
|
332
|
+
})
|
package/src/components/harness/extension-panel/panels/terminal/__tests__/terminal-instance.test.tsx
CHANGED
|
@@ -23,6 +23,8 @@
|
|
|
23
23
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
|
|
24
24
|
import { fireEvent, render, screen, waitFor, act } from "@testing-library/react"
|
|
25
25
|
import { TerminalInstance } from "../terminal-instance"
|
|
26
|
+
import { toast } from "@/components/ui/toast"
|
|
27
|
+
import { m } from "@/paraglide/messages.js"
|
|
26
28
|
|
|
27
29
|
// ── xterm-terminal 惰性 chunk 替身(jsdom 不加载真 xterm)──────────────────────
|
|
28
30
|
// 经 hoisted 控制器驱动 onData/onResize + 检查 write/reset + 捕获 theme prop。
|
|
@@ -60,18 +62,28 @@ vi.mock("../xterm-terminal", async () => {
|
|
|
60
62
|
return { default: XtermTerminal }
|
|
61
63
|
})
|
|
62
64
|
|
|
63
|
-
// ── 主题 provider
|
|
64
|
-
const themeCtl = vi.hoisted(() => ({ resolved: "dark" as "light" | "dark" }))
|
|
65
|
+
// ── 主题 provider 替身(组件已不消费 resolved;保留 mock 防 provider 真实现进 jsdom)──
|
|
65
66
|
vi.mock("@/providers/theme-mode-provider", () => ({
|
|
66
67
|
useThemeMode: () => ({
|
|
67
|
-
mode:
|
|
68
|
-
resolved:
|
|
68
|
+
mode: "dark",
|
|
69
|
+
resolved: "dark",
|
|
69
70
|
mounted: true,
|
|
70
71
|
setMode: () => {},
|
|
71
72
|
toggle: () => {},
|
|
72
73
|
}),
|
|
73
74
|
}))
|
|
74
75
|
|
|
76
|
+
// ── theme-color 替身(按 documentElement 的 dark class 返回可区分 oklch)────────
|
|
77
|
+
// 组件主题源 = class 订阅(computeXtermTheme 直读 class + resolveThemeColor);
|
|
78
|
+
// jsdom 真实现无主题变量回落同值 → mock 按 class 区分,测试用 class 翻转驱动断言。
|
|
79
|
+
vi.mock("@/components/design/canvas/document/style/theme-color", () => ({
|
|
80
|
+
resolveThemeColor: (name: string) => {
|
|
81
|
+
const dark = document.documentElement.classList.contains("dark")
|
|
82
|
+
if (name === "--background") return dark ? "oklch(0.153 0.006 107.1)" : "oklch(1 0 0)"
|
|
83
|
+
return dark ? "oklch(0.988 0.003 106.5)" : "oklch(0.153 0.006 107.1)"
|
|
84
|
+
},
|
|
85
|
+
}))
|
|
86
|
+
|
|
75
87
|
// ── EventSource 替身(记录实例,测试手动派发帧)────────────────────────────────
|
|
76
88
|
class FakeEventSource {
|
|
77
89
|
static instances: FakeEventSource[] = []
|
|
@@ -104,9 +116,22 @@ function jsonResponse(body: unknown, status = 200) {
|
|
|
104
116
|
return new Response(JSON.stringify(body), { status })
|
|
105
117
|
}
|
|
106
118
|
|
|
107
|
-
/**
|
|
108
|
-
function
|
|
109
|
-
const
|
|
119
|
+
/** 读 localStorage binding 存的 terminalId(方案 C 后 binding 是 {terminalId, serverEpoch} JSON)。 */
|
|
120
|
+
function storedBindingId(threadId = "th1", tabId = "terminal-1"): string | null {
|
|
121
|
+
const raw = window.localStorage.getItem(`allblue.harness.terminal-binding.v1.${threadId}.${tabId}`)
|
|
122
|
+
if (!raw) return null
|
|
123
|
+
try {
|
|
124
|
+
return (JSON.parse(raw) as { terminalId: string }).terminalId
|
|
125
|
+
} catch {
|
|
126
|
+
return raw // 旧纯 id 格式
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** 装配 fetch 替身:create/input/ack 路由 + 记录 input 载荷 + create 调用数。
|
|
131
|
+
* notFoundTermIds:命中 id 的 events 探测返回 404(服务端已无此终端——dev/host 重启或
|
|
132
|
+
* idle 回收后 binding 存活死 id),驱动 attach 的 notFound→create 重建路径。 */
|
|
133
|
+
function stubFetch(opts: { createShell?: string; failCreate?: boolean; envUnavailable?: boolean; notFoundTermIds?: string[] } = {}) {
|
|
134
|
+
const inputBodies: Array<{ writes?: Array<{ seq: number, data: string, typed?: boolean }>; resize?: { cols: number, rows: number } }> = []
|
|
110
135
|
let createCalls = 0
|
|
111
136
|
const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
112
137
|
const url = typeof input === "string" ? input : input.toString()
|
|
@@ -116,13 +141,18 @@ function stubFetch(opts: { createShell?: string; failCreate?: boolean; envUnavai
|
|
|
116
141
|
return jsonResponse({ error: "terminal_env_unavailable", message: "env down" }, 503)
|
|
117
142
|
}
|
|
118
143
|
if (opts.failCreate) return jsonResponse({ error: "terminal_limit", message: "上限" }, 409)
|
|
119
|
-
return jsonResponse({ terminalId: `term_${createCalls}`, shell: opts.createShell ?? "/bin/zsh" })
|
|
144
|
+
return jsonResponse({ terminalId: `term_${createCalls}`, shell: opts.createShell ?? "/bin/zsh", serverEpoch: "epoch_test" })
|
|
120
145
|
}
|
|
121
146
|
if (url.includes("/input")) {
|
|
122
147
|
inputBodies.push(JSON.parse(String(init?.body ?? "{}")))
|
|
123
148
|
return jsonResponse({ ack: 1 })
|
|
124
149
|
}
|
|
125
150
|
if (url.includes("/ack")) return jsonResponse({ ok: true })
|
|
151
|
+
// events 探测(SSE connect 前置):命中的死 id → 404 terminal_not_found
|
|
152
|
+
if (url.includes("/events")) {
|
|
153
|
+
const dead = opts.notFoundTermIds?.some((id) => url.includes(`/${id}/events`))
|
|
154
|
+
if (dead) return jsonResponse({ error: "terminal_not_found", message: "终端不存在" }, 404)
|
|
155
|
+
}
|
|
126
156
|
return jsonResponse({})
|
|
127
157
|
})
|
|
128
158
|
vi.stubGlobal("fetch", fetchMock)
|
|
@@ -147,14 +177,16 @@ beforeEach(() => {
|
|
|
147
177
|
xt.resizeHandler = null
|
|
148
178
|
xt.theme = null
|
|
149
179
|
xt.dims = { cols: 80, rows: 24 }
|
|
150
|
-
|
|
180
|
+
document.documentElement.classList.add("dark")
|
|
151
181
|
window.localStorage.clear()
|
|
152
182
|
vi.stubGlobal("EventSource", FakeEventSource)
|
|
153
183
|
})
|
|
154
184
|
|
|
155
185
|
afterEach(() => {
|
|
186
|
+
vi.restoreAllMocks() // 还原 performance.now 等 spy(typed 用例的手控时钟)
|
|
156
187
|
vi.unstubAllGlobals()
|
|
157
188
|
window.localStorage.clear()
|
|
189
|
+
document.documentElement.classList.remove("dark")
|
|
158
190
|
})
|
|
159
191
|
|
|
160
192
|
describe("attach(snapshot 重建 / 增量 write / exit 态)", () => {
|
|
@@ -171,7 +203,21 @@ describe("attach(snapshot 重建 / 增量 write / exit 态)", () => {
|
|
|
171
203
|
expect(xt.resets).toBe(1)
|
|
172
204
|
expect(xt.writes).toContain("hello$ ")
|
|
173
205
|
// binding 已写
|
|
174
|
-
expect(
|
|
206
|
+
expect(storedBindingId()).toBe("term_1")
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
it("create 响应的 serverEpoch 写入 binding(方案 C:重连比对区分重启/回收)", async () => {
|
|
210
|
+
stubFetch()
|
|
211
|
+
await renderTerminal()
|
|
212
|
+
act(() => FakeEventSource.last().emit({ type: "snapshot", connectionId: "c1", sequence: 0, screen: "$ ", info: { cols: 80, rows: 24, shell: "/bin/zsh" }, writable: true }))
|
|
213
|
+
await waitFor(() => expect(screen.getByTestId("dock-terminal-instance").getAttribute("data-terminal-phase")).toBe("connected"))
|
|
214
|
+
// binding 存的是 {terminalId, serverEpoch} 对象;create 响应带 epoch → 落进 binding
|
|
215
|
+
const raw = window.localStorage.getItem("allblue.harness.terminal-binding.v1.th1.terminal-1")
|
|
216
|
+
expect(raw).toBeTruthy()
|
|
217
|
+
const parsed = JSON.parse(raw!) as { terminalId: string; serverEpoch?: string }
|
|
218
|
+
expect(parsed.terminalId).toBe("term_1")
|
|
219
|
+
expect(typeof parsed.serverEpoch).toBe("string")
|
|
220
|
+
expect(parsed.serverEpoch).toMatch(/^epoch_/)
|
|
175
221
|
})
|
|
176
222
|
|
|
177
223
|
it("增量 output write + exit → exited(码)", async () => {
|
|
@@ -196,6 +242,39 @@ describe("attach(snapshot 重建 / 增量 write / exit 态)", () => {
|
|
|
196
242
|
})
|
|
197
243
|
})
|
|
198
244
|
|
|
245
|
+
describe("死 terminalId 自动重建(dev/host 重启或 idle 回收后 binding 存活死 id)", () => {
|
|
246
|
+
it("binding 死 id → events 404 → 清 binding + create 重建再连(不再永久 disconnected)", async () => {
|
|
247
|
+
// 预置 binding 指向服务端已不存在的终端(term_dead)
|
|
248
|
+
window.localStorage.setItem("allblue.harness.terminal-binding.v1.th1.terminal-1", "term_dead")
|
|
249
|
+
const { getCreateCalls } = stubFetch({ notFoundTermIds: ["term_dead"] })
|
|
250
|
+
render(<TerminalInstance threadId="th1" tabId="terminal-1" />)
|
|
251
|
+
// attach:读 binding → connect(term_dead) 探测 404 → 清 binding → create(term_1) → 重连成功
|
|
252
|
+
await waitFor(() => {
|
|
253
|
+
expect(getCreateCalls()).toBe(1)
|
|
254
|
+
expect(FakeEventSource.instances.length).toBeGreaterThan(0)
|
|
255
|
+
})
|
|
256
|
+
expect(FakeEventSource.last().url).toContain("/api/harness/terminal/term_1/events")
|
|
257
|
+
act(() => FakeEventSource.last().emit({ type: "snapshot", connectionId: "c1", sequence: 0, screen: "$ ", info: { cols: 80, rows: 24, shell: "/bin/zsh" }, writable: true }))
|
|
258
|
+
await waitFor(() => expect(screen.getByTestId("dock-terminal-instance").getAttribute("data-terminal-phase")).toBe("connected"))
|
|
259
|
+
// binding 已更新为新终端
|
|
260
|
+
expect(storedBindingId()).toBe("term_1")
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
it("重建后服务端仍 404(持续不可用)→ 守卫生效,不无限 create 循环", async () => {
|
|
264
|
+
window.localStorage.setItem("allblue.harness.terminal-binding.v1.th1.terminal-1", "term_dead")
|
|
265
|
+
// 仅 binding 的死 id 探测 404;重建的新终端探测通过但 EventSource 层 404(模拟真实浏览器
|
|
266
|
+
// 探测后到 ES 建连之间服务端状态变化/持续异常)→ ES onerror → disconnected,守卫不再 create
|
|
267
|
+
const { getCreateCalls } = stubFetch({ notFoundTermIds: ["term_dead"] })
|
|
268
|
+
render(<TerminalInstance threadId="th1" tabId="terminal-1" />)
|
|
269
|
+
await waitFor(() => expect(FakeEventSource.instances.length).toBe(1))
|
|
270
|
+
expect(FakeEventSource.last().url).toContain("/term_1/events")
|
|
271
|
+
act(() => FakeEventSource.last().fail())
|
|
272
|
+
await waitFor(() => expect(screen.getByTestId("dock-terminal-instance").getAttribute("data-terminal-phase")).toBe("disconnected"))
|
|
273
|
+
// 守卫:notFound 重建每代只一次 → create 总数 = 1(binding 死 id 重建),不进入 create↔connect 死循环
|
|
274
|
+
expect(getCreateCalls()).toBe(1)
|
|
275
|
+
})
|
|
276
|
+
})
|
|
277
|
+
|
|
199
278
|
describe("批输入(16ms 窗口 + 客户端 seq)", () => {
|
|
200
279
|
it("16ms 窗口合并多次 onData → 单条 write + 递增 seq", async () => {
|
|
201
280
|
const { inputBodies } = stubFetch()
|
|
@@ -295,7 +374,7 @@ describe("状态面(connecting / disconnected / exited + 重连/重开)", ()
|
|
|
295
374
|
fireEvent.click(screen.getByTestId("dock-terminal-reopen"))
|
|
296
375
|
await waitFor(() => expect(getCreateCalls()).toBe(2))
|
|
297
376
|
await waitFor(() => expect(FakeEventSource.last().url).toContain("term_2/events"))
|
|
298
|
-
expect(
|
|
377
|
+
expect(storedBindingId()).toBe("term_2")
|
|
299
378
|
})
|
|
300
379
|
})
|
|
301
380
|
|
|
@@ -314,18 +393,242 @@ describe("binding 恢复", () => {
|
|
|
314
393
|
})
|
|
315
394
|
})
|
|
316
395
|
|
|
317
|
-
describe("主题跟随
|
|
318
|
-
it("
|
|
396
|
+
describe("主题跟随 documentElement class(View Transition flushSync 内翻转,同帧跟随)", () => {
|
|
397
|
+
it("dark class → 深色不透明背景;移除 class → 浅色背景(不依赖 resolved 档位)", async () => {
|
|
319
398
|
stubFetch()
|
|
320
|
-
|
|
399
|
+
// beforeEach 已加 dark class → 初始主题深色
|
|
321
400
|
const { unmount } = render(<TerminalInstance threadId="th1" tabId="terminal-1" />)
|
|
322
401
|
await waitFor(() => expect(xt.theme).not.toBeNull())
|
|
323
|
-
|
|
402
|
+
// 背景不透明、取 --background 计算值(oklch(0.153…) → 深 hex),与主窗口严格一致
|
|
403
|
+
expect(xt.theme?.background).toBe("#0c0c09")
|
|
404
|
+
const darkFg = xt.theme?.foreground
|
|
405
|
+
// 同实例上翻转 class(真实浏览器:View Transition 在 flushSync 内切 class,
|
|
406
|
+
// MutationObserver 同帧触发重算)→ 背景变浅、前景变深,无需重挂载
|
|
407
|
+
act(() => document.documentElement.classList.remove("dark"))
|
|
408
|
+
await waitFor(() => expect(xt.theme?.background).toBe("#ffffff"))
|
|
409
|
+
expect(xt.theme?.foreground).not.toBe(darkFg)
|
|
324
410
|
unmount()
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
411
|
+
})
|
|
412
|
+
})
|
|
413
|
+
|
|
414
|
+
// ── T12 typed 手势识别(250ms 窗口 / 焦点报告排除 / 混合批整批置位)─────────────
|
|
415
|
+
// 手控时钟 mock performance.now(不用 fake timers——16ms 批处理 flush 仍走真定时器 +
|
|
416
|
+
// waitFor,窗口判定完全确定)。手势经容器 dispatchEvent 派发(capture 监听挂容器)。
|
|
417
|
+
describe("typed 输入手势识别(250ms 窗口 / 焦点报告排除)", () => {
|
|
418
|
+
/** 共用前奏:连接至 connected + 装手控时钟(从 t=1000 起,advance 逐段推进)。 */
|
|
419
|
+
async function setupTyped() {
|
|
420
|
+
const { inputBodies } = stubFetch()
|
|
421
|
+
await renderTerminal()
|
|
422
|
+
act(() => FakeEventSource.last().emit({ type: "snapshot", connectionId: "c1", sequence: 0, screen: "$ ", info: { cols: 80, rows: 24, shell: "/bin/zsh" }, writable: true }))
|
|
423
|
+
await waitFor(() => expect(screen.getByTestId("dock-terminal-instance").getAttribute("data-terminal-phase")).toBe("connected"))
|
|
424
|
+
let now = 1000
|
|
425
|
+
const spy = vi.spyOn(performance, "now").mockImplementation(() => now)
|
|
426
|
+
return {
|
|
427
|
+
inputBodies,
|
|
428
|
+
host: screen.getByTestId("dock-terminal-instance"),
|
|
429
|
+
/** 手控时钟推进 ms(手势窗口判定的确定性来源)。 */
|
|
430
|
+
advance: (ms: number) => {
|
|
431
|
+
now += ms
|
|
432
|
+
},
|
|
433
|
+
/** 主动还原 spy(afterEach 兜底之外的自清理)。 */
|
|
434
|
+
restoreClock: () => spy.mockRestore(),
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
it("手势窗口内 keydown → onData('a') → write typed:true", async () => {
|
|
439
|
+
const { inputBodies, host, restoreClock } = await setupTyped()
|
|
440
|
+
act(() => {
|
|
441
|
+
host.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true }))
|
|
442
|
+
xt.dataHandler?.("a")
|
|
443
|
+
})
|
|
444
|
+
await waitFor(() => expect(inputBodies.length).toBeGreaterThan(0))
|
|
445
|
+
expect(lastInput(inputBodies).writes?.[0]).toMatchObject({ seq: 1, data: "a", typed: true })
|
|
446
|
+
restoreClock()
|
|
447
|
+
})
|
|
448
|
+
|
|
449
|
+
it("窗口外(>250ms)→ typed:false", async () => {
|
|
450
|
+
const { inputBodies, host, advance, restoreClock } = await setupTyped()
|
|
451
|
+
act(() => host.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true })))
|
|
452
|
+
advance(400) // 越过 250ms 手势窗口
|
|
453
|
+
act(() => xt.dataHandler?.("a"))
|
|
454
|
+
await waitFor(() => expect(inputBodies.length).toBeGreaterThan(0))
|
|
455
|
+
expect(lastInput(inputBodies).writes?.[0]).toMatchObject({ seq: 1, data: "a", typed: false })
|
|
456
|
+
restoreClock()
|
|
457
|
+
})
|
|
458
|
+
|
|
459
|
+
it("DECSET 1004 焦点报告(\\x1b[I / \\x1b[O)恒 typed:false(即使窗口内)", async () => {
|
|
460
|
+
const { inputBodies, host, restoreClock } = await setupTyped()
|
|
461
|
+
act(() => {
|
|
462
|
+
host.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true }))
|
|
463
|
+
xt.dataHandler?.("\x1b[I")
|
|
464
|
+
})
|
|
465
|
+
await waitFor(() => expect(inputBodies.length).toBeGreaterThan(0))
|
|
466
|
+
expect(lastInput(inputBodies).writes?.[0]).toMatchObject({ seq: 1, data: "\x1b[I", typed: false })
|
|
467
|
+
// \x1b[O(失焦报告)同样排除
|
|
468
|
+
act(() => xt.dataHandler?.("\x1b[O"))
|
|
469
|
+
await waitFor(() => expect(inputBodies.length).toBe(2))
|
|
470
|
+
expect(lastInput(inputBodies).writes?.[0]).toMatchObject({ seq: 2, data: "\x1b[O", typed: false })
|
|
471
|
+
restoreClock()
|
|
472
|
+
})
|
|
473
|
+
|
|
474
|
+
it("混合批:批内任一笔 typed → 整批上送 typed:true(宁可误接管不可漏接管)", async () => {
|
|
475
|
+
const { inputBodies, host, advance, restoreClock } = await setupTyped()
|
|
476
|
+
// 同一 16ms flush 窗口内:第一笔在手势窗口内(typed),第二笔在窗口外(非 typed)
|
|
477
|
+
act(() => {
|
|
478
|
+
host.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true }))
|
|
479
|
+
xt.dataHandler?.("l")
|
|
480
|
+
})
|
|
481
|
+
advance(400)
|
|
482
|
+
act(() => xt.dataHandler?.("s"))
|
|
483
|
+
await waitFor(() => expect(inputBodies.length).toBeGreaterThan(0))
|
|
484
|
+
// 合并为单条 write "ls",整批 typed:true
|
|
485
|
+
expect(lastInput(inputBodies).writes).toHaveLength(1)
|
|
486
|
+
expect(lastInput(inputBodies).writes?.[0]).toMatchObject({ seq: 1, data: "ls", typed: true })
|
|
487
|
+
restoreClock()
|
|
488
|
+
})
|
|
489
|
+
})
|
|
490
|
+
|
|
491
|
+
// ── T14:state 帧控制权(origin/control 上抛 + 接管 toast)────────────────────
|
|
492
|
+
// prevControl 翻转边判定:prev.control==='agent' 且本次 'user' → toast 一次;
|
|
493
|
+
// 初始挂载 prev=null(首帧无论 user/agent 均不 toast);重复 user 帧不重发。
|
|
494
|
+
describe("state 帧控制权(T14 origin/control 上抛 + 接管 toast)", () => {
|
|
495
|
+
async function setup() {
|
|
496
|
+
stubFetch()
|
|
497
|
+
const onControlStateChange = vi.fn()
|
|
498
|
+
const infoSpy = vi.spyOn(toast, "info")
|
|
499
|
+
render(
|
|
500
|
+
<TerminalInstance
|
|
501
|
+
threadId="th1"
|
|
502
|
+
tabId="terminal-1"
|
|
503
|
+
onControlStateChange={onControlStateChange}
|
|
504
|
+
/>,
|
|
505
|
+
)
|
|
506
|
+
await waitFor(() => expect(FakeEventSource.instances.length).toBeGreaterThan(0))
|
|
507
|
+
const emitState = (origin: "user" | "agent", control: "user" | "agent", sequence: number) =>
|
|
508
|
+
act(() =>
|
|
509
|
+
FakeEventSource.last().emit({ type: "state", sequence, cols: 80, rows: 24, origin, control }),
|
|
510
|
+
)
|
|
511
|
+
return { onControlStateChange, infoSpy, emitState }
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
it("state 帧 origin/control → onControlStateChange 原样上抛", async () => {
|
|
515
|
+
const { onControlStateChange, emitState } = await setup()
|
|
516
|
+
emitState("agent", "agent", 1)
|
|
517
|
+
await waitFor(() =>
|
|
518
|
+
expect(onControlStateChange).toHaveBeenCalledWith({ origin: "agent", control: "agent" }),
|
|
519
|
+
)
|
|
520
|
+
emitState("agent", "user", 2)
|
|
521
|
+
await waitFor(() =>
|
|
522
|
+
expect(onControlStateChange).toHaveBeenCalledWith({ origin: "agent", control: "user" }),
|
|
523
|
+
)
|
|
524
|
+
})
|
|
525
|
+
|
|
526
|
+
it("接管翻转(prev agent → user)→ toast 一次;重复 user 帧不重发", async () => {
|
|
527
|
+
const { infoSpy, emitState } = await setup()
|
|
528
|
+
emitState("agent", "agent", 1)
|
|
529
|
+
emitState("agent", "user", 2)
|
|
530
|
+
await waitFor(() => expect(infoSpy).toHaveBeenCalledTimes(1))
|
|
531
|
+
expect(infoSpy).toHaveBeenCalledWith(m.harness_panel_terminal_takeoverToast())
|
|
532
|
+
emitState("agent", "user", 3)
|
|
533
|
+
expect(infoSpy).toHaveBeenCalledTimes(1)
|
|
534
|
+
})
|
|
535
|
+
|
|
536
|
+
it("初始挂载无 prevControl:首帧 control=user / control=agent 均不 toast", async () => {
|
|
537
|
+
const { infoSpy, emitState } = await setup()
|
|
538
|
+
emitState("user", "user", 1)
|
|
539
|
+
expect(infoSpy).not.toHaveBeenCalled()
|
|
540
|
+
emitState("agent", "agent", 2)
|
|
541
|
+
expect(infoSpy).not.toHaveBeenCalled()
|
|
542
|
+
})
|
|
543
|
+
})
|
|
544
|
+
|
|
545
|
+
// ── T14:initialTerminalId 优先 attach ────────────────────────────────────────
|
|
546
|
+
// tab.instanceId 存在(reconcile 开的 agent 终端 tab)→ 跳过 binding/create 直接
|
|
547
|
+
// attach;无效 id 服务端报错走现有 disconnected/reconnect 面,不回落 create。
|
|
548
|
+
describe("initialTerminalId 优先 attach(T14)", () => {
|
|
549
|
+
it("instanceId 存在 → 直接 attach 该 id(零 create、binding 不参与)", async () => {
|
|
550
|
+
const { getCreateCalls } = stubFetch()
|
|
551
|
+
window.localStorage.setItem("allblue.harness.terminal-binding.v1.th1.terminal-1", "term_binding")
|
|
552
|
+
render(<TerminalInstance threadId="th1" tabId="terminal-1" initialTerminalId="term_agent" />)
|
|
553
|
+
await waitFor(() => expect(FakeEventSource.instances.length).toBeGreaterThan(0))
|
|
554
|
+
expect(getCreateCalls()).toBe(0)
|
|
555
|
+
expect(FakeEventSource.last().url).toContain("term_agent/events")
|
|
556
|
+
expect(FakeEventSource.last().url).not.toContain("term_binding")
|
|
557
|
+
})
|
|
558
|
+
|
|
559
|
+
it("无效 id → ES 失败 → disconnected;不回落 create(由 reconcile 关 tab)", async () => {
|
|
560
|
+
const { getCreateCalls } = stubFetch()
|
|
561
|
+
render(<TerminalInstance threadId="th1" tabId="terminal-1" initialTerminalId="term_dead" />)
|
|
562
|
+
await waitFor(() => expect(FakeEventSource.instances.length).toBeGreaterThan(0))
|
|
563
|
+
expect(getCreateCalls()).toBe(0)
|
|
564
|
+
act(() => FakeEventSource.last().fail())
|
|
565
|
+
await waitFor(() =>
|
|
566
|
+
expect(screen.getByTestId("dock-terminal-instance").getAttribute("data-terminal-phase")).toBe(
|
|
567
|
+
"disconnected",
|
|
568
|
+
),
|
|
569
|
+
)
|
|
570
|
+
expect(getCreateCalls()).toBe(0)
|
|
571
|
+
})
|
|
572
|
+
|
|
573
|
+
// I-1:reopen 必须通知外部清 tab 元数据 instanceId(kind 经 onReopen 调
|
|
574
|
+
// registry.clearTabInstanceId)——否则 tab 残留死 agent id,T13 reconcile 误关新终端。
|
|
575
|
+
it("agent tab 重开 → onReopen 回调触发 + 走 create 链(不再回连死 id)", async () => {
|
|
576
|
+
const { getCreateCalls } = stubFetch()
|
|
577
|
+
const onReopen = vi.fn()
|
|
578
|
+
render(
|
|
579
|
+
<TerminalInstance
|
|
580
|
+
threadId="th1"
|
|
581
|
+
tabId="terminal-1"
|
|
582
|
+
initialTerminalId="term_agent"
|
|
583
|
+
onReopen={onReopen}
|
|
584
|
+
/>,
|
|
585
|
+
)
|
|
586
|
+
await waitFor(() => expect(FakeEventSource.instances.length).toBeGreaterThan(0))
|
|
587
|
+
expect(FakeEventSource.last().url).toContain("term_agent/events")
|
|
588
|
+
// exit → 重开
|
|
589
|
+
act(() => FakeEventSource.last().emit({ type: "exit", sequence: 1, code: 0 }))
|
|
590
|
+
await waitFor(() =>
|
|
591
|
+
expect(screen.getByTestId("dock-terminal-instance").getAttribute("data-terminal-phase")).toBe(
|
|
592
|
+
"exited",
|
|
593
|
+
),
|
|
594
|
+
)
|
|
595
|
+
fireEvent.click(screen.getByTestId("dock-terminal-reopen"))
|
|
596
|
+
expect(onReopen).toHaveBeenCalledTimes(1)
|
|
597
|
+
// 重开走 create(覆盖已清,新终端 user origin)
|
|
598
|
+
await waitFor(() => expect(getCreateCalls()).toBe(1))
|
|
599
|
+
await waitFor(() => expect(FakeEventSource.instances.length).toBe(2))
|
|
600
|
+
expect(FakeEventSource.last().url).toContain("term_1/events")
|
|
601
|
+
})
|
|
602
|
+
})
|
|
603
|
+
|
|
604
|
+
// I-2:reopen 必须复位控制权边判定基线(controlRef)——agent 终端退出后重开,新终端
|
|
605
|
+
// 首帧 control='user' 若以 prev=agent 判边会弹假「已接管」toast。
|
|
606
|
+
describe("reopen 接管基线复位(T14-I2)", () => {
|
|
607
|
+
it("agent 终端退出重开后,新终端首帧 control=user 不触发 toast", async () => {
|
|
608
|
+
const { getCreateCalls } = stubFetch()
|
|
609
|
+
const infoSpy = vi.spyOn(toast, "info")
|
|
610
|
+
render(<TerminalInstance threadId="th1" tabId="terminal-1" initialTerminalId="term_agent" />)
|
|
611
|
+
await waitFor(() => expect(FakeEventSource.instances.length).toBeGreaterThan(0))
|
|
612
|
+
// agent 终端在控(state 帧 control=agent)→ 退出
|
|
613
|
+
act(() =>
|
|
614
|
+
FakeEventSource.last().emit({ type: "state", sequence: 1, cols: 80, rows: 24, origin: "agent", control: "agent" }),
|
|
615
|
+
)
|
|
616
|
+
act(() => FakeEventSource.last().emit({ type: "exit", sequence: 2, code: 0 }))
|
|
617
|
+
await waitFor(() =>
|
|
618
|
+
expect(screen.getByTestId("dock-terminal-instance").getAttribute("data-terminal-phase")).toBe(
|
|
619
|
+
"exited",
|
|
620
|
+
),
|
|
621
|
+
)
|
|
622
|
+
// 重开 → 新终端(create)→ 首帧 user
|
|
623
|
+
fireEvent.click(screen.getByTestId("dock-terminal-reopen"))
|
|
624
|
+
await waitFor(() => expect(getCreateCalls()).toBe(1))
|
|
625
|
+
await waitFor(() => expect(FakeEventSource.instances.length).toBe(2))
|
|
626
|
+
act(() =>
|
|
627
|
+
FakeEventSource.last().emit({ type: "state", sequence: 1, cols: 80, rows: 24, origin: "user", control: "user" }),
|
|
628
|
+
)
|
|
629
|
+
// 重开复位 prev=null → 新终端首帧 user 不判接管边(断言核心;connected 转换由
|
|
630
|
+
// snapshot 帧驱动,与本用例无关)
|
|
631
|
+
expect(infoSpy).not.toHaveBeenCalled()
|
|
329
632
|
})
|
|
330
633
|
})
|
|
331
634
|
|