| 1 | import assert from "node:assert/strict"; |
| 2 | import { mkdtemp, rm, stat } from "node:fs/promises"; |
| 3 | import { tmpdir } from "node:os"; |
| 4 | import path from "node:path"; |
| 5 | import test from "node:test"; |
| 6 | |
| 7 | import { ThreadStore, validateBridgeConfig, isApprovalResponse, isDenyResponse, sanitizeFilename, formatPublicError, assertWecomAck, createStreamReply } from "../src/lib.mjs"; |
| 8 | |
| 9 | // ─── ThreadStore ───────────────────────────────────────────────── |
| 10 | |
| 11 | test("ThreadStore writes private state files", async () => { |
| 12 | const dir = await mkdtemp(path.join(tmpdir(), "codewhale-wecom-")); |
| 13 | try { |
| 14 | const statePath = path.join(dir, "nested", "thread-map.json"); |
| 15 | const store = await ThreadStore.open(statePath); |
| 16 | |
| 17 | await store.setChat("single:user-a", { |
| 18 | threadId: "thread-a", |
| 19 | lastSeq: 1, |
| 20 | activeTurnId: null |
| 21 | }); |
| 22 | |
| 23 | const saved = await ThreadStore.open(statePath); |
| 24 | assert.equal((await saved.getChat("single:user-a")).threadId, "thread-a"); |
| 25 | |
| 26 | if (process.platform !== "win32") { |
| 27 | assert.equal((await stat(path.dirname(statePath))).mode & 0o777, 0o700); |
| 28 | assert.equal((await stat(statePath)).mode & 0o777, 0o600); |
| 29 | } |
| 30 | } finally { |
| 31 | await rm(dir, { recursive: true, force: true }); |
| 32 | } |
| 33 | }); |
| 34 | |
| 35 | // ─── Config validation ────────────────────────────────────────── |
| 36 | |
| 37 | test("validateBridgeConfig rejects placeholder secrets", () => { |
| 38 | const result = validateBridgeConfig({ |
| 39 | WECOM_BOT_ID: "your-bot-id", |
| 40 | WECOM_BOT_SECRET: "your-bot-secret", |
| 41 | CODEWHALE_RUNTIME_TOKEN: "replace-with-long-random-token", |
| 42 | CODEWHALE_RUNTIME_URL: "http://127.0.0.1:7878" |
| 43 | }); |
| 44 | |
| 45 | assert.equal(result.ok, false); |
| 46 | assert.deepEqual( |
| 47 | result.errors.map((item) => item.code), |
| 48 | ["placeholder_runtime_token"] |
| 49 | ); |
| 50 | }); |
| 51 | |
| 52 | // ─── isApprovalResponse ───────────────────────────────────────── |
| 53 | |
| 54 | test("isApprovalResponse: single-word Chinese keywords", () => { |
| 55 | for (const kw of ["允许", "可以", "好", "同意", "批准"]) { |
| 56 | assert.equal(isApprovalResponse(kw), true, `"${kw}" should be approval`); |
| 57 | } |
| 58 | }); |
| 59 | |
| 60 | test("isApprovalResponse: single-word English keywords", () => { |
| 61 | for (const kw of ["yes", "ok", "y", "approve", "allow"]) { |
| 62 | assert.equal(isApprovalResponse(kw), true, `"${kw}" should be approval`); |
| 63 | } |
| 64 | }); |
| 65 | |
| 66 | test("isApprovalResponse: two-character Chinese keywords", () => { |
| 67 | for (const kw of ["好的", "可以", "没问题", "批准了", "同意"]) { |
| 68 | assert.equal(isApprovalResponse(kw), true, `"${kw}" should be approval`); |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | test("isApprovalResponse: case-insensitive", () => { |
| 73 | assert.equal(isApprovalResponse("YES"), true); |
| 74 | assert.equal(isApprovalResponse("Ok"), true); |
| 75 | assert.equal(isApprovalResponse("Allow"), true); |
| 76 | }); |
| 77 | |
| 78 | test("isApprovalResponse: trims whitespace", () => { |
| 79 | assert.equal(isApprovalResponse(" 允许 "), true); |
| 80 | assert.equal(isApprovalResponse(" yes "), true); |
| 81 | }); |
| 82 | |
| 83 | test("isApprovalResponse: rejects non-approval text", () => { |
| 84 | assert.equal(isApprovalResponse("帮我查一下"), false); |
| 85 | assert.equal(isApprovalResponse("/allow abc123"), false); |
| 86 | assert.equal(isApprovalResponse("run prompt"), false); |
| 87 | }); |
| 88 | |
| 89 | test("isApprovalResponse: handles null/empty/undefined", () => { |
| 90 | assert.equal(isApprovalResponse(null), false); |
| 91 | assert.equal(isApprovalResponse(""), false); |
| 92 | assert.equal(isApprovalResponse(undefined), false); |
| 93 | }); |
| 94 | |
| 95 | // ─── isDenyResponse ───────────────────────────────────────────── |
| 96 | |
| 97 | test("isDenyResponse: single-word Chinese keywords", () => { |
| 98 | for (const kw of ["拒绝", "不行", "不要", "取消", "否"]) { |
| 99 | assert.equal(isDenyResponse(kw), true, `"${kw}" should be denial`); |
| 100 | } |
| 101 | }); |
| 102 | |
| 103 | test("isDenyResponse: single-word English keywords", () => { |
| 104 | for (const kw of ["no", "n", "deny", "reject", "stop"]) { |
| 105 | assert.equal(isDenyResponse(kw), true, `"${kw}" should be denial`); |
| 106 | } |
| 107 | }); |
| 108 | |
| 109 | test("isDenyResponse: two-character Chinese keywords", () => { |
| 110 | for (const kw of ["不可以", "不同意", "不要执行"]) { |
| 111 | assert.equal(isDenyResponse(kw), true, `"${kw}" should be denial`); |
| 112 | } |
| 113 | }); |
| 114 | |
| 115 | test("isDenyResponse: case-insensitive and trims whitespace", () => { |
| 116 | assert.equal(isDenyResponse(" NO "), true); |
| 117 | assert.equal(isDenyResponse("No"), true); |
| 118 | }); |
| 119 | |
| 120 | test("isDenyResponse: rejects non-denial text", () => { |
| 121 | assert.equal(isDenyResponse("让我想想"), false); |
| 122 | assert.equal(isDenyResponse("/deny abc123"), false); |
| 123 | assert.equal(isDenyResponse("继续"), false); |
| 124 | }); |
| 125 | |
| 126 | test("isDenyResponse: handles null/empty/undefined", () => { |
| 127 | assert.equal(isDenyResponse(null), false); |
| 128 | assert.equal(isDenyResponse(""), false); |
| 129 | assert.equal(isDenyResponse(undefined), false); |
| 130 | }); |
| 131 | |
| 132 | // ─── Mutual exclusivity ───────────────────────────────────────── |
| 133 | |
| 134 | test("no keyword is both approval and denial", () => { |
| 135 | const all = [ |
| 136 | "允许", "可以", "好", "同意", "批准", |
| 137 | "好的", "没问题", "批准了", |
| 138 | "yes", "ok", "y", "approve", "allow", |
| 139 | "拒绝", "不行", "不要", "取消", "否", |
| 140 | "不可以", "不同意", "不要执行", |
| 141 | "no", "n", "deny", "reject", "stop" |
| 142 | ]; |
| 143 | for (const kw of all) { |
| 144 | assert.notEqual( |
| 145 | isApprovalResponse(kw), isDenyResponse(kw), |
| 146 | `"${kw}" should not be both approval and denial` |
| 147 | ); |
| 148 | } |
| 149 | }); |
| 150 | |
| 151 | test("WeCom accepted turn origin survives reload and cannot transfer to a later member", async () => { |
| 152 | const dir = await mkdtemp(path.join(tmpdir(), "codewhale-wecom-origin-")); |
| 153 | try { |
| 154 | const statePath = path.join(dir, "thread-map.json"); |
| 155 | const store = await ThreadStore.open(statePath); |
| 156 | await store.setChat("group-a", { threadId: "thread-a" }); |
| 157 | await store.recordTurnOrigin("group-a", "thread-a", "turn-a", "wecom-user:alice"); |
| 158 | await store.patchChat("group-a", { authorizedIdentity: { userId: "bob" }, activeTurnId: "turn-b" }); |
| 159 | const reopened = await ThreadStore.open(statePath); |
| 160 | assert.equal(reopened.turnOrigin("group-a", "thread-a", "turn-a").actorId, "wecom-user:alice"); |
| 161 | await assert.rejects(reopened.recordTurnOrigin("group-a", "thread-a", "turn-a", "wecom-user:bob")); |
| 162 | assert.equal(reopened.turnOrigin("group-a", "thread-a", "turn-a").actorId, "wecom-user:alice"); |
| 163 | } finally { |
| 164 | await rm(dir, { recursive: true, force: true }); |
| 165 | } |
| 166 | }); |
| 167 | |
| 168 | // ─── sanitizeFilename ──────────────────────────────────────────── |
| 169 | |
| 170 | test("sanitizeFilename strips path components and keeps the name", () => { |
| 171 | const risky = sanitizeFilename("../../etc/passwd"); |
| 172 | assert.ok(!risky.includes("/"), risky); |
| 173 | assert.ok(!risky.startsWith("."), risky); |
| 174 | assert.equal(sanitizeFilename("clip final.mp4"), "clip final.mp4"); |
| 175 | assert.equal(sanitizeFilename(" .hidden. "), "hidden"); |
| 176 | assert.equal(sanitizeFilename("", "fallback.bin"), "fallback.bin"); |
| 177 | const long = "a".repeat(500) + ".mp4"; |
| 178 | assert.ok(sanitizeFilename(long).length <= 120); |
| 179 | }); |
| 180 | |
| 181 | // ─── formatPublicError ─────────────────────────────────────────── |
| 182 | |
| 183 | test("formatPublicError renders WeCom error objects", () => { |
| 184 | assert.equal( |
| 185 | formatPublicError({ errcode: 846608, errmsg: "stream message update expired (>10 minutes)" }), |
| 186 | "errcode=846608: stream message update expired (>10 minutes)" |
| 187 | ); |
| 188 | assert.equal(formatPublicError({ errcode: 40011 }), "errcode=40011:"); |
| 189 | }); |
| 190 | |
| 191 | test("formatPublicError keeps messages and plain strings", () => { |
| 192 | assert.equal(formatPublicError(new Error("boom")), "boom"); |
| 193 | assert.equal(formatPublicError("boom"), "boom"); |
| 194 | assert.equal(formatPublicError(undefined), "unknown error"); |
| 195 | assert.match(formatPublicError({ no: "msg" }), /no.*msg|unknown/); |
| 196 | }); |
| 197 | |
| 198 | // ─── createStreamReply ─────────────────────────────────────────── |
| 199 | |
| 200 | test("createStreamReply rotates the stream after rotateMs", async () => { |
| 201 | const calls = []; |
| 202 | const client = { replyStream: async (frame, id, text, finish) => calls.push({ id, text, finish }) }; |
| 203 | let clock = 1_000_000; |
| 204 | const ids = ["s1", "s2", "s3"]; |
| 205 | const streamer = createStreamReply(client, {}, { rotateMs: 60_000, now: () => clock, genId: () => ids.shift() }); |
| 206 | await streamer.update("part-1", false); |
| 207 | clock += 61_000; // past the rotation window |
| 208 | await streamer.update("part-2", false); |
| 209 | const finished = calls.filter((c) => c.finish); |
| 210 | assert.equal(calls[0].id, "s1"); |
| 211 | assert.equal(calls.length, 3); // finalize s1 + update s2 |
| 212 | assert.ok(finished[0].text.includes("part-2")); // finalize carries the accumulated text |
| 213 | assert.equal(calls[2].id, "s2"); |
| 214 | }); |
| 215 | |
| 216 | test("createStreamReply reopens once when an update is rejected", async () => { |
| 217 | let rejectFirst = true; |
| 218 | const calls = []; |
| 219 | const client = { |
| 220 | replyStream: async (frame, id, text, finish) => { |
| 221 | if (rejectFirst) { rejectFirst = false; throw { errcode: 846608, errmsg: "expired" }; } |
| 222 | calls.push({ id, text, finish }); |
| 223 | } |
| 224 | }; |
| 225 | const ids = ["s1", "s2"]; |
| 226 | const streamer = createStreamReply(client, {}, { genId: () => ids.shift() }); |
| 227 | await streamer.update("hello", false); |
| 228 | assert.equal(calls.length, 1); |
| 229 | assert.equal(calls[0].id, "s2"); |
| 230 | }); |
| 231 | |
| 232 | test("createStreamReply rethrows when the reopened stream is also rejected", async () => { |
| 233 | const client = { |
| 234 | replyStream: async () => { throw { errcode: 846608, errmsg: "expired" }; } |
| 235 | }; |
| 236 | const streamer = createStreamReply(client, {}, { genId: () => "s2" }); |
| 237 | await assert.rejects(streamer.update("hello", true), (error) => error.errcode === 846608); |
| 238 | }); |
| 239 | |
| 240 | test("assertWecomAck and createStreamReply treat resolved non-zero errcode as failure", async () => { |
| 241 | assert.equal(assertWecomAck(undefined), undefined); |
| 242 | assert.deepEqual(assertWecomAck({ errcode: 0 }), { errcode: 0 }); |
| 243 | assert.throws(() => assertWecomAck({ body: { errcode: 846608, errmsg: "expired" } }), (e) => e.errcode === 846608); |
| 244 | |
| 245 | let first = true; |
| 246 | const calls = []; |
| 247 | const client = { |
| 248 | replyStream: async (_frame, id, text, finish) => { |
| 249 | calls.push({ id, text, finish }); |
| 250 | if (first) { |
| 251 | first = false; |
| 252 | return { errcode: 846608, errmsg: "expired" }; |
| 253 | } |
| 254 | return { errcode: 0 }; |
| 255 | } |
| 256 | }; |
| 257 | const ids = ["s1", "s2"]; |
| 258 | const streamer = createStreamReply(client, {}, { genId: () => ids.shift() }); |
| 259 | await streamer.update("hello", true); |
| 260 | assert.deepEqual(calls.map((c) => c.id), ["s1", "s2"]); |
| 261 | }); |
| 262 |