| 1 | // Run: tsx src/__tests__/memory-compiler-display.test.ts |
| 2 | // |
| 3 | // Display-boundary safety net for #5361: a corrupted/accreted Memory v5 contract |
| 4 | // from the pre-fix goal loop (#5342) must never render as raw JSON "乱码". |
| 5 | |
| 6 | import { stripMemoryCompilerExecution } from "../lib/memoryCompilerDisplay"; |
| 7 | |
| 8 | let passed = 0; |
| 9 | let failed = 0; |
| 10 | |
| 11 | function ok(cond: boolean, label: string) { |
| 12 | if (cond) { |
| 13 | process.stdout.write(` PASS ${label}\n`); |
| 14 | passed += 1; |
| 15 | } else { |
| 16 | process.stdout.write(` FAIL ${label}\n`); |
| 17 | failed += 1; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | const contract = (sourceEvent: string) => |
| 22 | "<memory-compiler-execution>\n" + |
| 23 | JSON.stringify({ type: "memory_v5_execution_contract", planner_ir: { version: 5, source_event: sourceEvent } }) + |
| 24 | "\n</memory-compiler-execution>"; |
| 25 | |
| 26 | console.log("\nmemory compiler display strip"); |
| 27 | |
| 28 | ok(stripMemoryCompilerExecution("fix the login bug") === "fix the login bug", "leaves plain user text untouched"); |
| 29 | |
| 30 | const complete = stripMemoryCompilerExecution(contract("do the thing")); |
| 31 | ok(!complete.includes("memory-compiler-execution"), "removes a complete contract block"); |
| 32 | |
| 33 | const withText = stripMemoryCompilerExecution("hello\n" + contract("hello")); |
| 34 | ok(withText.includes("hello") && !withText.includes("<memory-compiler-execution>"), "removes a complete block after user text"); |
| 35 | |
| 36 | const partial = 'keep this\n<memory-compiler-execution>\n{"planner_ir":{"source_event":"keep this",' + "x".repeat(40); |
| 37 | const cut = stripMemoryCompilerExecution(partial); |
| 38 | ok( |
| 39 | cut.includes("keep this") && !cut.includes("<memory-compiler-execution>") && !cut.includes("planner_ir"), |
| 40 | "cuts a dangling/truncated block instead of leaking raw JSON", |
| 41 | ); |
| 42 | |
| 43 | console.log(`\n${passed} passed, ${failed} failed`); |
| 44 | if (failed > 0) process.exit(1); |
| 45 |