|
| 1 | +/** |
| 2 | + * Regression test: action params must be extracted from standalone XML blocks |
| 3 | + * when the LLM outputs actions as a comma-separated list. |
| 4 | + * |
| 5 | + * The LLM may respond with: |
| 6 | + * <actions>REPLY,START_CODING_TASK</actions> |
| 7 | + * <START_CODING_TASK> |
| 8 | + * <repo>https://github.com/org/repo</repo> |
| 9 | + * <agents>claude:Fix auth | codex:Write tests</agents> |
| 10 | + * </START_CODING_TASK> |
| 11 | + * |
| 12 | + * The XML parser puts "START_CODING_TASK" as a top-level key on parsedXml. |
| 13 | + * extractStandaloneActionParams collects these into the legacy flat format |
| 14 | + * that parseActionParams can consume downstream. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, expect, it } from "vitest"; |
| 18 | +import { parseActionParams } from "../actions"; |
| 19 | +import { |
| 20 | + extractStandaloneActionParams, |
| 21 | + RESERVED_XML_KEYS, |
| 22 | +} from "../services/message"; |
| 23 | + |
| 24 | +// --------------------------------------------------------------------------- |
| 25 | +// extractStandaloneActionParams |
| 26 | +// --------------------------------------------------------------------------- |
| 27 | + |
| 28 | +describe("extractStandaloneActionParams", () => { |
| 29 | + it("extracts params from a matching parsedXml key", () => { |
| 30 | + const parsedXml = { |
| 31 | + actions: "REPLY,START_CODING_TASK", |
| 32 | + thought: "Spawning agents", |
| 33 | + START_CODING_TASK: |
| 34 | + "<repo>https://github.com/org/repo</repo><task>Fix it</task>", |
| 35 | + }; |
| 36 | + const result = extractStandaloneActionParams( |
| 37 | + ["REPLY", "START_CODING_TASK"], |
| 38 | + parsedXml, |
| 39 | + ); |
| 40 | + expect(result).toContain("<START_CODING_TASK>"); |
| 41 | + expect(result).toContain("<repo>https://github.com/org/repo</repo>"); |
| 42 | + expect(result).toContain("<task>Fix it</task>"); |
| 43 | + }); |
| 44 | + |
| 45 | + it("matches action names case-insensitively", () => { |
| 46 | + const parsedXml = { |
| 47 | + start_coding_task: "<task>Fix</task>", |
| 48 | + }; |
| 49 | + const result = extractStandaloneActionParams( |
| 50 | + ["START_CODING_TASK"], |
| 51 | + parsedXml, |
| 52 | + ); |
| 53 | + expect(result).toContain("<START_CODING_TASK>"); |
| 54 | + expect(result).toContain("<task>Fix</task>"); |
| 55 | + }); |
| 56 | + |
| 57 | + it("skips reserved keys even if they match action names", () => { |
| 58 | + const parsedXml = { |
| 59 | + actions: "REPLY", |
| 60 | + thought: "something", |
| 61 | + text: "<bold>hello</bold>", |
| 62 | + }; |
| 63 | + const result = extractStandaloneActionParams( |
| 64 | + ["actions", "thought", "text"], |
| 65 | + parsedXml, |
| 66 | + ); |
| 67 | + expect(result).toBe(""); |
| 68 | + }); |
| 69 | + |
| 70 | + it("skips keys that do not contain XML (plain text values)", () => { |
| 71 | + const parsedXml = { |
| 72 | + MY_ACTION: "just a plain string without XML", |
| 73 | + }; |
| 74 | + const result = extractStandaloneActionParams(["MY_ACTION"], parsedXml); |
| 75 | + expect(result).toBe(""); |
| 76 | + }); |
| 77 | + |
| 78 | + it("handles multiple actions with params", () => { |
| 79 | + const parsedXml = { |
| 80 | + START_CODING_TASK: "<repo>https://github.com/org/repo</repo>", |
| 81 | + FINALIZE_WORKSPACE: "<workspaceId>ws-123</workspaceId>", |
| 82 | + }; |
| 83 | + const result = extractStandaloneActionParams( |
| 84 | + ["START_CODING_TASK", "FINALIZE_WORKSPACE"], |
| 85 | + parsedXml, |
| 86 | + ); |
| 87 | + expect(result).toContain("<START_CODING_TASK>"); |
| 88 | + expect(result).toContain("<FINALIZE_WORKSPACE>"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("returns empty string when no matches found", () => { |
| 92 | + const result = extractStandaloneActionParams(["REPLY", "NONE"], {}); |
| 93 | + expect(result).toBe(""); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +// --------------------------------------------------------------------------- |
| 98 | +// RESERVED_XML_KEYS |
| 99 | +// --------------------------------------------------------------------------- |
| 100 | + |
| 101 | +describe("RESERVED_XML_KEYS", () => { |
| 102 | + it("includes standard response schema fields", () => { |
| 103 | + expect(RESERVED_XML_KEYS.has("actions")).toBe(true); |
| 104 | + expect(RESERVED_XML_KEYS.has("thought")).toBe(true); |
| 105 | + expect(RESERVED_XML_KEYS.has("text")).toBe(true); |
| 106 | + expect(RESERVED_XML_KEYS.has("simple")).toBe(true); |
| 107 | + expect(RESERVED_XML_KEYS.has("providers")).toBe(true); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +// --------------------------------------------------------------------------- |
| 112 | +// parseActionParams integration (end-to-end with the assembled format) |
| 113 | +// --------------------------------------------------------------------------- |
| 114 | + |
| 115 | +describe("parseActionParams with standalone action block format", () => { |
| 116 | + it("extracts params from assembled <ACTION_NAME>...</ACTION_NAME> format", () => { |
| 117 | + // This is the format that extractStandaloneActionParams produces |
| 118 | + const paramsXml = `<START_CODING_TASK> |
| 119 | + <repo>https://github.com/org/repo</repo> |
| 120 | + <agents>claude:Fix auth | codex:Write tests</agents> |
| 121 | + <task>Fix the login bug</task> |
| 122 | + </START_CODING_TASK>`; |
| 123 | + |
| 124 | + const result = parseActionParams(paramsXml); |
| 125 | + expect(result.has("START_CODING_TASK")).toBe(true); |
| 126 | + |
| 127 | + const params = result.get("START_CODING_TASK"); |
| 128 | + expect(params).toBeTruthy(); |
| 129 | + expect(params?.repo).toBe("https://github.com/org/repo"); |
| 130 | + expect(params?.agents).toBe("claude:Fix auth | codex:Write tests"); |
| 131 | + expect(params?.task).toBe("Fix the login bug"); |
| 132 | + }); |
| 133 | + |
| 134 | + it("handles multiple action blocks", () => { |
| 135 | + const paramsXml = `<START_CODING_TASK> |
| 136 | + <repo>https://github.com/org/repo</repo> |
| 137 | + <task>Fix bugs</task> |
| 138 | + </START_CODING_TASK> |
| 139 | + <FINALIZE_WORKSPACE> |
| 140 | + <workspaceId>ws-123</workspaceId> |
| 141 | + </FINALIZE_WORKSPACE>`; |
| 142 | + |
| 143 | + const result = parseActionParams(paramsXml); |
| 144 | + expect(result.has("START_CODING_TASK")).toBe(true); |
| 145 | + expect(result.has("FINALIZE_WORKSPACE")).toBe(true); |
| 146 | + expect(result.get("START_CODING_TASK")?.task).toBe("Fix bugs"); |
| 147 | + expect(result.get("FINALIZE_WORKSPACE")?.workspaceId).toBe("ws-123"); |
| 148 | + }); |
| 149 | + |
| 150 | + it("returns empty map for empty input", () => { |
| 151 | + expect(parseActionParams("").size).toBe(0); |
| 152 | + expect(parseActionParams(undefined).size).toBe(0); |
| 153 | + expect(parseActionParams(null).size).toBe(0); |
| 154 | + }); |
| 155 | +}); |
0 commit comments