2026-04-30 05:46:36 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
const assert = require("node:assert");
|
2026-05-09 19:05:06 +08:00
|
|
|
const { fetchWithTimeout } = require("./task114-rust-web-gateway-entry-smoke.js");
|
2026-04-30 05:46:36 +08:00
|
|
|
|
2026-05-09 19:05:06 +08:00
|
|
|
const BASE_URL = (process.env.MNOTE_WEB_SMOKE_BASE_URL || process.env.MNOTE_UI_BASE_URL || "http://127.0.0.1:3000").replace(/\/+$/, "");
|
2026-04-30 05:46:36 +08:00
|
|
|
|
|
|
|
|
async function fetchText(path) {
|
2026-05-09 19:05:06 +08:00
|
|
|
const response = await fetchWithTimeout(`${BASE_URL}${path}`);
|
2026-04-30 05:46:36 +08:00
|
|
|
const text = await response.text();
|
|
|
|
|
assert.equal(response.status, 200, `${path} 请求失败: ${response.status} ${text.slice(0, 200)}`);
|
|
|
|
|
return { response, text };
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-16 22:03:14 +08:00
|
|
|
function parseSseEventData(text, eventName) {
|
|
|
|
|
const blocks = text.split(/\n\n+/).filter((block) => block.trim().length > 0);
|
|
|
|
|
const block = blocks.find((candidate) => new RegExp(`^event:\\s*${eventName}\\s*$`, "m").test(candidate));
|
|
|
|
|
assert(block, `SSE 响应缺少 ${eventName} 事件`);
|
|
|
|
|
const dataLines = block
|
|
|
|
|
.split(/\n/)
|
|
|
|
|
.filter((line) => line.startsWith("data:"))
|
|
|
|
|
.map((line) => line.slice("data:".length).trimStart());
|
|
|
|
|
assert(dataLines.length > 0, `${eventName} 事件缺少 data`);
|
|
|
|
|
return JSON.parse(dataLines.join("\n"));
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 19:05:06 +08:00
|
|
|
async function readJsonResponse(response, label) {
|
|
|
|
|
const text = await response.text();
|
|
|
|
|
let payload = null;
|
|
|
|
|
try {
|
|
|
|
|
payload = text ? JSON.parse(text) : null;
|
|
|
|
|
} catch {
|
|
|
|
|
throw new Error(`${label} 返回了非 JSON 内容: ${text.slice(0, 1200)}`);
|
|
|
|
|
}
|
|
|
|
|
assert(response.ok, `${label} 请求失败: ${response.status}; body: ${text.slice(0, 1200)}`);
|
|
|
|
|
return payload;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function postTreeCommand(body, label) {
|
|
|
|
|
const response = await fetchWithTimeout(`${BASE_URL}/api/tree/commands`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: { "content-type": "application/json" },
|
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
|
});
|
|
|
|
|
const payload = await readJsonResponse(response, label);
|
|
|
|
|
const result = payload && typeof payload.result === "object" ? payload.result : null;
|
|
|
|
|
assert(result, `${label} 缺少 result`);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function createTempPage(title) {
|
|
|
|
|
const result = await postTreeCommand({ action: "create", title }, `创建临时页面 ${title}`);
|
|
|
|
|
assert(result.documentId, `创建临时页面 ${title} 缺少 documentId`);
|
|
|
|
|
assert(result.workspaceId, `创建临时页面 ${title} 缺少 workspaceId`);
|
|
|
|
|
return {
|
|
|
|
|
documentId: result.documentId,
|
|
|
|
|
workspaceId: result.workspaceId,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function purgeTempPage(target) {
|
|
|
|
|
if (!target?.documentId || !target?.workspaceId) return;
|
|
|
|
|
await postTreeCommand(
|
|
|
|
|
{
|
|
|
|
|
action: "purge",
|
|
|
|
|
workspaceId: target.workspaceId,
|
|
|
|
|
documentId: target.documentId,
|
|
|
|
|
},
|
|
|
|
|
`清理临时页面 ${target.documentId}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-30 05:46:36 +08:00
|
|
|
async function main() {
|
2026-05-09 19:05:06 +08:00
|
|
|
const pageTarget = await createTempPage(`task123-live-${Date.now().toString(36)}`);
|
|
|
|
|
try {
|
|
|
|
|
const page = await fetchText(`/documents/${encodeURIComponent(pageTarget.documentId)}?workspaceId=${encodeURIComponent(pageTarget.workspaceId)}`);
|
|
|
|
|
assert.match(page.text, /__MNOTE_TREE_LIVE_BOOTSTRAP__/);
|
|
|
|
|
assert.match(page.text, /mnote\.tree_live_bootstrap\.v1/);
|
|
|
|
|
assert.match(page.text, /\/api\/tree\/events/);
|
|
|
|
|
assert.match(page.text, /tree:snapshot/);
|
|
|
|
|
assert.match(page.text, /tree:delta/);
|
|
|
|
|
assert.match(page.text, /tree:resync/);
|
2026-04-30 05:46:36 +08:00
|
|
|
|
2026-05-09 19:05:06 +08:00
|
|
|
const events = await fetchText(`/api/tree/events?workspaceId=${encodeURIComponent(pageTarget.workspaceId)}&maxPolls=0`);
|
|
|
|
|
assert.match(events.response.headers.get("content-type") || "", /text\/event-stream/);
|
|
|
|
|
assert.equal(events.response.headers.get("x-mnote-tree-stream-owner"), "rust-web");
|
|
|
|
|
assert.match(events.text, /event:\s*snapshot/);
|
|
|
|
|
assert.match(events.text, /id:\s*/);
|
|
|
|
|
assert.match(events.text, /"revision"/);
|
2026-04-30 05:46:36 +08:00
|
|
|
|
2026-05-16 22:03:14 +08:00
|
|
|
const snapshot = parseSseEventData(events.text, "snapshot");
|
|
|
|
|
assert.equal(snapshot.kind, "snapshot");
|
|
|
|
|
assert.equal(snapshot.stream, "workspace");
|
|
|
|
|
assert.equal(snapshot.projection, "sidebar_tree");
|
|
|
|
|
assert.equal(snapshot.workspaceId, pageTarget.workspaceId);
|
|
|
|
|
const dataset = snapshot.data?.dataset || snapshot.data;
|
|
|
|
|
assert(dataset?.kernel_sidebar_projection, "snapshot 缺少 kernel_sidebar_projection");
|
|
|
|
|
assert(dataset?.kernel_file_tree_projection, "snapshot 缺少 kernel_file_tree_projection");
|
|
|
|
|
const fileTreeItems = dataset.kernel_file_tree_projection.items || [];
|
|
|
|
|
assert(
|
|
|
|
|
fileTreeItems.some(
|
|
|
|
|
(item) =>
|
|
|
|
|
(item?.rowId || item?.row_id) === `doc:${pageTarget.documentId}` &&
|
|
|
|
|
(item?.rowKind || item?.row_kind) === "document" &&
|
|
|
|
|
(item?.resourceMeta?.documentId || item?.resource_meta?.document_id) ===
|
|
|
|
|
pageTarget.documentId,
|
|
|
|
|
),
|
|
|
|
|
"workspace snapshot 的 file tree projection 缺少临时页 doc row",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
|
JSON.stringify(
|
|
|
|
|
{
|
|
|
|
|
ok: true,
|
|
|
|
|
owner: "rust-web",
|
|
|
|
|
stream: "/api/tree/events",
|
|
|
|
|
documentId: pageTarget.documentId,
|
|
|
|
|
snapshotProjection: snapshot.projection,
|
|
|
|
|
fileTreeRows: fileTreeItems.length,
|
|
|
|
|
},
|
|
|
|
|
null,
|
|
|
|
|
2,
|
|
|
|
|
),
|
|
|
|
|
);
|
2026-05-09 19:05:06 +08:00
|
|
|
} finally {
|
|
|
|
|
await purgeTempPage(pageTarget).catch(() => undefined);
|
|
|
|
|
}
|
2026-04-30 05:46:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main().catch((error) => {
|
|
|
|
|
console.error(error instanceof Error ? error.stack || error.message : String(error));
|
|
|
|
|
process.exit(1);
|
|
|
|
|
});
|