| 1 | import assert from "node:assert/strict"; |
| 2 | import { mkdtemp, rm } from "node:fs/promises"; |
| 3 | import { tmpdir } from "node:os"; |
| 4 | import { join } from "node:path"; |
| 5 | import test from "node:test"; |
| 6 | import { ThreadStore } from "../../bridge-core/src/lib.mjs"; |
| 7 | import { DingTalkBridge, readConfig } from "../src/lib.mjs"; |
| 8 | |
| 9 | const notice = "[Response exceeds the bridge text limit; inspect the Engine transcript.]"; |
| 10 | |
| 11 | function event(seq, name, payload) { |
| 12 | return { seq, event: name, thread_id: "thread-1", turn_id: "turn-1", payload }; |
| 13 | } |
| 14 | |
| 15 | function streamEnd() { |
| 16 | return { event: "stream.end", thread_id: "thread-1", retryable: false }; |
| 17 | } |
| 18 | |
| 19 | function delta(seq, text) { |
| 20 | return event(seq, "item.delta", { kind: "agent_message", delta: text }); |
| 21 | } |
| 22 | |
| 23 | test("a truncated DingTalk reply keeps its notice last after reopening the turn", async (t) => { |
| 24 | const dir = await mkdtemp(join(tmpdir(), "dingtalk-reply-limit-")); |
| 25 | t.after(() => rm(dir, { recursive: true, force: true })); |
| 26 | const config = readConfig({ |
| 27 | CODEWHALE_RUNTIME_URL: "http://127.0.0.1:7878", |
| 28 | CODEWHALE_RUNTIME_TOKEN: "fixture", |
| 29 | DINGTALK_CLIENT_ID: "fixture-app", |
| 30 | DINGTALK_CLIENT_SECRET: "fixture", |
| 31 | DINGTALK_TENANT_ALLOWLIST: "tenant", |
| 32 | DINGTALK_ACTOR_ALLOWLIST: "tenant:staff", |
| 33 | DINGTALK_STATE_PATH: join(dir, "state.json"), |
| 34 | }); |
| 35 | const original = await ThreadStore.open(config.statePath, { privateMode: true }); |
| 36 | await original.patchChat("chat", { |
| 37 | clientId: config.clientId, |
| 38 | runtimeUrl: config.runtimeUrl, |
| 39 | identity: { botTenant: "tenant", senderTenant: "tenant", staffId: "staff", conversationId: "chat", kind: "1" }, |
| 40 | actorId: "dingtalk:tenant:staff", |
| 41 | threadId: "thread-1", |
| 42 | active: { turnId: "turn-1", seq: 0, text: "x".repeat(999_999) }, |
| 43 | }); |
| 44 | const requests = []; |
| 45 | async function readReply(store, records) { |
| 46 | const bridge = new DingTalkBridge({ |
| 47 | config, |
| 48 | store, |
| 49 | runtimeFetch: async (url) => { |
| 50 | requests.push(url); |
| 51 | return new Response(records.map(record => `data: ${JSON.stringify(record)}\n\n`).join("")); |
| 52 | }, |
| 53 | sendReply: async () => assert.fail("This fixture has no live delivery route"), |
| 54 | }); |
| 55 | try { |
| 56 | await bridge.runTurn("chat"); |
| 57 | } finally { |
| 58 | await bridge.stop(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | await readReply(original, [delta(1, "y"), streamEnd()]); |
| 63 | assert.equal((await original.getChat("chat")).active.text.length, 1_000_000); |
| 64 | await readReply(original, [delta(2, "z"), streamEnd()]); |
| 65 | |
| 66 | const resumed = await ThreadStore.open(config.statePath, { privateMode: true }); |
| 67 | await readReply(resumed, [delta(3, "TAIL"), streamEnd()]); |
| 68 | await readReply(resumed, [event(4, "turn.completed", {})]); |
| 69 | const { active, outbox } = await resumed.getChat("chat"); |
| 70 | assert.deepEqual(requests.map(url => new URL(url, config.runtimeUrl).searchParams.get("since_seq")), ["0", "1", "2", "3"]); |
| 71 | assert.equal(active, null); |
| 72 | assert.equal(outbox.length, 1); |
| 73 | assert.ok(outbox[0].chunks.at(-1).endsWith(notice)); |
| 74 | assert.doesNotMatch(outbox[0].chunks.join(""), /TAIL/); |
| 75 | }); |
| 76 | |
| 77 | test("DingTalk reply truncation does not split a UTF-16 surrogate pair", async (t) => { |
| 78 | const dir = await mkdtemp(join(tmpdir(), "dingtalk-surrogate-limit-")); |
| 79 | t.after(() => rm(dir, { recursive: true, force: true })); |
| 80 | const config = readConfig({ |
| 81 | CODEWHALE_RUNTIME_URL: "http://127.0.0.1:7878", |
| 82 | CODEWHALE_RUNTIME_TOKEN: "fixture", |
| 83 | DINGTALK_CLIENT_ID: "fixture-app", |
| 84 | DINGTALK_CLIENT_SECRET: "fixture", |
| 85 | DINGTALK_TENANT_ALLOWLIST: "tenant", |
| 86 | DINGTALK_ACTOR_ALLOWLIST: "tenant:staff", |
| 87 | DINGTALK_STATE_PATH: join(dir, "state.json"), |
| 88 | }); |
| 89 | const store = await ThreadStore.open(config.statePath, { privateMode: true }); |
| 90 | await store.patchChat("chat", { |
| 91 | clientId: config.clientId, |
| 92 | runtimeUrl: config.runtimeUrl, |
| 93 | identity: { botTenant: "tenant", senderTenant: "tenant", staffId: "staff", conversationId: "chat", kind: "1" }, |
| 94 | actorId: "dingtalk:tenant:staff", |
| 95 | threadId: "thread-1", |
| 96 | active: { turnId: "turn-1", seq: 0, text: `${"a".repeat(999_899)}🧪${"b".repeat(100)}` }, |
| 97 | }); |
| 98 | const bridge = new DingTalkBridge({ |
| 99 | config, |
| 100 | store, |
| 101 | runtimeFetch: async () => new Response( |
| 102 | [delta(1, "c"), event(2, "turn.completed", {})].map((r) => `data: ${JSON.stringify(r)}\n\n`).join("") |
| 103 | ), |
| 104 | sendReply: async () => assert.fail("This fixture has no live delivery route"), |
| 105 | }); |
| 106 | try { |
| 107 | await bridge.runTurn("chat"); |
| 108 | } finally { |
| 109 | await bridge.stop(); |
| 110 | } |
| 111 | const { outbox } = await store.getChat("chat"); |
| 112 | const full = outbox[0].chunks.join(""); |
| 113 | assert.ok(full.endsWith(notice)); |
| 114 | assert.equal(full.isWellFormed(), true); |
| 115 | }); |
| 116 |