import { parseCommandXml } from "./parseCommandXml"; describe("parseCommandXml", () => { describe("command parsing", () => { it("parses command-name only", () => { const input = "git status"; const result = parseCommandXml(input); expect(result).toEqual({ kind: "command", commandName: "git status", commandArgs: undefined, commandMessage: undefined, }); }); it("parses command-name with command-args", () => { const input = "git commit-m 'test'"; const result = parseCommandXml(input); expect(result).toEqual({ kind: "command", commandName: "git commit", commandArgs: "-m 'test'", commandMessage: undefined, }); }); it("parses command-name with command-message", () => { const input = "lsListing files"; const result = parseCommandXml(input); expect(result).toEqual({ kind: "command", commandName: "ls", commandArgs: undefined, commandMessage: "Listing files", }); }); it("parses all command tags together", () => { const input = "npm install--save-dev vitestInstalling test dependencies"; const result = parseCommandXml(input); expect(result).toEqual({ kind: "command", commandName: "npm install", commandArgs: "--save-dev vitest", commandMessage: "Installing test dependencies", }); }); it("parses command tags with whitespace in content", () => { const input = "\n git status \n --short "; const result = parseCommandXml(input); expect(result).toEqual({ kind: "command", commandName: "\n git status \n", commandArgs: " --short ", commandMessage: undefined, }); }); it("parses command tags in different order", () => { const input = "Test message-vtest command"; const result = parseCommandXml(input); expect(result).toEqual({ kind: "command", commandName: "test command", commandArgs: "-v", commandMessage: "Test message", }); }); }); describe("local-command parsing", () => { it("parses local-command-stdout", () => { const input = "output text"; const result = parseCommandXml(input); expect(result).toEqual({ kind: "local-command", stdout: "output text", }); }); it("parses local-command-stdout with multiline content", () => { const input = "line1\nline2\nline3"; const result = parseCommandXml(input); expect(result).toEqual({ kind: "local-command", stdout: "line1\nline2\nline3", }); }); it("parses local-command-stdout with whitespace", () => { const input = " \n output with spaces \n "; const result = parseCommandXml(input); // The regex pattern preserves all whitespace in content expect(result).toEqual({ kind: "local-command", stdout: " \n output with spaces \n ", }); }); }); describe("priority: command over local-command", () => { it("returns command when both command and local-command tags exist", () => { const input = "testoutput"; const result = parseCommandXml(input); expect(result.kind).toBe("command"); if (result.kind === "command") { expect(result.commandName).toBe("test"); } }); }); describe("fallback to text", () => { it("returns text when no matching tags found", () => { const input = "just plain text"; const result = parseCommandXml(input); expect(result).toEqual({ kind: "text", content: "just plain text", }); }); it("returns text when tags are not closed properly", () => { const input = "incomplete"; const result = parseCommandXml(input); expect(result).toEqual({ kind: "text", content: "incomplete", }); }); it("returns text when tags are mismatched", () => { const input = "test"; const result = parseCommandXml(input); expect(result).toEqual({ kind: "text", content: "test", }); }); it("returns text with empty string", () => { const input = ""; const result = parseCommandXml(input); expect(result).toEqual({ kind: "text", content: "", }); }); it("returns text with only unrecognized tags", () => { const input = "content"; const result = parseCommandXml(input); expect(result).toEqual({ kind: "text", content: "content", }); }); }); describe("edge cases", () => { it("handles multiple same tags (uses first match)", () => { const input = "firstsecond"; const result = parseCommandXml(input); expect(result.kind).toBe("command"); if (result.kind === "command") { expect(result.commandName).toBe("first"); } }); it("handles empty tag content", () => { const input = ""; const result = parseCommandXml(input); expect(result).toEqual({ kind: "command", commandName: "", commandArgs: undefined, commandMessage: undefined, }); }); it("handles tags with special characters in content", () => { const input = "git commit -m 'test & demo'"; const result = parseCommandXml(input); expect(result.kind).toBe("command"); if (result.kind === "command") { expect(result.commandName).toBe("git commit -m 'test & demo'"); } }); it("does not match nested tags (regex limitation)", () => { const input = "innerouter"; const result = parseCommandXml(input); // The regex won't match properly nested tags due to [^<]* pattern expect(result.kind).toBe("text"); }); it("handles tags with surrounding text", () => { const input = "Some text before test and after"; const result = parseCommandXml(input); expect(result).toEqual({ kind: "command", commandName: "test", commandArgs: undefined, commandMessage: undefined, }); }); it("handles newlines between tags", () => { const input = "test\n\narg"; const result = parseCommandXml(input); expect(result).toEqual({ kind: "command", commandName: "test", commandArgs: "arg", commandMessage: undefined, }); }); it("handles very long content", () => { const longContent = "x".repeat(10000); const input = `${longContent}`; const result = parseCommandXml(input); expect(result.kind).toBe("command"); if (result.kind === "command") { expect(result.commandName).toBe(longContent); } }); it("handles tags with attributes (not matched)", () => { const input = 'test'; const result = parseCommandXml(input); // Tags with attributes won't match because regex expects not expect(result.kind).toBe("text"); }); it("handles self-closing tags (not matched)", () => { const input = ""; const result = parseCommandXml(input); expect(result.kind).toBe("text"); }); it("handles Unicode content", () => { const input = "テスト コマンド 🚀"; const result = parseCommandXml(input); expect(result).toEqual({ kind: "command", commandName: "テスト コマンド 🚀", commandArgs: undefined, commandMessage: undefined, }); }); it("handles mixed content with multiple tag types", () => { const input = "Some text cmd more text tag"; const result = parseCommandXml(input); expect(result.kind).toBe("command"); if (result.kind === "command") { expect(result.commandName).toBe("cmd"); } }); }); });