Open History to see the last commit
1import remarkParse from "remark-parse";
2import { unified } from "unified";
3import { expect, it } from "vite-plus/test";
4import { proposedPlanTitle, stripDisplayedPlanMarkdown } from "./proposedPlanText.ts";
5
6it("takes the plan title from headings outside code", () => {
7 for (const [open, inner, close] of [
8 ["```bash", "~~~", "```"],
9 ["~~~bash", "```", "~~~"],
10 ["````bash", "```", "````"],
11 ["```html", "<!--", "```"],
12 ["```md", "```\u00a0", "``` \t"],
13 ["```md\u2028", "example", "```"],
14 ]) {
15 const code = `${open}\n# Back up database\n${inner}\n# Still code\n${close}\n`;
16 expect(proposedPlanTitle(`${code}\n## Real Plan Title\n`)).toBe("Real Plan Title");
17 }
18 expect(proposedPlanTitle("```bash\n# Back up database\n")).toBeNull();
19 expect(proposedPlanTitle("\t# Back up database\n\n## Real Plan Title")).toBe("Real Plan Title");
20});
21
22it("takes the plan title from headings outside HTML comments", () => {
23 for (const comment of ["<!--\n```ts\n-->", "<!--\n# Hidden title\n-->", " <!-- # Hidden -->"]) {
24 expect(proposedPlanTitle(`${comment}\n# Release plan`)).toBe("Release plan");
25 expect(stripDisplayedPlanMarkdown(`${comment}\n# Release plan\n\n## Summary\n\nBody`)).toBe(
26 "Body",
27 );
28 }
29 expect(proposedPlanTitle("<!--\n# Hidden title\n# Release plan")).toBeNull();
30 expect(stripDisplayedPlanMarkdown("<!--\n# Hidden title\n# Release plan")).toBe("");
31});
32
33it("keeps text on a comment's closing line while removing the plan title", () => {
34 for (const [preamble, titleBlock, displayedPreamble = preamble] of [
35 ["<!-- notes --> note", "# Release plan\n"],
36 ["<!--\n# Hidden title\n--> note", "\n#\n# Release plan\n\n## Summary\n"],
37 [
38 "<!-- first --> note\n#\n<!-- second --> more",
39 "# Release plan\n",
40 "<!-- first --> note\n<!-- second --> more",
41 ],
42 ]) {
43 const markdown = `${preamble}\n${titleBlock}\nBody`;
44 expect(proposedPlanTitle(markdown)).toBe("Release plan");
45 expect(stripDisplayedPlanMarkdown(markdown)).toBe(`${displayedPreamble}\n\nBody`);
46 }
47});
48
49it("skips empty plan headings without reading into the next line", () => {
50 for (const [heading, title] of [
51 ["# \r\n###\t\r\n## ###\r\n## Real Title", "Real Title"],
52 ["## # #", "#"],
53 ["## ###\t###", "###"],
54 ]) {
55 const markdown = `${heading}\r\n\r\n## Summary\r\n\r\nBody text`;
56 expect(proposedPlanTitle(markdown)).toBe(title);
57 expect(stripDisplayedPlanMarkdown(markdown)).toBe("Body text");
58 }
59});
60
61it("keeps plan titles that contain a Unicode line separator", () => {
62 for (const separator of ["\u2028", "\u2029"]) {
63 const markdown = `# Rollout${separator}check\n\n## Deploy\n\nBody`;
64 expect(proposedPlanTitle(markdown)).toBe(`Rollout${separator}check`);
65 expect(stripDisplayedPlanMarkdown(markdown)).toBe("## Deploy\n\nBody");
66 }
67});
68
69it("strips the displayed plan title and summary after leading blank lines", () => {
70 const markdown = "\r\n# My Plan\r\n\r\n## Summary\r\n\r\nBody text";
71 expect(proposedPlanTitle(markdown)).toBe("My Plan");
72 expect(stripDisplayedPlanMarkdown(markdown)).toBe("Body text");
73});
74
75it("reads the plan title past a leading byte-order mark", () => {
76 for (const markdown of [
77 "\uFEFF# Rollout\n\nDeploy",
78 "\uFEFF # Rollout\r\n\r\n## Summary\r\n\r\nDeploy",
79 ]) {
80 expect(proposedPlanTitle(markdown)).toBe("Rollout");
81 expect(stripDisplayedPlanMarkdown(markdown)).toBe("Deploy");
82 }
83 expect(proposedPlanTitle("\uFEFF # Indented code")).toBeNull();
84});
85
86const parseDisplayedPlan = (markdown: string) =>
87 unified().use(remarkParse).parse(stripDisplayedPlanMarkdown(markdown));
88
89it("strips the summary after a title that follows a code example", () => {
90 const code = "```sh\nnode x\n```";
91 const markdown = `${code}\n\n# Release plan\n\n## Summary\n\nBody`;
92 const body = parseDisplayedPlan(markdown);
93 expect(body.children.filter((node) => node.type !== "definition")).toMatchObject([
94 { type: "code", lang: "sh", value: "node x" },
95 { type: "paragraph", children: [{ type: "text", value: "Body" }] },
96 ]);
97});
98
99it("keeps trailing spaces on the last code line before the plan title", () => {
100 const code = "Example:\r\n\r\n value \r\n\r\n# Rollout";
101 expect(stripDisplayedPlanMarkdown(code)).toBe("Example:\n\n value ");
102 expect(stripDisplayedPlanMarkdown(`${code}\r\n\r\nDeploy.`)).toBe(
103 "Example:\n\n value \n\n[0]: <>\n\nDeploy.",
104 );
105});
106
107it("keeps the ordered lists on either side of the plan title separate", () => {
108 const markdown = "```sh\nnode x\n```\n\n1. Check staging\n\n# Release plan\n\n1. Deploy";
109 const body = parseDisplayedPlan(markdown);
110 const lists = body.children.filter((node) => node.type === "list");
111 expect(lists.map((list) => [list.ordered, list.start, list.children.length])).toEqual([
112 [true, 1, 1],
113 [true, 1, 1],
114 ]);
115});
116
117it("keeps headings inside list items and uses only the top-level heading as the title", () => {
118 const list = "- Review the example\n\n ```sh\n node x\n ```\n\n ## Expected output";
119 const markdown = `${list}\n\n# Release plan`;
120 expect(proposedPlanTitle(markdown)).toBe("Release plan");
121 expect(stripDisplayedPlanMarkdown(markdown)).toBe(list);
122});
123
124it("reads an underlined heading as the plan title", () => {
125 for (const [markdown, title, body] of [
126 ["Rollout\r\n=======\r\n\r\n# Deploy\r\n\r\nRun", "Rollout", "# Deploy\n\nRun"],
127 ["Release\nplan\n---\n\n## Summary\n\nBody", "Release plan", "Body"],
128 ] as const) {
129 expect(proposedPlanTitle(markdown)).toBe(title);
130 expect(stripDisplayedPlanMarkdown(markdown)).toBe(body);
131 }
132 expect(proposedPlanTitle("\u00a0\n\u00a0\n---\n\n# Real")).toBe("Real");
133 expect(proposedPlanTitle("Run the checks.\n---\u00a0\n")).toBeNull();
134});
135
136it.each([
137 ["# Release plan\r\n\r\n sUmMaRy\r\n ---\r\n\r\n# Deploy\r\n\r\nRun", "# Deploy\n\nRun"],
138 ["Release plan\n============\n\nSummary\n=======", ""],
139])("strips an underlined Summary immediately after the plan title in %j", (plan, body) => {
140 expect(stripDisplayedPlanMarkdown(plan)).toBe(body);
141});