@andares/pi-toolkits 0.12.4 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +100 -15
- package/README.zh-CN.md +94 -14
- package/package.json +19 -10
- package/src/features/ask/index.ts +6 -0
- package/src/features/init/apply.ts +32 -12
- package/src/features/init/audit-uat.test.ts +103 -0
- package/src/features/init/audit.ts +148 -0
- package/src/features/init/detect.ts +4 -0
- package/src/features/init/index.ts +47 -2
- package/src/features/init/plan.ts +11 -0
- package/src/features/init/refresh.test.ts +208 -0
- package/src/features/init/refresh.ts +255 -0
- package/src/features/init/spec.ts +20 -0
- package/src/features/init/templates/agents-skeleton.md +2 -0
- package/src/features/init/templates/uat-readme.md +53 -0
- package/src/features/init/templates/workflow-block.md +21 -0
- package/src/features/init/templates.test.ts +3 -1
- package/src/features/init/templates.ts +2 -0
- package/src/features/init/types.ts +12 -1
- package/src/features/permission/index.ts +32 -0
- package/src/features/plan/assets/index-C1YLIi4I.js +1 -0
- package/src/features/plan/assets/index-DMEpYC3V.css +1 -0
- package/src/features/plan/assets/index.html +14 -0
- package/src/features/plan/assets.test.ts +64 -0
- package/src/features/plan/blocks.test.ts +77 -0
- package/src/features/plan/blocks.ts +153 -0
- package/src/features/plan/diff.test.ts +144 -0
- package/src/features/plan/diff.ts +193 -0
- package/src/features/plan/feedback.test.ts +233 -0
- package/src/features/plan/feedback.ts +182 -0
- package/src/features/plan/index.test.ts +385 -0
- package/src/features/plan/index.ts +433 -0
- package/src/features/plan/prompt.test.ts +91 -0
- package/src/features/plan/prompt.ts +110 -0
- package/src/features/plan/render.test.ts +94 -0
- package/src/features/plan/render.ts +131 -0
- package/src/features/plan/server.test.ts +374 -0
- package/src/features/plan/server.ts +272 -0
- package/src/features/plan/store.test.ts +145 -0
- package/src/features/plan/store.ts +244 -0
- package/src/features/review/index.ts +54 -28
- package/src/features/review/review.test.ts +46 -1
- package/src/features/uat/assets/index-D-W9zj0w.css +1 -0
- package/src/features/uat/assets/index-DCBQCsli.js +1 -0
- package/src/features/uat/assets/index.html +14 -0
- package/src/features/uat/constants.ts +23 -0
- package/src/features/uat/index.test.ts +404 -0
- package/src/features/uat/index.ts +329 -0
- package/src/features/uat/item-tool.ts +280 -0
- package/src/features/uat/ledger.test.ts +249 -0
- package/src/features/uat/ledger.ts +504 -0
- package/src/features/uat/prompt.ts +78 -0
- package/src/features/uat/record-tool.ts +167 -0
- package/src/features/uat/records.test.ts +145 -0
- package/src/features/uat/records.ts +268 -0
- package/src/features/uat/server.ts +181 -0
- package/src/features/uat/state.test.ts +41 -0
- package/src/features/uat/state.ts +67 -0
- package/src/index.ts +10 -1
- package/src/lib/ask-control.ts +25 -0
- package/src/lib/opener.test.ts +72 -0
- package/src/lib/opener.ts +121 -0
- package/src/lib/permission-control.ts +39 -0
- package/src/lib/startup.test.ts +1 -1
- package/src/lib/web-review.ts +341 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { parseBlocks } from "./blocks.js";
|
|
3
|
+
import { escapeHtml, renderBlockHtml } from "./render.js";
|
|
4
|
+
|
|
5
|
+
const render = (markdown: string): string => {
|
|
6
|
+
const [block] = parseBlocks(markdown);
|
|
7
|
+
if (!block) throw new Error(`no block parsed from: ${markdown}`);
|
|
8
|
+
return renderBlockHtml(block);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
describe("escapeHtml", () => {
|
|
12
|
+
it("escapes the five entities", () => {
|
|
13
|
+
expect(escapeHtml(`<a href="x" title='y'>&`)).toBe(
|
|
14
|
+
"<a href="x" title='y'>&",
|
|
15
|
+
);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe("renderBlockHtml", () => {
|
|
20
|
+
it("never passes raw HTML through", () => {
|
|
21
|
+
const html = render("<script>alert(1)</script>");
|
|
22
|
+
expect(html).toBe("<p><script>alert(1)</script></p>");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("renders inline code, bold, italic and links", () => {
|
|
26
|
+
const html = render("use `plan_submit` and **never** *skip* [the spec](https://example.com/x)");
|
|
27
|
+
expect(html).toContain("<code>plan_submit</code>");
|
|
28
|
+
expect(html).toContain("<strong>never</strong>");
|
|
29
|
+
expect(html).toContain("<em>skip</em>");
|
|
30
|
+
expect(html).toContain('href="https://example.com/x"');
|
|
31
|
+
expect(html).toContain('rel="noreferrer noopener"');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("keeps HTML escaped inside inline code", () => {
|
|
35
|
+
expect(render("`<b>`")).toBe("<p><code><b></code></p>");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("renders headings at their level", () => {
|
|
39
|
+
expect(render("### Step three")).toBe("<h3>Step three</h3>");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("strips closing hashes from a heading", () => {
|
|
43
|
+
expect(render("## Title ##")).toBe("<h2>Title</h2>");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("renders a code fence verbatim with its language", () => {
|
|
47
|
+
const html = render("```ts\nconst a = 1 < 2;\n\nif (a) run();\n```");
|
|
48
|
+
expect(html).toBe(
|
|
49
|
+
'<pre><code class="language-ts">const a = 1 < 2;\n\nif (a) run();</code></pre>',
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("keeps the last line of an unclosed code fence", () => {
|
|
54
|
+
const html = render("```ts\nconst a = 1;\nconst b = 2;");
|
|
55
|
+
expect(html).toBe(
|
|
56
|
+
'<pre><code class="language-ts">const a = 1;\nconst b = 2;</code></pre>',
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("does not close a fence on a line that has an info string", () => {
|
|
61
|
+
expect(render("```\nfirst\n```ts\nlast")).toBe(
|
|
62
|
+
"<pre><code>first\n```ts\nlast</code></pre>",
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("does not run inline markup inside code", () => {
|
|
67
|
+
expect(render("```\n**not bold** <tag>\n```")).toBe(
|
|
68
|
+
"<pre><code>**not bold** <tag></code></pre>",
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("renders unordered and ordered lists", () => {
|
|
73
|
+
expect(render("- one\n- two")).toBe("<ul><li>one</li><li>two</li></ul>");
|
|
74
|
+
expect(render("1. one\n2. two")).toBe("<ol><li>one</li><li>two</li></ol>");
|
|
75
|
+
// a continuation line belongs to the item above it
|
|
76
|
+
expect(render("- one\n continued")).toBe("<ul><li>one continued</li></ul>");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("renders block quotes with their marker stripped", () => {
|
|
80
|
+
expect(render("> quoted text")).toBe("<blockquote><p>quoted text</p></blockquote>");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("renders a table with a header and body", () => {
|
|
84
|
+
const html = render("| a | b |\n| --- | --- |\n| 1 | 2 |");
|
|
85
|
+
expect(html).toBe(
|
|
86
|
+
"<table><thead><tr><th>a</th><th>b</th></tr></thead>" +
|
|
87
|
+
"<tbody><tr><td>1</td><td>2</td></tr></tbody></table>",
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("joins prose lines with <br />", () => {
|
|
92
|
+
expect(render("first line\nsecond line")).toBe("<p>first line<br />second line</p>");
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* plan block rendering — markdown subset → HTML, server side.
|
|
3
|
+
*
|
|
4
|
+
* Why not a markdown library: the UI only needs to show prose, headings, lists,
|
|
5
|
+
* code and tables of a document our own agent wrote, and a dependency here
|
|
6
|
+
* would ship a parser (and its transitive surface) to every user of the
|
|
7
|
+
* extension. Why not render in the browser: then the escaping rules live in a
|
|
8
|
+
* second implementation that the test suite cannot reach.
|
|
9
|
+
*
|
|
10
|
+
* Everything is escaped first; the transformations below only ever insert
|
|
11
|
+
* markup, so a plan that contains `<script>` renders as text. That is the whole
|
|
12
|
+
* security model for this path — no raw HTML passthrough, by design.
|
|
13
|
+
*/
|
|
14
|
+
import type { Block } from "./blocks.js";
|
|
15
|
+
|
|
16
|
+
export function escapeHtml(text: string): string {
|
|
17
|
+
return text
|
|
18
|
+
.replace(/&/g, "&")
|
|
19
|
+
.replace(/</g, "<")
|
|
20
|
+
.replace(/>/g, ">")
|
|
21
|
+
.replace(/"/g, """)
|
|
22
|
+
.replace(/'/g, "'");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Inline markup: code spans, bold, italic, links. Input is NOT escaped here. */
|
|
26
|
+
function renderInline(escaped: string): string {
|
|
27
|
+
return escaped
|
|
28
|
+
.replace(/`([^`]+)`/g, "<code>$1</code>")
|
|
29
|
+
.replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>")
|
|
30
|
+
.replace(/(^|[\s(])\*([^*\s][^*]*)\*/g, "$1<em>$2</em>")
|
|
31
|
+
.replace(
|
|
32
|
+
/\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g,
|
|
33
|
+
'<a href="$2" rel="noreferrer noopener" target="_blank">$1</a>',
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function inline(text: string): string {
|
|
38
|
+
return renderInline(escapeHtml(text));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function renderHeading(lines: readonly string[]): string {
|
|
42
|
+
const match = /^ {0,3}(#{1,6})\s+(.*)$/.exec(lines[0] ?? "");
|
|
43
|
+
const level = match ? match[1].length : 1;
|
|
44
|
+
const title = match ? match[2].replace(/\s+#+\s*$/, "") : (lines[0] ?? "");
|
|
45
|
+
return `<h${level}>${inline(title)}</h${level}>`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function renderQuote(lines: readonly string[]): string {
|
|
49
|
+
const body = lines.map((line) => line.replace(/^ {0,3}>\s?/, "")).join("\n");
|
|
50
|
+
return `<blockquote>${body
|
|
51
|
+
.split("\n")
|
|
52
|
+
.map((line) => `<p>${inline(line)}</p>`)
|
|
53
|
+
.join("")}</blockquote>`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function renderList(lines: readonly string[]): string {
|
|
57
|
+
const ordered = /^ {0,3}\d{1,9}[.)]\s/.test(lines[0] ?? "");
|
|
58
|
+
const items: string[] = [];
|
|
59
|
+
for (const line of lines) {
|
|
60
|
+
const bullet = /^ {0,3}(?:[-*+]|\d{1,9}[.)])\s+(.*)$/.exec(line);
|
|
61
|
+
if (bullet) {
|
|
62
|
+
items.push(bullet[1]);
|
|
63
|
+
} else if (items.length > 0) {
|
|
64
|
+
items[items.length - 1] += ` ${line.trim()}`;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const tag = ordered ? "ol" : "ul";
|
|
68
|
+
const body = items.map((item) => `<li>${inline(item)}</li>`).join("");
|
|
69
|
+
return `<${tag}>${body}</${tag}>`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function renderCode(lines: readonly string[]): string {
|
|
73
|
+
const first = lines[0] ?? "";
|
|
74
|
+
const opener = /^ {0,3}(`{3,}|~{3,})\s*([\w+#.-]*)/.exec(first);
|
|
75
|
+
const language = opener?.[2] ? ` class="language-${escapeHtml(opener[2])}"` : "";
|
|
76
|
+
const marker = opener?.[1];
|
|
77
|
+
const last = lines.at(-1) ?? "";
|
|
78
|
+
const closing = /^ {0,3}(`{3,}|~{3,})\s*$/.exec(last)?.[1];
|
|
79
|
+
const closed =
|
|
80
|
+
lines.length > 1 &&
|
|
81
|
+
marker !== undefined &&
|
|
82
|
+
closing !== undefined &&
|
|
83
|
+
closing[0] === marker[0] &&
|
|
84
|
+
closing.length >= marker.length;
|
|
85
|
+
// An unfinished fence is legal markdown while the agent is drafting. Its
|
|
86
|
+
// final line is content, not a closing delimiter, so do not silently drop it.
|
|
87
|
+
const body = lines.slice(1, closed ? -1 : undefined).join("\n");
|
|
88
|
+
return `<pre><code${language}>${escapeHtml(body)}</code></pre>`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function splitRow(line: string): string[] {
|
|
92
|
+
const trimmed = line.trim().replace(/^\|/, "").replace(/\|$/, "");
|
|
93
|
+
return trimmed.split("|").map((cell) => cell.trim());
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function renderTable(lines: readonly string[]): string {
|
|
97
|
+
const rows = lines.filter((line) => !/^ {0,3}\|?[\s:-]*-{3,}[\s:|-]*$/.test(line));
|
|
98
|
+
const [head, ...body] = rows.map(splitRow);
|
|
99
|
+
const headHtml = head
|
|
100
|
+
? `<thead><tr>${head.map((cell) => `<th>${inline(cell)}</th>`).join("")}</tr></thead>`
|
|
101
|
+
: "";
|
|
102
|
+
const bodyHtml = body.length
|
|
103
|
+
? `<tbody>${body
|
|
104
|
+
.map((row) => `<tr>${row.map((cell) => `<td>${inline(cell)}</td>`).join("")}</tr>`)
|
|
105
|
+
.join("")}</tbody>`
|
|
106
|
+
: "";
|
|
107
|
+
return `<table>${headHtml}${bodyHtml}</table>`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function renderText(lines: readonly string[]): string {
|
|
111
|
+
return `<p>${lines.map((line) => inline(line)).join("<br />")}</p>`;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Render one block to HTML. Unknown kinds fall back to prose. */
|
|
115
|
+
export function renderBlockHtml(block: Pick<Block, "kind" | "text">): string {
|
|
116
|
+
const lines = block.text.split("\n");
|
|
117
|
+
switch (block.kind) {
|
|
118
|
+
case "heading":
|
|
119
|
+
return renderHeading(lines);
|
|
120
|
+
case "quote":
|
|
121
|
+
return renderQuote(lines);
|
|
122
|
+
case "list":
|
|
123
|
+
return renderList(lines);
|
|
124
|
+
case "code":
|
|
125
|
+
return renderCode(lines);
|
|
126
|
+
case "table":
|
|
127
|
+
return renderTable(lines);
|
|
128
|
+
default:
|
|
129
|
+
return renderText(lines);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
2
|
+
import { request } from "node:http";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
|
6
|
+
import { parseBlocks } from "./blocks.js";
|
|
7
|
+
import { renderBlockHtml } from "./render.js";
|
|
8
|
+
import { buildPlanState, normalizeAnnotations, startPlanServer, type PlanServerHandle } from "./server.js";
|
|
9
|
+
import { appendPlanVersion, createPlanSession, type PlanSession } from "./store.js";
|
|
10
|
+
|
|
11
|
+
let agentDir: string;
|
|
12
|
+
|
|
13
|
+
beforeAll(() => {
|
|
14
|
+
agentDir = mkdtempSync(join(tmpdir(), "pi-plan-server-"));
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
afterAll(() => {
|
|
18
|
+
rmSync(agentDir, { recursive: true, force: true });
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
function makeSession(text = "## Plan\n\nfirst step"): PlanSession {
|
|
22
|
+
return createPlanSession({
|
|
23
|
+
agentDir,
|
|
24
|
+
slug: `srv-${Math.random().toString(36).slice(2, 8)}`,
|
|
25
|
+
text,
|
|
26
|
+
source: "specs/x.md",
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function serve(
|
|
31
|
+
session: PlanSession,
|
|
32
|
+
overrides: Partial<Parameters<typeof startPlanServer>[0]> = {},
|
|
33
|
+
): Promise<PlanServerHandle> {
|
|
34
|
+
return startPlanServer({
|
|
35
|
+
session,
|
|
36
|
+
assetsDir: join(agentDir, "no-assets"),
|
|
37
|
+
render: renderBlockHtml,
|
|
38
|
+
portStart: 42_000 + Math.floor(Math.random() * 2000),
|
|
39
|
+
portAttempts: 20,
|
|
40
|
+
bindHost: "127.0.0.1",
|
|
41
|
+
urlHost: "127.0.0.1",
|
|
42
|
+
closeDelayMs: 0,
|
|
43
|
+
...overrides,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface CallOptions {
|
|
48
|
+
port: number;
|
|
49
|
+
path: string;
|
|
50
|
+
method?: string;
|
|
51
|
+
token?: string;
|
|
52
|
+
headers?: Record<string, string>;
|
|
53
|
+
body?: unknown;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface CallResult {
|
|
57
|
+
status: number;
|
|
58
|
+
text: string;
|
|
59
|
+
json: () => unknown;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Talk to the review server the way the page does (plain node:http, no client lib). */
|
|
63
|
+
async function call(options: CallOptions): Promise<CallResult> {
|
|
64
|
+
return new Promise((resolve, reject) => {
|
|
65
|
+
const req = request(
|
|
66
|
+
{
|
|
67
|
+
host: "127.0.0.1",
|
|
68
|
+
port: options.port,
|
|
69
|
+
method: options.method ?? "GET",
|
|
70
|
+
path: options.path,
|
|
71
|
+
headers: {
|
|
72
|
+
...(options.token ? { "x-plan-token": options.token } : {}),
|
|
73
|
+
...(options.body !== undefined ? { "content-type": "application/json" } : {}),
|
|
74
|
+
...options.headers,
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
(res) => {
|
|
78
|
+
const chunks: Buffer[] = [];
|
|
79
|
+
res.on("data", (chunk: Buffer) => chunks.push(chunk));
|
|
80
|
+
res.on("end", () => {
|
|
81
|
+
const text = Buffer.concat(chunks).toString("utf8");
|
|
82
|
+
resolve({ status: res.statusCode ?? 0, text, json: () => JSON.parse(text) as unknown });
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
);
|
|
86
|
+
req.on("error", reject);
|
|
87
|
+
if (options.body !== undefined) req.write(JSON.stringify(options.body));
|
|
88
|
+
req.end();
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Collect SSE chunks until a marker shows up (or the test times out). */
|
|
93
|
+
function openStream(port: number, path: string): {
|
|
94
|
+
until: (marker: string) => Promise<string>;
|
|
95
|
+
close: () => void;
|
|
96
|
+
} {
|
|
97
|
+
const chunks: Buffer[] = [];
|
|
98
|
+
let notify: (() => void) | undefined;
|
|
99
|
+
const req = request({ host: "127.0.0.1", port, path }, (res) => {
|
|
100
|
+
res.on("data", (chunk: Buffer) => {
|
|
101
|
+
chunks.push(chunk);
|
|
102
|
+
notify?.();
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
req.end();
|
|
106
|
+
return {
|
|
107
|
+
async until(marker: string): Promise<string> {
|
|
108
|
+
for (;;) {
|
|
109
|
+
const text = Buffer.concat(chunks).toString("utf8");
|
|
110
|
+
if (text.includes(marker)) return text;
|
|
111
|
+
await new Promise<void>((resolve) => {
|
|
112
|
+
notify = resolve;
|
|
113
|
+
});
|
|
114
|
+
notify = undefined;
|
|
115
|
+
}
|
|
116
|
+
},
|
|
117
|
+
close: () => req.destroy(),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
describe("buildPlanState", () => {
|
|
122
|
+
it("shows no badges for the first revision", () => {
|
|
123
|
+
const session = makeSession("## Plan\n\none\n\ntwo");
|
|
124
|
+
const state = buildPlanState(session);
|
|
125
|
+
expect(state.items.map((item) => item.status)).toEqual(["unchanged", "unchanged", "unchanged"]);
|
|
126
|
+
expect(state.totalVersions).toBe(1);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("carries the diff statuses and the previous-block ids for a later revision", () => {
|
|
130
|
+
const session = makeSession("## Plan\n\nwrite the block parser before the server\n\nsecond step");
|
|
131
|
+
const first = session.versions[0];
|
|
132
|
+
appendPlanVersion(session, {
|
|
133
|
+
text: "## Plan\n\nwrite the block parser before the HTTP server\n\nsecond step\n\nthird step",
|
|
134
|
+
});
|
|
135
|
+
const state = buildPlanState(session);
|
|
136
|
+
expect(state.version).toBe(2);
|
|
137
|
+
expect(state.items.map((item) => item.status)).toEqual([
|
|
138
|
+
"unchanged",
|
|
139
|
+
"modified",
|
|
140
|
+
"unchanged",
|
|
141
|
+
"added",
|
|
142
|
+
]);
|
|
143
|
+
expect(state.items[1].prevId).toBe(first.blocks[1].id);
|
|
144
|
+
expect(state.history.map((entry) => entry.n)).toEqual([2, 1]);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("refuses an unknown revision", () => {
|
|
148
|
+
const session = makeSession();
|
|
149
|
+
expect(() => buildPlanState(session, 9)).toThrow(/no plan revision 9/);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe("normalizeAnnotations", () => {
|
|
154
|
+
it("keeps well-formed annotations and drops the rest", () => {
|
|
155
|
+
const normalized = normalizeAnnotations({
|
|
156
|
+
global: [
|
|
157
|
+
{ verdict: "change", comment: "fix scope" },
|
|
158
|
+
{ verdict: "unknown", comment: "nope" },
|
|
159
|
+
{ verdict: "suggest", comment: " " },
|
|
160
|
+
],
|
|
161
|
+
blocks: [
|
|
162
|
+
{ blockId: "abc#0", annotations: [{ verdict: "question", comment: "why?" }] },
|
|
163
|
+
{ blockId: "", annotations: [{ verdict: "change", comment: "orphan" }] },
|
|
164
|
+
{ blockId: "def#0", annotations: [] },
|
|
165
|
+
],
|
|
166
|
+
});
|
|
167
|
+
expect(normalized.global).toEqual([{ verdict: "change", comment: "fix scope" }]);
|
|
168
|
+
expect(normalized.blocks).toEqual([
|
|
169
|
+
{ blockId: "abc#0", annotations: [{ verdict: "question", comment: "why?" }] },
|
|
170
|
+
]);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("treats a missing body as no annotations", () => {
|
|
174
|
+
expect(normalizeAnnotations(undefined)).toEqual({ global: [], blocks: [] });
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
describe("startPlanServer", () => {
|
|
179
|
+
it("serves the fallback page and refuses a missing token", async () => {
|
|
180
|
+
const session = makeSession();
|
|
181
|
+
const server = await serve(session);
|
|
182
|
+
try {
|
|
183
|
+
expect((await call({ port: server.port, path: "/" })).status).toBe(401);
|
|
184
|
+
|
|
185
|
+
const page = await call({ port: server.port, path: `/?t=${server.token}` });
|
|
186
|
+
expect(page.status).toBe(200);
|
|
187
|
+
expect(page.text).toContain("The review UI is not built");
|
|
188
|
+
|
|
189
|
+
expect((await call({ port: server.port, path: "/api/state" })).status).toBe(401);
|
|
190
|
+
} finally {
|
|
191
|
+
await server.close();
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("serves state with rendered block html", async () => {
|
|
196
|
+
const session = makeSession("## Plan\n\nuse **bold** here");
|
|
197
|
+
const server = await serve(session);
|
|
198
|
+
try {
|
|
199
|
+
const response = await call({ port: server.port, path: `/api/state?t=${server.token}` });
|
|
200
|
+
expect(response.status).toBe(200);
|
|
201
|
+
const state = response.json() as { items: Array<{ html: string }> };
|
|
202
|
+
expect(state.items[0].html).toBe("<h2>Plan</h2>");
|
|
203
|
+
expect(state.items[1].html).toContain("<strong>bold</strong>");
|
|
204
|
+
} finally {
|
|
205
|
+
await server.close();
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("persists annotations posted by the page", async () => {
|
|
210
|
+
const session = makeSession();
|
|
211
|
+
const server = await serve(session);
|
|
212
|
+
try {
|
|
213
|
+
const response = await call({
|
|
214
|
+
port: server.port,
|
|
215
|
+
path: "/api/annotate",
|
|
216
|
+
method: "POST",
|
|
217
|
+
token: server.token,
|
|
218
|
+
body: {
|
|
219
|
+
version: 1,
|
|
220
|
+
annotations: { global: [{ verdict: "change", comment: "tighten it" }], blocks: [] },
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
expect(response.status).toBe(200);
|
|
224
|
+
expect(session.versions[0].annotations.global[0].comment).toBe("tighten it");
|
|
225
|
+
} finally {
|
|
226
|
+
await server.close();
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("refuses to annotate an older revision", async () => {
|
|
231
|
+
const session = makeSession("one");
|
|
232
|
+
appendPlanVersion(session, { text: "two" });
|
|
233
|
+
const server = await serve(session);
|
|
234
|
+
try {
|
|
235
|
+
const response = await call({
|
|
236
|
+
port: server.port,
|
|
237
|
+
path: "/api/annotate",
|
|
238
|
+
method: "POST",
|
|
239
|
+
token: server.token,
|
|
240
|
+
body: {
|
|
241
|
+
version: 1,
|
|
242
|
+
annotations: { global: [{ verdict: "change", comment: "too late" }], blocks: [] },
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
expect(response.status).toBe(409);
|
|
246
|
+
expect(session.versions[0].annotations.global).toEqual([]);
|
|
247
|
+
} finally {
|
|
248
|
+
await server.close();
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("records the decision, notifies the caller and closes the port", async () => {
|
|
253
|
+
const session = makeSession();
|
|
254
|
+
const onDecision = vi.fn();
|
|
255
|
+
const server = await serve(session, { onDecision });
|
|
256
|
+
const response = await call({
|
|
257
|
+
port: server.port,
|
|
258
|
+
path: "/api/decide",
|
|
259
|
+
method: "POST",
|
|
260
|
+
token: server.token,
|
|
261
|
+
body: { action: "approve" },
|
|
262
|
+
});
|
|
263
|
+
expect(response.status).toBe(200);
|
|
264
|
+
expect(response.json()).toEqual({ ok: true, action: "approve", version: 1 });
|
|
265
|
+
await vi.waitFor(() => expect(onDecision).toHaveBeenCalledWith({ action: "approve", version: 1 }));
|
|
266
|
+
expect(session.versions[0].decision?.action).toBe("approve");
|
|
267
|
+
await vi.waitFor(async () => {
|
|
268
|
+
await expect(call({ port: server.port, path: "/api/state" })).rejects.toThrow();
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("accepts only the first decision", async () => {
|
|
273
|
+
const session = makeSession();
|
|
274
|
+
const onDecision = vi.fn();
|
|
275
|
+
const server = await serve(session, { onDecision, closeDelayMs: 50 });
|
|
276
|
+
const first = await call({
|
|
277
|
+
port: server.port,
|
|
278
|
+
path: "/api/decide",
|
|
279
|
+
method: "POST",
|
|
280
|
+
token: server.token,
|
|
281
|
+
body: { action: "deny" },
|
|
282
|
+
});
|
|
283
|
+
const second = await call({
|
|
284
|
+
port: server.port,
|
|
285
|
+
path: "/api/decide",
|
|
286
|
+
method: "POST",
|
|
287
|
+
token: server.token,
|
|
288
|
+
body: { action: "approve" },
|
|
289
|
+
});
|
|
290
|
+
expect(first.status).toBe(200);
|
|
291
|
+
expect(second.status).toBe(409);
|
|
292
|
+
await vi.waitFor(() => expect(onDecision).toHaveBeenCalledTimes(1));
|
|
293
|
+
expect(session.versions[0].decision?.action).toBe("deny");
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it("rejects a decision that is neither approve nor deny", async () => {
|
|
297
|
+
const session = makeSession();
|
|
298
|
+
const server = await serve(session);
|
|
299
|
+
try {
|
|
300
|
+
const response = await call({
|
|
301
|
+
port: server.port,
|
|
302
|
+
path: "/api/decide",
|
|
303
|
+
method: "POST",
|
|
304
|
+
token: server.token,
|
|
305
|
+
body: { action: "maybe" },
|
|
306
|
+
});
|
|
307
|
+
expect(response.status).toBe(400);
|
|
308
|
+
} finally {
|
|
309
|
+
await server.close();
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it("refuses a foreign Host header and a cross-origin write", async () => {
|
|
314
|
+
const session = makeSession();
|
|
315
|
+
const server = await serve(session);
|
|
316
|
+
try {
|
|
317
|
+
const badHost = await call({
|
|
318
|
+
port: server.port,
|
|
319
|
+
path: `/api/state?t=${server.token}`,
|
|
320
|
+
headers: { Host: "evil.example:80" },
|
|
321
|
+
});
|
|
322
|
+
expect(badHost.status).toBe(400);
|
|
323
|
+
|
|
324
|
+
const crossOrigin = await call({
|
|
325
|
+
port: server.port,
|
|
326
|
+
path: "/api/annotate",
|
|
327
|
+
method: "POST",
|
|
328
|
+
token: server.token,
|
|
329
|
+
headers: { origin: "http://evil.example", "sec-fetch-site": "cross-site" },
|
|
330
|
+
body: { annotations: { global: [], blocks: [] } },
|
|
331
|
+
});
|
|
332
|
+
expect(crossOrigin.status).toBe(403);
|
|
333
|
+
|
|
334
|
+
// No port in Origin means :80, not this server's port.
|
|
335
|
+
const defaultPortOrigin = await call({
|
|
336
|
+
port: server.port,
|
|
337
|
+
path: "/api/annotate",
|
|
338
|
+
method: "POST",
|
|
339
|
+
token: server.token,
|
|
340
|
+
headers: { origin: "http://127.0.0.1", "sec-fetch-site": "cross-site" },
|
|
341
|
+
body: { annotations: { global: [], blocks: [] } },
|
|
342
|
+
});
|
|
343
|
+
expect(defaultPortOrigin.status).toBe(403);
|
|
344
|
+
|
|
345
|
+
const crossSiteWithoutOrigin = await call({
|
|
346
|
+
port: server.port,
|
|
347
|
+
path: "/api/annotate",
|
|
348
|
+
method: "POST",
|
|
349
|
+
token: server.token,
|
|
350
|
+
headers: { "sec-fetch-site": "cross-site" },
|
|
351
|
+
body: { annotations: { global: [], blocks: [] } },
|
|
352
|
+
});
|
|
353
|
+
expect(crossSiteWithoutOrigin.status).toBe(403);
|
|
354
|
+
} finally {
|
|
355
|
+
await server.close();
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it("streams revisions over SSE", async () => {
|
|
360
|
+
const session = makeSession("one");
|
|
361
|
+
const server = await serve(session);
|
|
362
|
+
const stream = openStream(server.port, `/api/events?t=${server.token}`);
|
|
363
|
+
try {
|
|
364
|
+
expect(await stream.until(": connected")).toContain(": connected");
|
|
365
|
+
appendPlanVersion(session, { text: "two" });
|
|
366
|
+
expect(parseBlocks("two")).toHaveLength(1);
|
|
367
|
+
server.broadcast("revision", { version: 2 });
|
|
368
|
+
expect(await stream.until("event: revision")).toContain('"version":2');
|
|
369
|
+
} finally {
|
|
370
|
+
stream.close();
|
|
371
|
+
await server.close();
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
});
|