dsh-app-updater 1.3.2 → 1.3.4

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/test/harness.mjs CHANGED
@@ -1,125 +1,125 @@
1
- /**
2
- * 宿主半身端到端自测:用一个假的 cordis ctx + 真 http server 把插件路由跑起来,
3
- * 然后像浏览器一样打真实请求。这样不重启 DSH 也能验证整条链路。
4
- *
5
- * 用法:node test/harness.mjs [--scan] [--update <id>]
6
- */
7
-
8
- import http from "node:http";
9
- import { fileURLToPath } from "node:url";
10
- import { dirname, join } from "node:path";
11
-
12
- const here = dirname(fileURLToPath(import.meta.url));
13
- process.env.DSH_HOME = process.env.DSH_HOME || join(here, "..", ".test-home");
14
- // 这个假 ctx 没有 connection 服务,等价于旧宿主:默认拒绝下路由一律 403。
15
- // 所以自测里显式开启兼容模式(与用户在旧宿主上手动开启走的是同一段代码)。
16
- process.env.DSH_APP_UPDATER_ALLOW_LEGACY_LOOPBACK = "1";
17
-
18
- const plugin = (await import("../lib/index.js")).default;
19
-
20
- const routes = [];
21
- const webServer = {
22
- register(spec) {
23
- routes.push(spec);
24
- return () => {};
25
- },
26
- };
27
-
28
- const ctx = {
29
- effect(fn) {
30
- const dispose = fn();
31
- return () => {
32
- if (typeof dispose === "function") dispose();
33
- };
34
- },
35
- inject(deps, fn) {
36
- fn({ webServer });
37
- },
38
- };
39
-
40
- plugin.apply(ctx);
41
-
42
- const server = http.createServer((req, res) => {
43
- const url = new URL(req.url, "http://127.0.0.1");
44
- const route = routes.find((r) => r.path === url.pathname);
45
- if (!route) {
46
- res.writeHead(404, { "Content-Type": "application/json" });
47
- res.end(JSON.stringify({ ok: false, error: "no route" }));
48
- return;
49
- }
50
- Promise.resolve(route.handler(req, res)).catch((err) => {
51
- if (!res.headersSent) res.writeHead(500, { "Content-Type": "application/json" });
52
- res.end(JSON.stringify({ ok: false, error: String((err && err.message) || err) }));
53
- });
54
- });
55
-
56
- await new Promise((r) => server.listen(0, "127.0.0.1", r));
57
- const base = `http://127.0.0.1:${server.address().port}`;
58
-
59
- const get = async (p) => (await fetch(base + p)).json();
60
- const post = async (p, body) => (await fetch(base + p, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) })).json();
61
-
62
- function head(title) {
63
- console.log(`\n=== ${title} ===`);
64
- }
65
-
66
- head("routes");
67
- console.log(routes.map((r) => r.path));
68
-
69
- head("health");
70
- console.log(await get("/dsh-app-updater/health.json"));
71
-
72
- head("state (before scan)");
73
- const s0 = await get("/dsh-app-updater/state.json");
74
- console.log({ phase: s0.phase, winget: s0.winget, items: s0.apps.items.length, downloadDir: s0.downloadDir });
75
-
76
- head("write gate: missing confirm");
77
- console.log(await post("/dsh-app-updater/scan", {}));
78
-
79
- head("write gate: bad json");
80
- console.log(await (await fetch(base + "/dsh-app-updater/scan", { method: "POST", body: "{oops" })).json());
81
-
82
- if (process.argv.includes("--scan")) {
83
- head("scan");
84
- console.log(await post("/dsh-app-updater/scan", { confirm: true, force: true }));
85
- for (let i = 0; i < 120; i += 1) {
86
- const st = await get("/dsh-app-updater/state.json");
87
- if (st.phase === "idle" && st.apps.scannedAt) {
88
- console.log({ phase: st.phase, stats: st.apps.stats, winget: st.winget });
89
- const up = st.apps.items.filter((a) => a.hasUpdate);
90
- console.log("upgradable:", up.map((a) => `${a.name} ${a.version}->${a.available}`));
91
- break;
92
- }
93
- await new Promise((r) => setTimeout(r, 500));
94
- }
95
- head("apps.json");
96
- const apps = await get("/dsh-app-updater/apps.json");
97
- console.log({ total: apps.items.length, stats: apps.stats, warnings: apps.warnings });
98
- }
99
-
100
- if (process.argv.includes("--update")) {
101
- const id = process.argv[process.argv.indexOf("--update") + 1];
102
- head(`update ${id}`);
103
- console.log(await post("/dsh-app-updater/update", { confirm: true, ids: [id], mode: "silent" }));
104
- for (let i = 0; i < 900; i += 1) {
105
- const st = await get("/dsh-app-updater/state.json");
106
- if (st.phase !== "updating") {
107
- console.log("done:", JSON.stringify(st.update.results, null, 1));
108
- break;
109
- }
110
- if (i % 10 === 0) console.log("progress", st.update.done + "/" + st.update.total, st.update.current || "");
111
- await new Promise((r) => setTimeout(r, 1000));
112
- }
113
- head("log tail");
114
- const st = await get("/dsh-app-updater/state.json");
115
- console.log(st.log.slice(-12).map((l) => `[${l.level}] ${l.text}`).join("\n"));
116
- }
117
-
118
- head("settings");
119
- console.log(await post("/dsh-app-updater/settings", { confirm: true, settings: { keepInstallers: true, downloadDir: "" } }));
120
-
121
- head("open-downloads (dry: just check path)");
122
- console.log(await post("/dsh-app-updater/open-downloads", { confirm: true }));
123
-
124
- server.close();
125
- console.log("\nharness done");
1
+ /**
2
+ * 宿主半身端到端自测:用一个假的 cordis ctx + 真 http server 把插件路由跑起来,
3
+ * 然后像浏览器一样打真实请求。这样不重启 DSH 也能验证整条链路。
4
+ *
5
+ * 用法:node test/harness.mjs [--scan] [--update <id>]
6
+ */
7
+
8
+ import http from "node:http";
9
+ import { fileURLToPath } from "node:url";
10
+ import { dirname, join } from "node:path";
11
+
12
+ const here = dirname(fileURLToPath(import.meta.url));
13
+ process.env.DSH_HOME = process.env.DSH_HOME || join(here, "..", ".test-home");
14
+ // 这个假 ctx 没有 connection 服务,等价于旧宿主:默认拒绝下路由一律 403。
15
+ // 所以自测里显式开启兼容模式(与用户在旧宿主上手动开启走的是同一段代码)。
16
+ process.env.DSH_APP_UPDATER_ALLOW_LEGACY_LOOPBACK = "1";
17
+
18
+ const plugin = (await import("../lib/index.js")).default;
19
+
20
+ const routes = [];
21
+ const webServer = {
22
+ register(spec) {
23
+ routes.push(spec);
24
+ return () => {};
25
+ },
26
+ };
27
+
28
+ const ctx = {
29
+ effect(fn) {
30
+ const dispose = fn();
31
+ return () => {
32
+ if (typeof dispose === "function") dispose();
33
+ };
34
+ },
35
+ inject(deps, fn) {
36
+ fn({ webServer });
37
+ },
38
+ };
39
+
40
+ plugin.apply(ctx);
41
+
42
+ const server = http.createServer((req, res) => {
43
+ const url = new URL(req.url, "http://127.0.0.1");
44
+ const route = routes.find((r) => r.path === url.pathname);
45
+ if (!route) {
46
+ res.writeHead(404, { "Content-Type": "application/json" });
47
+ res.end(JSON.stringify({ ok: false, error: "no route" }));
48
+ return;
49
+ }
50
+ Promise.resolve(route.handler(req, res)).catch((err) => {
51
+ if (!res.headersSent) res.writeHead(500, { "Content-Type": "application/json" });
52
+ res.end(JSON.stringify({ ok: false, error: String((err && err.message) || err) }));
53
+ });
54
+ });
55
+
56
+ await new Promise((r) => server.listen(0, "127.0.0.1", r));
57
+ const base = `http://127.0.0.1:${server.address().port}`;
58
+
59
+ const get = async (p) => (await fetch(base + p)).json();
60
+ const post = async (p, body) => (await fetch(base + p, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) })).json();
61
+
62
+ function head(title) {
63
+ console.log(`\n=== ${title} ===`);
64
+ }
65
+
66
+ head("routes");
67
+ console.log(routes.map((r) => r.path));
68
+
69
+ head("health");
70
+ console.log(await get("/dsh-app-updater/health.json"));
71
+
72
+ head("state (before scan)");
73
+ const s0 = await get("/dsh-app-updater/state.json");
74
+ console.log({ phase: s0.phase, winget: s0.winget, items: s0.apps.items.length, downloadDir: s0.downloadDir });
75
+
76
+ head("write gate: missing confirm");
77
+ console.log(await post("/dsh-app-updater/scan", {}));
78
+
79
+ head("write gate: bad json");
80
+ console.log(await (await fetch(base + "/dsh-app-updater/scan", { method: "POST", body: "{oops" })).json());
81
+
82
+ if (process.argv.includes("--scan")) {
83
+ head("scan");
84
+ console.log(await post("/dsh-app-updater/scan", { confirm: true, force: true }));
85
+ for (let i = 0; i < 120; i += 1) {
86
+ const st = await get("/dsh-app-updater/state.json");
87
+ if (st.phase === "idle" && st.apps.scannedAt) {
88
+ console.log({ phase: st.phase, stats: st.apps.stats, winget: st.winget });
89
+ const up = st.apps.items.filter((a) => a.hasUpdate);
90
+ console.log("upgradable:", up.map((a) => `${a.name} ${a.version}->${a.available}`));
91
+ break;
92
+ }
93
+ await new Promise((r) => setTimeout(r, 500));
94
+ }
95
+ head("apps.json");
96
+ const apps = await get("/dsh-app-updater/apps.json");
97
+ console.log({ total: apps.items.length, stats: apps.stats, warnings: apps.warnings });
98
+ }
99
+
100
+ if (process.argv.includes("--update")) {
101
+ const id = process.argv[process.argv.indexOf("--update") + 1];
102
+ head(`update ${id}`);
103
+ console.log(await post("/dsh-app-updater/update", { confirm: true, ids: [id], mode: "silent" }));
104
+ for (let i = 0; i < 900; i += 1) {
105
+ const st = await get("/dsh-app-updater/state.json");
106
+ if (st.phase !== "updating") {
107
+ console.log("done:", JSON.stringify(st.update.results, null, 1));
108
+ break;
109
+ }
110
+ if (i % 10 === 0) console.log("progress", st.update.done + "/" + st.update.total, st.update.current || "");
111
+ await new Promise((r) => setTimeout(r, 1000));
112
+ }
113
+ head("log tail");
114
+ const st = await get("/dsh-app-updater/state.json");
115
+ console.log(st.log.slice(-12).map((l) => `[${l.level}] ${l.text}`).join("\n"));
116
+ }
117
+
118
+ head("settings");
119
+ console.log(await post("/dsh-app-updater/settings", { confirm: true, settings: { keepInstallers: true, downloadDir: "" } }));
120
+
121
+ head("open-downloads (dry: just check path)");
122
+ console.log(await post("/dsh-app-updater/open-downloads", { confirm: true }));
123
+
124
+ server.close();
125
+ console.log("\nharness done");