import test from "node:test"
import assert from "node:assert/strict"
import { readFileSync } from "node:fs"

const ipcSource = readFileSync(new URL("../src/main/ipc.ts", import.meta.url), "utf8")
const preloadSource = readFileSync(new URL("../src/preload/index.ts", import.meta.url), "utf8")

test("desktop IPC registers chat cancel stream handler", () => {
  assert.match(ipcSource, /activeChatStreams/)
  assert.match(ipcSource, /cancelStream\s*=\s*async/)
  assert.match(ipcSource, /IPC_CHANNELS\.chatCancelStream/)
  assert.match(preloadSource, /cancelStream: \(requestId: string, runId\?: string, sessionId\?: string\)/)
})

test("desktop cancel marks assistant stream as stopped and broadcasts cancelled event", () => {
  assert.match(ipcSource, /statusLabel:\s*"已停止"/)
  assert.match(ipcSource, /type:\s*"cancelled"/)
  assert.match(ipcSource, /gatewayClient\.cancelChatRun/)
})
