|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { |
| 5 | + extractPromptText, |
| 6 | + parsePromptText, |
| 7 | +} from "./agentSessionTranscriptHelpers.ts"; |
| 8 | + |
| 9 | +const HEX = "a".repeat(64); |
| 10 | +const HEX_UPPER = "A".repeat(64); |
| 11 | + |
| 12 | +// --- parsePromptText: no section headers --- |
| 13 | + |
| 14 | +test("parsePromptText returns the empty/Prompt fallback for whitespace-only input", () => { |
| 15 | + // The early `sections.length === 0` branch only fires when there are no |
| 16 | + // section bodies at all (e.g. empty/whitespace input). |
| 17 | + const result = parsePromptText(" "); |
| 18 | + assert.deepEqual(result, { |
| 19 | + sections: [], |
| 20 | + userText: "", |
| 21 | + userTitle: "Prompt", |
| 22 | + userPubkey: null, |
| 23 | + }); |
| 24 | +}); |
| 25 | + |
| 26 | +test("parsePromptText wraps header-less free text in a single Prompt section", () => { |
| 27 | + // Free text with no `[header]` becomes one "Prompt" section. Since no |
| 28 | + // section is a "Sprout event", there is no event content to surface, so |
| 29 | + // userText is empty and the title falls through to "Sprout event". |
| 30 | + const result = parsePromptText("just some free text"); |
| 31 | + assert.deepEqual( |
| 32 | + result.sections.map((s) => s.title), |
| 33 | + ["Prompt"], |
| 34 | + ); |
| 35 | + assert.equal(result.sections[0].body, "just some free text"); |
| 36 | + assert.equal(result.userText, ""); |
| 37 | + assert.equal(result.userTitle, "Sprout event"); |
| 38 | + assert.equal(result.userPubkey, null); |
| 39 | +}); |
| 40 | + |
| 41 | +// --- parsePromptText: Sprout event section --- |
| 42 | + |
| 43 | +test("parsePromptText extracts content, hex pubkey, and a title-cased kind", () => { |
| 44 | + const text = [ |
| 45 | + "[System]", |
| 46 | + "system preamble here", |
| 47 | + "", |
| 48 | + "[Sprout event: @mention]", |
| 49 | + "Channel: demo", |
| 50 | + `From: Wes (hex: ${HEX})`, |
| 51 | + "Content: hello @Brain please look", |
| 52 | + ].join("\n"); |
| 53 | + |
| 54 | + const result = parsePromptText(text); |
| 55 | + |
| 56 | + assert.equal(result.userText, "hello @Brain please look"); |
| 57 | + assert.equal(result.userPubkey, HEX); |
| 58 | + // titleCase capitalizes after word boundaries but leaves the leading "@" |
| 59 | + // (a non-word char) in place: "@mention" -> "@Mention". |
| 60 | + assert.equal(result.userTitle, "@Mention"); |
| 61 | + // Both headers become sections. |
| 62 | + assert.deepEqual( |
| 63 | + result.sections.map((s) => s.title), |
| 64 | + ["System", "Sprout event: @mention"], |
| 65 | + ); |
| 66 | +}); |
| 67 | + |
| 68 | +test("parsePromptText lowercases the extracted hex pubkey", () => { |
| 69 | + const text = [ |
| 70 | + "[Sprout event: dm]", |
| 71 | + `From: Someone (hex: ${HEX_UPPER})`, |
| 72 | + "Content: hi", |
| 73 | + ].join("\n"); |
| 74 | + |
| 75 | + const result = parsePromptText(text); |
| 76 | + assert.equal(result.userPubkey, HEX); |
| 77 | +}); |
| 78 | + |
| 79 | +test("parsePromptText yields a null pubkey when From has no hex", () => { |
| 80 | + const text = ["[Sprout event: note]", "From: Someone", "Content: hi"].join( |
| 81 | + "\n", |
| 82 | + ); |
| 83 | + |
| 84 | + const result = parsePromptText(text); |
| 85 | + assert.equal(result.userPubkey, null); |
| 86 | + assert.equal(result.userText, "hi"); |
| 87 | + assert.equal(result.userTitle, "Note"); |
| 88 | +}); |
| 89 | + |
| 90 | +test("parsePromptText defaults the title to 'Sprout event' when no kind is present", () => { |
| 91 | + const text = ["[Sprout event]", "Content: x"].join("\n"); |
| 92 | + const result = parsePromptText(text); |
| 93 | + assert.equal(result.userTitle, "Sprout event"); |
| 94 | +}); |
| 95 | + |
| 96 | +test("parsePromptText leading text before a header becomes a Prompt section", () => { |
| 97 | + const text = ["preamble line", "[Other]", "body"].join("\n"); |
| 98 | + const result = parsePromptText(text); |
| 99 | + assert.deepEqual( |
| 100 | + result.sections.map((s) => s.title), |
| 101 | + ["Prompt", "Other"], |
| 102 | + ); |
| 103 | +}); |
| 104 | + |
| 105 | +// --- extractPromptText --- |
| 106 | + |
| 107 | +test("extractPromptText joins text blocks from params.prompt", () => { |
| 108 | + const payload = { |
| 109 | + params: { |
| 110 | + prompt: [{ text: "line one" }, { text: "line two" }], |
| 111 | + }, |
| 112 | + }; |
| 113 | + assert.equal(extractPromptText(payload), "line one\nline two"); |
| 114 | +}); |
| 115 | + |
| 116 | +test("extractPromptText handles plain string blocks", () => { |
| 117 | + const payload = { params: { prompt: ["a", "b"] } }; |
| 118 | + assert.equal(extractPromptText(payload), "a\nb"); |
| 119 | +}); |
| 120 | + |
| 121 | +test("extractPromptText returns empty string when prompt is missing or not an array", () => { |
| 122 | + assert.equal(extractPromptText({}), ""); |
| 123 | + assert.equal(extractPromptText({ params: { prompt: "nope" } }), ""); |
| 124 | +}); |
0 commit comments