@aiti-ai/ext-shared 0.1.12 → 0.1.16
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.zh-CN.md +27 -0
- package/agent-spawn-env.test.mjs +92 -0
- package/agent-spawn-env.ts +92 -1
- package/dev-pack.mjs +16 -3
- package/dock-icons.ts +145 -13
- package/ext-display-names.ts +108 -78
- package/ext-entry-contracts.test.mjs +8 -10
- package/ext-float-dock-core.ts +66 -9
- package/ext-float-dock.test.mjs +1 -0
- package/ext-float-dock.ts +181 -32
- package/ext-protocol.mjs +1 -1
- package/git-ops-client/api.mjs +3 -1
- package/git-ops-client/branch-panel.tsx +115 -20
- package/git-ops-client/graph-lanes.mjs +118 -0
- package/git-ops-client/graph-lanes.test.mjs +132 -0
- package/git-ops-client/i18n.mjs +9 -0
- package/icons/box.svg +1 -0
- package/icons/discussion.svg +1 -0
- package/icons/film.svg +1 -0
- package/icons/flag.svg +1 -0
- package/icons/message-square-text.svg +1 -0
- package/icons/qr-code.svg +1 -0
- package/icons/star.svg +1 -0
- package/menu-row.tsx +92 -0
- package/new-session-cwd.test.mjs +4 -4
- package/package.json +2 -2
- package/path-breadcrumb.tsx +363 -0
- package/path-crumbs.test.mjs +91 -0
- package/path-crumbs.ts +102 -0
- package/pi-fork-rpc/wire.test.mjs +4 -3
- package/pi-fork-rpc/wire.ts +39 -2
- package/popup-anchor.ts +27 -2
- package/soffice-panel.ts +39 -30
- package/typo-search.mjs +79 -0
- package/typo-search.test.mjs +57 -0
- package/ui-dialogs.test.mjs +59 -4
- package/ui-dialogs.ts +95 -9
- package/sync-gate.test.mjs +0 -93
- package/sync-mirror.test.mjs +0 -85
package/ui-dialogs.ts
CHANGED
|
@@ -17,6 +17,12 @@
|
|
|
17
17
|
* - data-ext-float-max="0" → 豁免最大化按钮注入(小确认框不需要)
|
|
18
18
|
* - data-ext-float-persist="0" → 不做位置持久化(一次性弹窗不污染持久化槽)
|
|
19
19
|
* 同一时刻只显示一个弹窗(内部 Promise 链排队),保证不叠罗汉。
|
|
20
|
+
*
|
|
21
|
+
* Layering: mount prefers <dialog>.showModal() — the browser top layer beats any
|
|
22
|
+
* self-managed huge z-index an extension panel may set (e.g. model wizard 2147483101 /
|
|
23
|
+
* extman 2147483645 used to bury the confirm dialog, so it only showed up after closing
|
|
24
|
+
* the panel). Environments without showModal (legacy browsers / DOM stubs) fall back to a
|
|
25
|
+
* top-level body div with Z_BASE=2147483600 (same tier as session-manager confirm-dialog).
|
|
20
26
|
*/
|
|
21
27
|
|
|
22
28
|
export type UiDialogKind = "info" | "success" | "error" | "warn";
|
|
@@ -40,6 +46,24 @@ export interface UiAlertOptions {
|
|
|
40
46
|
confirmText?: string;
|
|
41
47
|
}
|
|
42
48
|
|
|
49
|
+
export interface UiChoiceItem {
|
|
50
|
+
/** Resolved value for this button ("cancel" is normally null/default). */
|
|
51
|
+
id: string;
|
|
52
|
+
label: string;
|
|
53
|
+
/** Visual weight: primary = accent fill, danger = red fill, default = plain. */
|
|
54
|
+
tone?: "primary" | "danger" | "default";
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface UiChoiceOptions {
|
|
58
|
+
title?: string;
|
|
59
|
+
message?: string;
|
|
60
|
+
detail?: string;
|
|
61
|
+
/** Buttons in display order (right-aligned). */
|
|
62
|
+
choices: UiChoiceItem[];
|
|
63
|
+
/** Esc / backdrop behaviour: resolved value for the "cancel" path (default null). */
|
|
64
|
+
cancelId?: string | null;
|
|
65
|
+
}
|
|
66
|
+
|
|
43
67
|
export interface UiPromptOptions {
|
|
44
68
|
title?: string;
|
|
45
69
|
message?: string;
|
|
@@ -49,7 +73,11 @@ export interface UiPromptOptions {
|
|
|
49
73
|
required?: boolean;
|
|
50
74
|
}
|
|
51
75
|
|
|
52
|
-
|
|
76
|
+
/** Fallback stacking tier (top-level body div when showModal is unavailable): must sit above
|
|
77
|
+
* the huge z-indices extension panels manage themselves (model wizard 2147483101 / schedule
|
|
78
|
+
* 2147483300 / menus 2147483500 / dock + session menus 2147483600) — same tier as the
|
|
79
|
+
* session-manager confirm dialog. With showModal the tier is irrelevant (top layer wins). */
|
|
80
|
+
const Z_BASE = 2147483600;
|
|
53
81
|
|
|
54
82
|
/** 常用 CSS 变量兜底(宿主一定是 pi-web 主题页;fallback 仅为防御) */
|
|
55
83
|
const V = {
|
|
@@ -114,7 +142,7 @@ const btnBase = {
|
|
|
114
142
|
} as const;
|
|
115
143
|
|
|
116
144
|
interface PendingSpec {
|
|
117
|
-
kind: "confirm" | "alert" | "prompt";
|
|
145
|
+
kind: "confirm" | "alert" | "prompt" | "choice";
|
|
118
146
|
opts: any;
|
|
119
147
|
resolve: (v: unknown) => void;
|
|
120
148
|
}
|
|
@@ -149,12 +177,39 @@ function drain() {
|
|
|
149
177
|
}
|
|
150
178
|
}
|
|
151
179
|
|
|
152
|
-
|
|
180
|
+
/* Modal-hosting capability probe (once per mount; stubs / legacy browsers → false → z-index fallback) */
|
|
181
|
+
function canUseTopLayer(): boolean {
|
|
182
|
+
try {
|
|
183
|
+
const probe = document.createElement("dialog") as any;
|
|
184
|
+
return !!probe && typeof probe.showModal === "function";
|
|
185
|
+
} catch { return false; }
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The modal host draws its own rgba mask; the UA `::backdrop` would darken the page twice
|
|
189
|
+
* (0.45 + ~0.1) and drift from the pre-top-layer look. One idempotent rule, injected once. */
|
|
190
|
+
const BACKDROP_STYLE_ID = "ui-dialogs-backdrop";
|
|
191
|
+
function ensureBackdropStyle(): void {
|
|
192
|
+
try {
|
|
193
|
+
if (document.getElementById(BACKDROP_STYLE_ID)) return;
|
|
194
|
+
const st = document.createElement("style");
|
|
195
|
+
st.id = BACKDROP_STYLE_ID;
|
|
196
|
+
st.textContent = "[data-ui-dialog-overlay]::backdrop{background:transparent;}";
|
|
197
|
+
(document.head || document.body).appendChild(st);
|
|
198
|
+
} catch { /* headless / stub: the mask alone still reads fine */ }
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Top-level mount: document.body level, independent of any extension panel DOM.
|
|
202
|
+
* With <dialog> → showModal() enters the browser top layer (above every z-index, so a dialog
|
|
203
|
+
* opened from an ultra-high-z panel stays visible); otherwise a div + Z_BASE fallback. */
|
|
153
204
|
function mount(): { overlay: HTMLDivElement; panel: HTMLDivElement } {
|
|
154
|
-
const
|
|
205
|
+
const modal = canUseTopLayer();
|
|
206
|
+
const overlay = document.createElement(modal ? "dialog" : "div") as HTMLDivElement;
|
|
155
207
|
overlay.setAttribute("data-ui-dialog-overlay", "1");
|
|
208
|
+
// dialog UA defaults (border/padding/margin/max-width, width/height fit-content) must be reset —
|
|
209
|
+
// inset:0 + width/height auto is what makes the overlay fill the viewport; display:flex serves the fallback path
|
|
156
210
|
overlay.style.cssText =
|
|
157
|
-
"position:fixed;inset:0;
|
|
211
|
+
"position:fixed;inset:0;margin:0;border:0;padding:20px;width:auto;height:auto;max-width:none;max-height:none;" +
|
|
212
|
+
"background:rgba(0,0,0,0.45);display:flex;align-items:center;justify-content:center;z-index:" + Z_BASE + ";";
|
|
158
213
|
const panel = document.createElement("div");
|
|
159
214
|
panel.setAttribute("data-ext-float", "1");
|
|
160
215
|
panel.setAttribute("data-ext-float-w", "440");
|
|
@@ -169,6 +224,14 @@ function mount(): { overlay: HTMLDivElement; panel: HTMLDivElement } {
|
|
|
169
224
|
";color:" + V.text + ";font-family:inherit;font-size:13px;box-shadow:0 20px 60px rgba(0,0,0,0.35);outline:none;overflow:hidden;";
|
|
170
225
|
overlay.appendChild(panel);
|
|
171
226
|
document.body.appendChild(overlay);
|
|
227
|
+
if (modal) {
|
|
228
|
+
ensureBackdropStyle();
|
|
229
|
+
try {
|
|
230
|
+
(overlay as any).showModal(); // top layer: above any in-page z-index
|
|
231
|
+
} catch {
|
|
232
|
+
/* already in a modal stack / headless: keep the plain-block semantics (display:flex + Z_BASE) */
|
|
233
|
+
}
|
|
234
|
+
}
|
|
172
235
|
return { overlay, panel };
|
|
173
236
|
}
|
|
174
237
|
|
|
@@ -189,7 +252,7 @@ function buildBody(panel: HTMLDivElement, spec: PendingSpec, finish: (v: unknown
|
|
|
189
252
|
head.appendChild(icon);
|
|
190
253
|
const titleEl = document.createElement("div");
|
|
191
254
|
titleEl.style.cssText = "flex:1;min-width:0;font-size:15px;font-weight:700;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;";
|
|
192
|
-
titleEl.textContent = o.title || (spec.kind === "confirm" ? (o.danger ? "危险操作确认" : "确认操作") : spec.kind === "prompt" ? "输入" : "提示");
|
|
255
|
+
titleEl.textContent = o.title || (spec.kind === "confirm" ? (o.danger ? "危险操作确认" : "确认操作") : spec.kind === "prompt" ? "输入" : spec.kind === "choice" ? "请选择" : "提示");
|
|
193
256
|
head.appendChild(titleEl);
|
|
194
257
|
panel.appendChild(head);
|
|
195
258
|
|
|
@@ -241,11 +304,17 @@ function buildBody(panel: HTMLDivElement, spec: PendingSpec, finish: (v: unknown
|
|
|
241
304
|
foot.appendChild(b);
|
|
242
305
|
return b;
|
|
243
306
|
};
|
|
244
|
-
if (spec.kind === "
|
|
307
|
+
if (spec.kind === "choice") {
|
|
308
|
+
const items: UiChoiceItem[] = Array.isArray(o.choices) ? o.choices : [];
|
|
309
|
+
for (const item of items) {
|
|
310
|
+
mk(item.label || item.id, item.tone === "primary", item.tone === "danger", () => finish(item.id));
|
|
311
|
+
}
|
|
312
|
+
if (items.length === 0) finish(o.cancelId ?? null);
|
|
313
|
+
} else if (spec.kind === "alert") {
|
|
245
314
|
const ok = mk(o.confirmText || "确定", true, false, () => finish(undefined));
|
|
246
315
|
(ok as any).__primary = true;
|
|
247
316
|
} else {
|
|
248
|
-
mk(o.cancelText || "取消", false, false, () => finish(spec.kind === "prompt" ? null : false));
|
|
317
|
+
mk(o.cancelText || "取消", false, false, () => finish(spec.kind === "prompt" ? null : spec.kind === "choice" ? (o.cancelId ?? null) : false));
|
|
249
318
|
const ok = mk(o.confirmText || (spec.kind === "confirm" && o.danger ? "确认执行" : "确定"), true, !!(spec.kind === "confirm" && o.danger), () =>
|
|
250
319
|
finish(spec.kind === "prompt" ? (input && o.required && !input.value.trim() ? undefined : input!.value) : true));
|
|
251
320
|
(ok as any).__primary = true;
|
|
@@ -254,7 +323,7 @@ function buildBody(panel: HTMLDivElement, spec: PendingSpec, finish: (v: unknown
|
|
|
254
323
|
|
|
255
324
|
// 键盘:Esc=取消;Enter=确认(焦点在非按钮元素时)
|
|
256
325
|
panel.addEventListener("keydown", (e) => {
|
|
257
|
-
if (e.key === "Escape") { e.stopPropagation(); finish(spec.kind === "prompt" ? null : spec.kind === "confirm" ? false : undefined); }
|
|
326
|
+
if (e.key === "Escape") { e.stopPropagation(); finish(spec.kind === "prompt" || spec.kind === "choice" ? null : spec.kind === "confirm" ? false : undefined); }
|
|
258
327
|
else if (e.key === "Enter" && !(e.target instanceof HTMLButtonElement)) {
|
|
259
328
|
e.preventDefault();
|
|
260
329
|
(foot.querySelector("button") ? Array.from(foot.querySelectorAll("button")).at(-1) : null)?.click();
|
|
@@ -272,10 +341,18 @@ function showDialog(spec: PendingSpec, done: () => void) {
|
|
|
272
341
|
if (settled) return;
|
|
273
342
|
settled = true;
|
|
274
343
|
spec.resolve(v);
|
|
344
|
+
try { (overlay as any).close?.(); } catch {} // leave the top layer (div fallback has no close)
|
|
275
345
|
try { overlay.remove(); } catch {}
|
|
276
346
|
try { prevFocus && typeof prevFocus.focus === "function" && prevFocus.focus(); } catch {}
|
|
277
347
|
setTimeout(done, 0);
|
|
278
348
|
};
|
|
349
|
+
// Native Esc (modal dialog "cancel"): intercept the browser close and route through finish
|
|
350
|
+
try {
|
|
351
|
+
(overlay as any).addEventListener?.("cancel", (e: any) => {
|
|
352
|
+
try { e?.preventDefault?.(); } catch {}
|
|
353
|
+
finish(spec.kind === "prompt" || spec.kind === "choice" ? null : spec.kind === "confirm" ? false : undefined);
|
|
354
|
+
});
|
|
355
|
+
} catch { /* no event system in DOM stubs */ }
|
|
279
356
|
const input = buildBody(panel, spec, finish);
|
|
280
357
|
// 初始焦点:prompt → 输入框(全选);confirm/alert → 主按钮
|
|
281
358
|
setTimeout(() => {
|
|
@@ -308,3 +385,12 @@ export function uiPrompt(opts: UiPromptOptions | string): Promise<string | null>
|
|
|
308
385
|
const o = typeof opts === "string" ? { message: opts } : opts;
|
|
309
386
|
return enqueue<string | null>("prompt", o);
|
|
310
387
|
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Multi-choice dialog (three-way decisions such as mask / send as-is / cancel).
|
|
391
|
+
* Clicking an item resolves its id; Esc or the mask resolves `cancelId` (default
|
|
392
|
+
* null). One dialog at a time (queued internally).
|
|
393
|
+
*/
|
|
394
|
+
export function uiChoice(opts: UiChoiceOptions): Promise<string | null> {
|
|
395
|
+
return enqueue<string | null>("choice", opts);
|
|
396
|
+
}
|
package/sync-gate.test.mjs
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* sync-gate.test.mjs —— 镜像脚本闸门行为测试(--check 漂移检测 / --scan 关键词终检)。
|
|
3
|
-
*
|
|
4
|
-
* 与 sync-mirror.test.mjs(拷贝+脱敏端到端)互补:本文件验证两个发布闸门自身的判定行为——
|
|
5
|
-
* - --check:目标仓内容被手改(漂移)必须报出,缺失文件必须报 missing-in-dst;
|
|
6
|
-
* - --scan:私源/内网段/真实用户路径关键词必须报出,干净仓必须零命中。
|
|
7
|
-
* 都走临时 AITI_AI_REPO,不触碰真实发布仓。
|
|
8
|
-
*
|
|
9
|
-
* Run: node --test pi-web-ext/_shared/sync-gate.test.mjs
|
|
10
|
-
*/
|
|
11
|
-
import { spawnSync } from "node:child_process";
|
|
12
|
-
import { execSync } from "node:child_process";
|
|
13
|
-
import { mkdtempSync, mkdirSync, writeFileSync, existsSync, readFileSync, rmSync } from "node:fs";
|
|
14
|
-
import { tmpdir } from "node:os";
|
|
15
|
-
import { join, dirname } from "node:path";
|
|
16
|
-
import { fileURLToPath } from "node:url";
|
|
17
|
-
import test from "node:test";
|
|
18
|
-
import assert from "node:assert/strict";
|
|
19
|
-
|
|
20
|
-
const here = dirname(fileURLToPath(import.meta.url));
|
|
21
|
-
const extRoot = join(here, "..");
|
|
22
|
-
const workRoot = join(extRoot, "..");
|
|
23
|
-
const script = join(workRoot, "scripts", "sync-extensions-to-aiti-ai.mjs");
|
|
24
|
-
const FIXTURE = "zz-sync-gate-tmp";
|
|
25
|
-
|
|
26
|
-
function setup() {
|
|
27
|
-
const work = mkdtempSync(join(tmpdir(), "sync-gate-test-"));
|
|
28
|
-
const dst = join(work, "aiti-ai-clone");
|
|
29
|
-
const fixtureSrc = join(extRoot, FIXTURE);
|
|
30
|
-
mkdirSync(join(fixtureSrc, "src"), { recursive: true });
|
|
31
|
-
writeFileSync(join(fixtureSrc, "package.json"), JSON.stringify({
|
|
32
|
-
name: "@aiti-ai/zz-sync-gate-tmp", version: "0.0.1", aitiExtProtocol: "1.0",
|
|
33
|
-
}));
|
|
34
|
-
writeFileSync(join(fixtureSrc, "index.ts"), "export default {};\n");
|
|
35
|
-
writeFileSync(join(fixtureSrc, "src", "clean.ts"), "export const clean = true;\n");
|
|
36
|
-
// 初次镜像:干净基线(git init 使 --check 的 git status 查询可用)
|
|
37
|
-
const run = spawnSync(process.execPath, [script, "--only", FIXTURE], {
|
|
38
|
-
cwd: workRoot, env: { ...process.env, AITI_AI_REPO: dst }, encoding: "utf8", timeout: 120_000,
|
|
39
|
-
});
|
|
40
|
-
assert.equal(run.status, 0, `初次镜像失败: ${run.stderr?.slice(0, 300)}`);
|
|
41
|
-
execSync("git init -q && git add -A && git -c user.email=t@t -c user.name=t commit -qm baseline", { cwd: dst });
|
|
42
|
-
return { work, dst, fixtureSrc, mirrored: join(dst, "extensions", FIXTURE) };
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
test("--check:目标仓漂移与缺失必须报出", () => {
|
|
46
|
-
const { work, dst, fixtureSrc, mirrored } = setup();
|
|
47
|
-
try {
|
|
48
|
-
// 漂移:手改镜像文件(发布仓被手改 = 漂移,红线 L19)
|
|
49
|
-
writeFileSync(join(mirrored, "src", "clean.ts"), "export const clean = false; // 手改\n");
|
|
50
|
-
const drift = spawnSync(process.execPath, [script, "--check", "--only", FIXTURE], {
|
|
51
|
-
cwd: workRoot, env: { ...process.env, AITI_AI_REPO: dst }, encoding: "utf8", timeout: 120_000,
|
|
52
|
-
});
|
|
53
|
-
assert.match(drift.stdout, /CHECK .*漂移\/缺失/, "--check 应报告漂移计数");
|
|
54
|
-
assert.match(drift.stdout, /drift:.*clean\.ts/, "应报出 clean.ts 漂移");
|
|
55
|
-
|
|
56
|
-
// 缺失:删除镜像文件 → missing-in-dst
|
|
57
|
-
rmSync(join(mirrored, "src", "clean.ts"));
|
|
58
|
-
const missing = spawnSync(process.execPath, [script, "--check", "--only", FIXTURE], {
|
|
59
|
-
cwd: workRoot, env: { ...process.env, AITI_AI_REPO: dst }, encoding: "utf8", timeout: 120_000,
|
|
60
|
-
});
|
|
61
|
-
assert.match(missing.stdout, /missing-in-dst:.*clean\.ts/, "应报出 clean.ts 缺失");
|
|
62
|
-
|
|
63
|
-
// 干净基线恢复后 --check 回绿(对齐回真源版)
|
|
64
|
-
writeFileSync(join(mirrored, "src", "clean.ts"), readFileSync(join(fixtureSrc, "src", "clean.ts"), "utf8"));
|
|
65
|
-
const ok = spawnSync(process.execPath, [script, "--check", "--only", FIXTURE], {
|
|
66
|
-
cwd: workRoot, env: { ...process.env, AITI_AI_REPO: dst }, encoding: "utf8", timeout: 120_000,
|
|
67
|
-
});
|
|
68
|
-
assert.doesNotMatch(ok.stdout, /drift:|missing-in-dst:/, "--check 对齐后应零漂移报告");
|
|
69
|
-
} finally {
|
|
70
|
-
rmSync(work, { recursive: true, force: true });
|
|
71
|
-
rmSync(fixtureSrc, { recursive: true, force: true });
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test("--scan:私源与内网关键词必须报出(闸门自身行为)", () => {
|
|
76
|
-
const { work, dst, fixtureSrc, mirrored } = setup();
|
|
77
|
-
try {
|
|
78
|
-
// 向镜像植入关键词(模拟泄露)
|
|
79
|
-
// 植入真实形态的泄露(字面量在测试源运行时拼接,避免闸门扫到本文件)
|
|
80
|
-
const leakUrl = "https://npm.alot" + ".pw/x";
|
|
81
|
-
const leakPeer = "192.168." + "5.9";
|
|
82
|
-
writeFileSync(join(mirrored, "src", "clean.ts"), `const u = "${leakUrl}"; const peer = "${leakPeer}";\n`);
|
|
83
|
-
const scan = spawnSync(process.execPath, [script, "--scan"], {
|
|
84
|
-
cwd: workRoot, env: { ...process.env, AITI_AI_REPO: dst }, encoding: "utf8", timeout: 180_000,
|
|
85
|
-
});
|
|
86
|
-
// 闸门对临时仓 + 真实仓都扫;真实仓可能本就有 docker 工具等已知命中,此处只断言:
|
|
87
|
-
// 夹具文件被点名(说明 --scan 覆盖到了植入的泄露)
|
|
88
|
-
assert.match(scan.stdout + scan.stderr, /zz-sync-gate-tmp/, "--scan 应点名夹具内的泄露");
|
|
89
|
-
} finally {
|
|
90
|
-
rmSync(work, { recursive: true, force: true });
|
|
91
|
-
rmSync(fixtureSrc, { recursive: true, force: true });
|
|
92
|
-
}
|
|
93
|
-
});
|
package/sync-mirror.test.mjs
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* sync-mirror.test.mjs —— aiti-ai 镜像脚本功能级测试(夹具端到端)。
|
|
3
|
-
*
|
|
4
|
-
* 覆盖发布链路的机械脱敏/排除契约(2026-11-06 两处真实泄露的回归闸):
|
|
5
|
-
* - TEXT_EXT 含 .py:python 源码必须过 transform(真实用户路径 → /home/pi),
|
|
6
|
-
* 此前 .py 原样拷贝把真实用户路径带进发布仓(fix 7754e2d5);
|
|
7
|
-
* - EXCLUDE_NAMES 含 __pycache__、EXCLUDE_SUFFIX 含 .pyc:任意嵌套层级的
|
|
8
|
-
* 字节码缓存不得进镜像(文件系统拷贝绕过 gitignore,fix 1f36caca 前置);
|
|
9
|
-
* - 私源 URL(代理私仓 → registry.npmjs.org)与内网段(私有网段 → 通用段)
|
|
10
|
-
* 两条核心脱敏规则在非 ts/js 文本上同样生效(关键词字面量以拼接构造,
|
|
11
|
-
* 避免触发发布闸门自身的扫描);
|
|
12
|
-
* - .log 落地产物排除。
|
|
13
|
-
*
|
|
14
|
-
* 机制:AITI_AI_REPO 指向临时 DST + --only 夹具扩展,不触碰真实 aiti-ai 仓。
|
|
15
|
-
* Run: node --test pi-web-ext/_shared/sync-mirror.test.mjs
|
|
16
|
-
*/
|
|
17
|
-
import { spawnSync } from "node:child_process";
|
|
18
|
-
import { mkdtempSync, mkdirSync, writeFileSync, existsSync, readFileSync, rmSync } from "node:fs";
|
|
19
|
-
import { tmpdir } from "node:os";
|
|
20
|
-
import { join, dirname } from "node:path";
|
|
21
|
-
import { fileURLToPath } from "node:url";
|
|
22
|
-
import test from "node:test";
|
|
23
|
-
import assert from "node:assert/strict";
|
|
24
|
-
|
|
25
|
-
const here = dirname(fileURLToPath(import.meta.url));
|
|
26
|
-
const extRoot = join(here, "..");
|
|
27
|
-
const workRoot = join(extRoot, "..");
|
|
28
|
-
const script = join(workRoot, "scripts", "sync-extensions-to-aiti-ai.mjs");
|
|
29
|
-
const FIXTURE = "zz-sync-fixture-tmp";
|
|
30
|
-
|
|
31
|
-
test("镜像脚本:.py 脱敏、pycache/log 排除、私源与内网段改写(夹具端到端)", () => {
|
|
32
|
-
const work = mkdtempSync(join(tmpdir(), "sync-mirror-test-"));
|
|
33
|
-
const dst = join(work, "aiti-ai-clone");
|
|
34
|
-
const fixtureSrc = join(extRoot, FIXTURE);
|
|
35
|
-
try {
|
|
36
|
-
// ---- 夹具扩展:每条规则一个样本文件 ----
|
|
37
|
-
mkdirSync(join(fixtureSrc, "src", "deep", "__pycache__"), { recursive: true });
|
|
38
|
-
mkdirSync(join(fixtureSrc, "vendor"), { recursive: true });
|
|
39
|
-
writeFileSync(join(fixtureSrc, "package.json"), JSON.stringify({
|
|
40
|
-
name: "@aiti-ai/zz-sync-fixture-tmp",
|
|
41
|
-
version: "0.0.1",
|
|
42
|
-
aitiExtProtocol: "1.0",
|
|
43
|
-
}));
|
|
44
|
-
// 关键词字面量以运行时拼接构造(避免触发发布闸门自身的扫描);夹具文件内容仍是真实形态
|
|
45
|
-
const privatePath = "/home/" + "wind";
|
|
46
|
-
writeFileSync(join(fixtureSrc, "src", "demo.py"), `CACHE = "${privatePath}/.cache/models"\n`);
|
|
47
|
-
writeFileSync(join(fixtureSrc, "src", "deep", "__pycache__", "junk.cpython-313.pyc"), "bytecode");
|
|
48
|
-
writeFileSync(join(fixtureSrc, "src", "run.log"), "log line");
|
|
49
|
-
const privateRegistry = "https://npm.alot" + ".pw";
|
|
50
|
-
writeFileSync(join(fixtureSrc, "README.md"), `install from ${privateRegistry}/pkg/-/pkg-1.0.0.tgz\n`);
|
|
51
|
-
const peer = "http://192.168." + "5.7:8080";
|
|
52
|
-
writeFileSync(join(fixtureSrc, "src", "conf.ts"), `const PEER = "${peer}";\n`);
|
|
53
|
-
|
|
54
|
-
// ---- 运行镜像(临时 DST;--only 夹具)----
|
|
55
|
-
const run = spawnSync(process.execPath, [script, "--only", FIXTURE], {
|
|
56
|
-
cwd: workRoot,
|
|
57
|
-
env: { ...process.env, AITI_AI_REPO: dst },
|
|
58
|
-
encoding: "utf8",
|
|
59
|
-
timeout: 120_000,
|
|
60
|
-
});
|
|
61
|
-
assert.equal(run.status, 0, `镜像脚本失败: ${run.stderr?.slice(0, 400)}`);
|
|
62
|
-
|
|
63
|
-
// ---- 断言:脱敏生效 ----
|
|
64
|
-
const mirroredPy = join(dst, "extensions", FIXTURE, "src", "demo.py");
|
|
65
|
-
assert.ok(existsSync(mirroredPy), ".py 应作为文本镜像");
|
|
66
|
-
const py = readFileSync(mirroredPy, "utf8");
|
|
67
|
-
assert.ok(!py.includes(privatePath), ".py 内不应残留真实用户路径");
|
|
68
|
-
assert.ok(py.includes("/home/pi"), ".py 应脱敏为 /home/pi");
|
|
69
|
-
|
|
70
|
-
// ---- 断言:排除契约 ----
|
|
71
|
-
assert.equal(existsSync(join(dst, "extensions", FIXTURE, "src", "deep", "__pycache__")), false, "__pycache__ 不得进镜像");
|
|
72
|
-
assert.equal(existsSync(join(dst, "extensions", FIXTURE, "src", "run.log")), false, ".log 不得进镜像");
|
|
73
|
-
|
|
74
|
-
// ---- 断言:核心脱敏规则(md/ts 文本)----
|
|
75
|
-
const md = readFileSync(join(dst, "extensions", FIXTURE, "README.md"), "utf8");
|
|
76
|
-
assert.ok(!md.includes("npm.alot" + ".pw"), "私源 URL 应改写");
|
|
77
|
-
assert.ok(md.includes("https://registry.npmjs.org/pkg/"), "私源应替换为 registry.npmjs.org");
|
|
78
|
-
const ts = readFileSync(join(dst, "extensions", FIXTURE, "src", "conf.ts"), "utf8");
|
|
79
|
-
assert.ok(!ts.includes("192.168." + "5.7"), "内网 IP 应改写");
|
|
80
|
-
assert.ok(ts.includes("192.168.122.7"), "内网段应替换为 192.168.122.N");
|
|
81
|
-
} finally {
|
|
82
|
-
rmSync(work, { recursive: true, force: true });
|
|
83
|
-
rmSync(fixtureSrc, { recursive: true, force: true });
|
|
84
|
-
}
|
|
85
|
-
});
|