|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { contentToXml, type EditorContent, xmlToContent } from "./content"; |
| 3 | + |
| 4 | +describe("xmlToContent", () => { |
| 5 | + it("parses a file tag into a file chip", () => { |
| 6 | + const result = xmlToContent('<file path="src/foo/bar.ts" />'); |
| 7 | + expect(result).toEqual({ |
| 8 | + segments: [ |
| 9 | + { |
| 10 | + type: "chip", |
| 11 | + chip: { type: "file", id: "src/foo/bar.ts", label: "foo/bar.ts" }, |
| 12 | + }, |
| 13 | + ], |
| 14 | + }); |
| 15 | + }); |
| 16 | + |
| 17 | + it("derives file label from the final path segment when no parent", () => { |
| 18 | + const result = xmlToContent('<file path="README.md" />'); |
| 19 | + expect(result.segments).toEqual([ |
| 20 | + { |
| 21 | + type: "chip", |
| 22 | + chip: { type: "file", id: "README.md", label: "README.md" }, |
| 23 | + }, |
| 24 | + ]); |
| 25 | + }); |
| 26 | + |
| 27 | + it("unescapes XML attributes", () => { |
| 28 | + const result = xmlToContent('<file path="a/"weird".ts" />'); |
| 29 | + const segment = result.segments[0]; |
| 30 | + expect(segment.type).toBe("chip"); |
| 31 | + if (segment.type === "chip") { |
| 32 | + expect(segment.chip.id).toBe('a/"weird".ts'); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + it("parses github_issue tags with title", () => { |
| 37 | + const xml = |
| 38 | + '<github_issue number="42" title="Fix bug" url="https://github.com/org/repo/issues/42" />'; |
| 39 | + expect(xmlToContent(xml).segments).toEqual([ |
| 40 | + { |
| 41 | + type: "chip", |
| 42 | + chip: { |
| 43 | + type: "github_issue", |
| 44 | + id: "https://github.com/org/repo/issues/42", |
| 45 | + label: "#42 - Fix bug", |
| 46 | + }, |
| 47 | + }, |
| 48 | + ]); |
| 49 | + }); |
| 50 | + |
| 51 | + it("parses github_issue tags without title", () => { |
| 52 | + const xml = |
| 53 | + '<github_issue number="7" url="https://github.com/org/repo/issues/7" />'; |
| 54 | + const segment = xmlToContent(xml).segments[0]; |
| 55 | + expect(segment.type).toBe("chip"); |
| 56 | + if (segment.type === "chip") { |
| 57 | + expect(segment.chip.label).toBe("#7"); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + it.each([ |
| 62 | + ["error", "err-1"], |
| 63 | + ["experiment", "exp-1"], |
| 64 | + ["insight", "ins-1"], |
| 65 | + ["feature_flag", "flag-1"], |
| 66 | + ])("parses %s tag into a chip with id as label", (type, id) => { |
| 67 | + const xml = `<${type} id="${id}" />`; |
| 68 | + expect(xmlToContent(xml).segments).toEqual([ |
| 69 | + { type: "chip", chip: { type, id, label: id } }, |
| 70 | + ]); |
| 71 | + }); |
| 72 | + |
| 73 | + it("preserves surrounding text around chips", () => { |
| 74 | + const result = xmlToContent( |
| 75 | + 'please review <file path="src/a.ts" /> and <file path="src/b.ts" />', |
| 76 | + ); |
| 77 | + expect(result.segments).toEqual([ |
| 78 | + { type: "text", text: "please review " }, |
| 79 | + { |
| 80 | + type: "chip", |
| 81 | + chip: { type: "file", id: "src/a.ts", label: "src/a.ts" }, |
| 82 | + }, |
| 83 | + { type: "text", text: " and " }, |
| 84 | + { |
| 85 | + type: "chip", |
| 86 | + chip: { type: "file", id: "src/b.ts", label: "src/b.ts" }, |
| 87 | + }, |
| 88 | + ]); |
| 89 | + }); |
| 90 | + |
| 91 | + it("returns a single text segment when no tags are present", () => { |
| 92 | + expect(xmlToContent("just plain text").segments).toEqual([ |
| 93 | + { type: "text", text: "just plain text" }, |
| 94 | + ]); |
| 95 | + }); |
| 96 | + |
| 97 | + it("returns a single text segment for empty input", () => { |
| 98 | + expect(xmlToContent("").segments).toEqual([{ type: "text", text: "" }]); |
| 99 | + }); |
| 100 | + |
| 101 | + it("round-trips contentToXml for a mix of text and chips", () => { |
| 102 | + const content: EditorContent = { |
| 103 | + segments: [ |
| 104 | + { type: "text", text: "look at " }, |
| 105 | + { |
| 106 | + type: "chip", |
| 107 | + chip: { type: "file", id: "apps/code/src/a.ts", label: "src/a.ts" }, |
| 108 | + }, |
| 109 | + { type: "text", text: " and " }, |
| 110 | + { |
| 111 | + type: "chip", |
| 112 | + chip: { |
| 113 | + type: "github_issue", |
| 114 | + id: "https://github.com/org/repo/issues/9", |
| 115 | + label: "#9 - Thing", |
| 116 | + }, |
| 117 | + }, |
| 118 | + ], |
| 119 | + }; |
| 120 | + |
| 121 | + const xml = contentToXml(content); |
| 122 | + const parsed = xmlToContent(xml); |
| 123 | + expect(parsed.segments).toEqual([ |
| 124 | + { type: "text", text: "look at " }, |
| 125 | + { |
| 126 | + type: "chip", |
| 127 | + chip: { type: "file", id: "apps/code/src/a.ts", label: "src/a.ts" }, |
| 128 | + }, |
| 129 | + { type: "text", text: " and " }, |
| 130 | + { |
| 131 | + type: "chip", |
| 132 | + chip: { |
| 133 | + type: "github_issue", |
| 134 | + id: "https://github.com/org/repo/issues/9", |
| 135 | + label: "#9 - Thing", |
| 136 | + }, |
| 137 | + }, |
| 138 | + ]); |
| 139 | + }); |
| 140 | +}); |
0 commit comments