


























1+// Plugin Index SQLite tests cover shared E2E install-index readers.
2+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+import { tmpdir } from "node:os";
4+import path from "node:path";
5+import { DatabaseSync } from "node:sqlite";
6+import { pathToFileURL } from "node:url";
7+import { describe, expect, it } from "vitest";
8+9+const MODULE_URL = pathToFileURL(path.resolve("scripts/e2e/lib/plugin-index-sqlite.mjs")).href;
10+let importCounter = 0;
11+12+async function loadPluginIndex(env: Record<string, string> = {}) {
13+const previous = new Map(Object.keys(env).map((key) => [key, process.env[key]]));
14+Object.assign(process.env, env);
15+try {
16+return await import(`${MODULE_URL}?case=${importCounter++}`);
17+} finally {
18+for (const [key, value] of previous) {
19+if (value === undefined) {
20+delete process.env[key];
21+} else {
22+process.env[key] = value;
23+}
24+}
25+}
26+}
27+28+function writeLegacyIndex(root: string, text: string) {
29+const file = path.join(root, "plugins", "installs.json");
30+mkdirSync(path.dirname(file), { recursive: true });
31+writeFileSync(file, text, "utf8");
32+}
33+34+function configPath(root: string) {
35+return path.join(root, "openclaw.json");
36+}
37+38+function writeSqliteIndex(root: string, installRecordsJson: string) {
39+const dbPath = path.join(root, "state", "openclaw.sqlite");
40+mkdirSync(path.dirname(dbPath), { recursive: true });
41+const db = new DatabaseSync(dbPath);
42+try {
43+db.exec(`
44+ CREATE TABLE installed_plugin_index (
45+ index_key TEXT NOT NULL PRIMARY KEY,
46+ version INTEGER NOT NULL,
47+ host_contract_version TEXT NOT NULL,
48+ compat_registry_version TEXT NOT NULL,
49+ migration_version INTEGER NOT NULL,
50+ policy_hash TEXT NOT NULL,
51+ generated_at_ms INTEGER NOT NULL,
52+ refresh_reason TEXT,
53+ install_records_json TEXT NOT NULL,
54+ plugins_json TEXT NOT NULL,
55+ diagnostics_json TEXT NOT NULL,
56+ warning TEXT,
57+ updated_at_ms INTEGER NOT NULL
58+ );
59+ `);
60+db.prepare(
61+`
62+ INSERT INTO installed_plugin_index (
63+ index_key, version, host_contract_version, compat_registry_version,
64+ migration_version, policy_hash, generated_at_ms, refresh_reason,
65+ install_records_json, plugins_json, diagnostics_json, warning, updated_at_ms
66+ )
67+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
68+ `,
69+).run(
70+"installed-plugin-index",
71+1,
72+"1",
73+"1",
74+1,
75+"hash",
76+Date.now(),
77+null,
78+installRecordsJson,
79+"{}",
80+"{}",
81+null,
82+Date.now(),
83+);
84+} finally {
85+db.close();
86+}
87+}
88+89+describe("plugin index SQLite E2E helpers", () => {
90+it("reads legacy install records when SQLite index state is absent", async () => {
91+const root = mkdtempSync(path.join(tmpdir(), "openclaw-plugin-index-"));
92+try {
93+writeLegacyIndex(
94+root,
95+JSON.stringify({ records: { demo: { installPath: "/tmp/demo", source: "npm" } } }),
96+);
97+98+const { readPluginInstallRecords } = await loadPluginIndex();
99+100+expect(readPluginInstallRecords({ stateDir: root, configPath: configPath(root) })).toEqual({
101+demo: { installPath: "/tmp/demo", source: "npm" },
102+});
103+} finally {
104+rmSync(root, { force: true, recursive: true });
105+}
106+});
107+108+it("keeps malformed legacy install JSON as an empty fallback", async () => {
109+const root = mkdtempSync(path.join(tmpdir(), "openclaw-plugin-index-"));
110+try {
111+writeLegacyIndex(root, "{not-json");
112+113+const { readPluginInstallRecords } = await loadPluginIndex();
114+115+expect(readPluginInstallRecords({ stateDir: root, configPath: configPath(root) })).toEqual(
116+{},
117+);
118+} finally {
119+rmSync(root, { force: true, recursive: true });
120+}
121+});
122+123+it("rejects oversized legacy install JSON before parsing it", async () => {
124+const root = mkdtempSync(path.join(tmpdir(), "openclaw-plugin-index-"));
125+try {
126+writeLegacyIndex(root, JSON.stringify({ records: {}, filler: "x".repeat(128) }));
127+128+const { readPluginInstallRecords } = await loadPluginIndex({
129+OPENCLAW_PLUGIN_INDEX_JSON_MAX_BYTES: "64",
130+});
131+132+expect(() =>
133+readPluginInstallRecords({ stateDir: root, configPath: configPath(root) }),
134+).toThrow("plugin index JSON artifact exceeded 64 bytes");
135+} finally {
136+rmSync(root, { force: true, recursive: true });
137+}
138+});
139+140+it("rejects oversized SQLite install index JSON before parsing it", async () => {
141+const root = mkdtempSync(path.join(tmpdir(), "openclaw-plugin-index-"));
142+try {
143+writeSqliteIndex(root, JSON.stringify({ filler: "x".repeat(128) }));
144+145+const { readPluginInstallIndex } = await loadPluginIndex({
146+OPENCLAW_PLUGIN_INDEX_JSON_MAX_BYTES: "64",
147+});
148+149+expect(() =>
150+readPluginInstallIndex({ stateDir: root, configPath: configPath(root) }),
151+).toThrow("plugin index install_records_json exceeded 64 bytes");
152+} finally {
153+rmSync(root, { force: true, recursive: true });
154+}
155+});
156+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。