|
| 1 | +/** |
| 2 | + * Tests for copy-sensitive token preservation in the note rendering pipeline |
| 3 | + * (fix #94730: Clack's second-wrap hard-wrapping no longer splits paths). |
| 4 | + * |
| 5 | + * Before the fix, `note()` delegated to `clackNote(...)` which re-wrapped the |
| 6 | + * message with hard wrapAnsi, splitting copy-sensitive tokens (paths, URLs, |
| 7 | + * file-like tokens) mid-token at the box boundary. After the fix, `note()` |
| 8 | + * renders the box itself, preserving the copy-safe wrapping already performed |
| 9 | + * by `wrapNoteMessage`. |
| 10 | + */ |
| 11 | +import { describe, expect, it } from "vitest"; |
| 12 | +import { resolveNoteColumns, withSuppressedNotes, note, wrapNoteMessage } from "./note.js"; |
| 13 | + |
| 14 | +describe("wrapNoteMessage copy-sensitive token preservation (#94730)", () => { |
| 15 | + it("keeps a long session lock path on a single line at 80 columns", () => { |
| 16 | + const path = "~/.openclaw/agents/main/sessions/9c2acae5-841f-4aea-936b-fdb513b60202.jsonl.lock"; |
| 17 | + const wrapped = wrapNoteMessage(path, { columns: 80 }); |
| 18 | + const lines = wrapped.split("\n"); |
| 19 | + |
| 20 | + // The path must appear on one intact line with full extension |
| 21 | + expect(lines.length).toBe(1); |
| 22 | + expect(lines[0]).toContain(".jsonl.lock"); |
| 23 | + // Bug was splitting .jsonl.lock across lines โ verify no mid-path newline |
| 24 | + expect(lines[0]).not.toMatch(/\S+\n\S+/); |
| 25 | + }); |
| 26 | + |
| 27 | + it("keeps URLs intact", () => { |
| 28 | + const url = |
| 29 | + "https://github.com/openclaw/openclaw/blob/main/packages/terminal-core/src/note.ts#L210"; |
| 30 | + const wrapped = wrapNoteMessage(url, { columns: 80 }); |
| 31 | + const lines = wrapped.split("\n"); |
| 32 | + |
| 33 | + expect(lines.length).toBe(1); |
| 34 | + expect(lines[0]).toContain("https://"); |
| 35 | + expect(lines[0]).toContain("note.ts"); |
| 36 | + }); |
| 37 | + |
| 38 | + it("keeps copy-sensitive tokens with underscores intact", () => { |
| 39 | + const path = "/etc/ssh/administrators_authorized_keys"; |
| 40 | + const wrapped = wrapNoteMessage(path, { columns: 80 }); |
| 41 | + const lines = wrapped.split("\n"); |
| 42 | + |
| 43 | + expect(lines.length).toBe(1); |
| 44 | + expect(lines[0]).toContain("administrators_authorized_keys"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("keeps Windows drive paths intact", () => { |
| 48 | + const path = "C:\\Users\\Name\\AppData\\Local\\openclaw\\data"; |
| 49 | + const wrapped = wrapNoteMessage(path, { columns: 80 }); |
| 50 | + const lines = wrapped.split("\n"); |
| 51 | + |
| 52 | + expect(lines.length).toBe(1); |
| 53 | + expect(lines[0]).toContain("C:\\Users"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("keeps absolute Unix paths intact", () => { |
| 57 | + const path = |
| 58 | + "/home/user/workspace/openclaw-worktrees/fix/issue-94730/packages/terminal-core/src/note.ts"; |
| 59 | + const wrapped = wrapNoteMessage(path, { columns: 80 }); |
| 60 | + const lines = wrapped.split("\n"); |
| 61 | + |
| 62 | + expect(lines.length).toBe(1); |
| 63 | + expect(lines[0]).toContain("note.ts"); |
| 64 | + }); |
| 65 | + |
| 66 | + it("does not split a session lock line with path + metadata at 80 columns", () => { |
| 67 | + const input = |
| 68 | + "- Found 1 session lock file.\n" + |
| 69 | + "- ~/.openclaw/agents/main/sessions/9c2acae5-841f-4aea-936b-fdb513b60202.jsonl.lock pid=86519 (alive) age=2m47s stale=no"; |
| 70 | + const wrapped = wrapNoteMessage(input, { columns: 80 }); |
| 71 | + const lines = wrapped.split("\n"); |
| 72 | + |
| 73 | + // The path line must contain the full .jsonl.lock extension |
| 74 | + const pathLine = lines.find((l) => l.includes("9c2acae5")); |
| 75 | + expect(pathLine).toBeTruthy(); |
| 76 | + expect(pathLine!).toContain(".jsonl.lock"); |
| 77 | + expect(pathLine!).not.toContain(".js\n"); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +describe("resolveNoteColumns", () => { |
| 82 | + it("returns the given columns when >= MIN_NOTE_COLUMNS", () => { |
| 83 | + expect(resolveNoteColumns(120)).toBe(120); |
| 84 | + expect(resolveNoteColumns(80)).toBe(80); |
| 85 | + }); |
| 86 | + |
| 87 | + it("falls back to MIN_NOTE_COLUMNS when too small", () => { |
| 88 | + expect(resolveNoteColumns(0)).toBe(80); |
| 89 | + expect(resolveNoteColumns(40)).toBe(80); |
| 90 | + }); |
| 91 | + |
| 92 | + it("falls back to MIN_NOTE_COLUMNS for invalid input", () => { |
| 93 | + expect(resolveNoteColumns(undefined as unknown as number)).toBe(80); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe("withSuppressedNotes", () => { |
| 98 | + it("returns the callback result", () => { |
| 99 | + const result = withSuppressedNotes(() => 42); |
| 100 | + expect(result).toBe(42); |
| 101 | + }); |
| 102 | +}); |
0 commit comments