@linkdesk/plugin-sdk 0.1.46 → 0.1.48

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.
@@ -0,0 +1,19 @@
1
+ /**
2
+ * 共享测试地基——mock `window.linkdesk` 的最小面。
3
+ *
4
+ * 测试跑在 Node.js / jsdom,没有 Electron preload 注入的 `window.linkdesk`;而插件代码直接依赖它
5
+ * ⇒ 测试环境必须提供最小 mock。**本模块是壳仓与全部插件仓共用的唯一真源**:
6
+ *
7
+ * - **插件仓**:`vitest.setup.ts` 是一行指针 ⇒ `import "@linkdesk/plugin-sdk/vitest-setup";`
8
+ * - **壳仓**:走同仓相对路径引**本文件的源码**(壳没有 `@linkdesk/plugin-sdk` 依赖,
9
+ * 也不该有)——两处形态**有意不同**,真源只有这一份。
10
+ *
11
+ * 🔴 **插件专属的桩不许加进来**(如 `window.linkdesk.serial`):那些住各仓自己的测试文件里,
12
+ * 用 `vi.fn()` 按需覆盖。这里只放「凡碰 `window.linkdesk` 的测试都用得到」的那六个命名空间。
13
+ * 加专属桩 = 让共享地基替某一只插件说话,下一只插件就要为它付代价。
14
+ *
15
+ * ⚠️ 改这个文件 = 改**全部插件仓 + 壳仓**几十个测试文件的运行环境:语义只许**向后兼容地加**,
16
+ * 不许删键、不许改既有替身的行为(改了会让别仓既有测试的期望值静默错位)。
17
+ */
18
+ export {};
19
+ //# sourceMappingURL=vitest-setup.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vitest-setup.d.ts","sourceRoot":"","sources":["../src/vitest-setup.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG"}
@@ -0,0 +1,94 @@
1
+ /**
2
+ * 共享测试地基——mock `window.linkdesk` 的最小面。
3
+ *
4
+ * 测试跑在 Node.js / jsdom,没有 Electron preload 注入的 `window.linkdesk`;而插件代码直接依赖它
5
+ * ⇒ 测试环境必须提供最小 mock。**本模块是壳仓与全部插件仓共用的唯一真源**:
6
+ *
7
+ * - **插件仓**:`vitest.setup.ts` 是一行指针 ⇒ `import "@linkdesk/plugin-sdk/vitest-setup";`
8
+ * - **壳仓**:走同仓相对路径引**本文件的源码**(壳没有 `@linkdesk/plugin-sdk` 依赖,
9
+ * 也不该有)——两处形态**有意不同**,真源只有这一份。
10
+ *
11
+ * 🔴 **插件专属的桩不许加进来**(如 `window.linkdesk.serial`):那些住各仓自己的测试文件里,
12
+ * 用 `vi.fn()` 按需覆盖。这里只放「凡碰 `window.linkdesk` 的测试都用得到」的那六个命名空间。
13
+ * 加专属桩 = 让共享地基替某一只插件说话,下一只插件就要为它付代价。
14
+ *
15
+ * ⚠️ 改这个文件 = 改**全部插件仓 + 壳仓**几十个测试文件的运行环境:语义只许**向后兼容地加**,
16
+ * 不许删键、不许改既有替身的行为(改了会让别仓既有测试的期望值静默错位)。
17
+ */
18
+ // path 纯函数——直接实现,不走 IPC
19
+ const pathMock = {
20
+ normalize: (p) => p.replace(/\\/g, "/"),
21
+ join: (...parts) => parts.map((p) => String(p).replace(/\\/g, "/")).join("/").replace(/\/+/g, "/"),
22
+ basename: (p) => {
23
+ const s = p.replace(/\\/g, "/").split("/");
24
+ return s[s.length - 1] || "";
25
+ },
26
+ dirname: (p) => {
27
+ const s = p.replace(/\\/g, "/").split("/");
28
+ s.pop();
29
+ return s.join("/") || ".";
30
+ },
31
+ extname: (p) => {
32
+ const b = p.replace(/\\/g, "/").split("/").pop() || "";
33
+ const i = b.lastIndexOf(".");
34
+ return i > 0 ? b.slice(i) : "";
35
+ },
36
+ };
37
+ const _g = globalThis;
38
+ // configuration——默认返回 null,测试中按需 mock。
39
+ // __ldkConfigStore 暴露给测试——测试可直接设置值控制 get() 返回。
40
+ const _configStore = (_g.__ldkConfigStore = new Map());
41
+ const configurationMock = {
42
+ get: async (key) => _configStore.get(key) ?? null,
43
+ set: async (key, v) => { _configStore.set(key, v); },
44
+ onChange: (_key, _cb) => {
45
+ return () => { }; // no-op unsubscribe
46
+ },
47
+ };
48
+ // workspace——默认返回空工作区
49
+ const workspaceMock = {
50
+ getFolders: async () => [],
51
+ getActive: async () => undefined,
52
+ };
53
+ // filesystem——可替换的最小 stub。测试可覆盖 lk.filesystem.xxx = vi.fn() 按需定制
54
+ const filesystemMock = {
55
+ readTextFile: async (_p) => "",
56
+ writeTextFile: async (_p, _d) => { },
57
+ readBinaryFile: async (_p) => new Uint8Array(),
58
+ writeBinaryFile: async (_p, _d) => { },
59
+ listDir: async (_p) => [],
60
+ exists: async (_p) => false,
61
+ mkdir: async (_p) => { },
62
+ copy: async (_src, _dest) => { },
63
+ remove: async (_p) => { },
64
+ watch: async (_dirPath, _onEvent) => {
65
+ return () => { }; // unsubscribe
66
+ },
67
+ };
68
+ // tabs——最小 stub
69
+ const tabsMock = {
70
+ create: async (_type, _opts) => "tab-1",
71
+ openOrFocus: async (_type, _opts) => "tab-1",
72
+ focus: async (_tabId) => { },
73
+ close: async (_tabId) => { },
74
+ focusBySourceId: async (_sourceId) => { },
75
+ updateLabelBySourceId: async (_sourceId, _label) => { },
76
+ closeBySourceId: async (_sourceId) => { },
77
+ };
78
+ // 最小 mock——故意不满足 LinkDeskAPI 全契约(测试按需覆盖),经 linkdesk?: object 窄口赋值
79
+ _g.window = _g.window ?? {};
80
+ _g.window.linkdesk = {
81
+ path: pathMock,
82
+ configuration: configurationMock,
83
+ config: configurationMock,
84
+ workspace: workspaceMock,
85
+ filesystem: filesystemMock,
86
+ tabs: tabsMock,
87
+ // event stubs
88
+ events: {
89
+ on: () => () => { },
90
+ emit: () => { },
91
+ },
92
+ };
93
+ export {};
94
+ //# sourceMappingURL=vitest-setup.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vitest-setup.js","sourceRoot":"","sources":["../src/vitest-setup.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,wBAAwB;AACxB,MAAM,QAAQ,GAAG;IACf,SAAS,EAAE,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CA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 "version": "0.1.48",
4
4
  "description": "@linkdesk/plugin-sdk——LinkDesk 插件作者 SDK(对标 @types/vscode):window.linkdesk.* 类型 + 一键 build(.linkdesk-plugin) + dev 宿主 + plugin.json 验证 + eslint 门禁(E6#54d)。作者 npm i 后零壳源码依赖。",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -14,12 +14,22 @@
14
14
  "types": "./dist/eslint/index.d.ts",
15
15
  "default": "./dist/eslint/index.js"
16
16
  },
17
+ "./vitest-setup": {
18
+ "types": "./dist/vitest-setup.d.ts",
19
+ "default": "./dist/vitest-setup.js"
20
+ },
21
+ "./test-audit": {
22
+ "types": "./test-audit.d.ts",
23
+ "default": "./test-audit.mjs"
24
+ },
17
25
  "./package.json": "./package.json"
18
26
  },
19
27
  "files": [
20
28
  "dist",
21
29
  "schemas",
22
- "dev-host"
30
+ "dev-host",
31
+ "test-audit.mjs",
32
+ "test-audit.d.ts"
23
33
  ],
24
34
  "sideEffects": false,
25
35
  "bin": {
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @linkdesk/plugin-sdk/test-audit 的类型声明(实现 = 同目录 test-audit.mjs,免构建直发)。
3
+ * 口径与判据说明见 .mjs 头注——那里是单一真源,这里不重述。
4
+ */
5
+
6
+ export interface AuditedUnit {
7
+ /** 仓相对路径(POSIX 斜杠,不含 .ts 后缀) */
8
+ unit: string;
9
+ /** 判据来源标注(basename-miss / reference-miss) */
10
+ evidence: string[];
11
+ }
12
+
13
+ export interface RepoAudit {
14
+ /** logic = 有纯逻辑单元;exempt = 纯声明式/零逻辑(豁免理由见 exempt);third-party = 第三方仓(消费方只读报出) */
15
+ kind: "logic" | "exempt" | "third-party";
16
+ exempt: string | null;
17
+ prodLines: number;
18
+ testLines: number;
19
+ testFiles: number;
20
+ /** 宽口径单元数 */
21
+ wideUnits: number;
22
+ /** 纯逻辑单元数(严口径) */
23
+ logicUnits: number;
24
+ zeroTest: AuditedUnit[];
25
+ coveredBasenameOnly: string[];
26
+ coveredReferenceOnly: string[];
27
+ coveredBoth: string[];
28
+ }
29
+
30
+ export declare const AUDIT_CALIBER: Record<string, string>;
31
+
32
+ export declare function analyzeRepo(repoDir: string, opts?: { official?: boolean }): RepoAudit;
package/test-audit.mjs ADDED
@@ -0,0 +1,171 @@
1
+ /**
2
+ * @linkdesk/plugin-sdk/test-audit——插件仓「测试覆盖」结构审计核心(单一真源)。
3
+ *
4
+ * ── 谁在用同一份 ──
5
+ * · 官方覆盖尺(壳仓 `scripts/audit-plugin-tests.mjs`,19 仓只读盘点、只报不拦);
6
+ * · 插件仓 CI 第六段(脚手架模板 `scripts/ci-verify.mjs` ⑥,E6#155 步二起**判红**——2026-09-26 用户拍板)。
7
+ * ⛔ 别在任何一处复制这套判据——判据漂移 = 两把尺子打架,假红会让真红失效。
8
+ *
9
+ * ── 口径(与插件测试覆盖层 00-整理档案 §〇b 同源;立案 2026-09-25,本核心 2026-09-26 落地)──
10
+ * · 生产行 = src 下全部 .ts / .tsx / .css(任意深度)去测试文件,逐文件按行切分求和(含末行换行)。
11
+ * · **纯逻辑单元** = src 下任意深度的 .ts 去 .d.ts / 测试文件 / barrel(`types.ts`·`index.ts`·`constants.ts`)
12
+ * 再减 `use*` hook / 内容含 `window.linkdesk` / 含 React 导入(.tsx 本就不在口径内)。
13
+ * · 覆盖命中(两条任一即算):① 同名测试文件(基名 = 单元名 ∨ 所在目录名);
14
+ * ② 任一测试文件的模块说明符引用到它(`from`/`import()`/`require()`/`vi.mock*`,含目录桶 index)。
15
+ * ⚠️ 已知盲区:只跟一跳——`A ← B ← 测试` 的传递覆盖看不见(首跑唯一官方命中即此类「一跳传递假红」)。
16
+ * ⇒ **零测报出 ≠ 判死**:消费方判红信息必须带「先核覆盖再补测」的提示,别逼作者写无意义测试。
17
+ *
18
+ * ── 豁免 ──
19
+ * 纯声明式仓(无 `src/`)与零逻辑空壳(entry 头注自述「零逻辑」)⇒ `kind: "exempt"` + 有据豁免理由。
20
+ * ⛔ 不做硬编码白名单——豁免必须长在结构事实或仓内自述上。
21
+ */
22
+
23
+ import fs from "node:fs";
24
+ import path from "node:path";
25
+
26
+ /** barrel 三件套(无逻辑、只搬运/声明)。 */
27
+ const BARRELS = new Set(["types.ts", "index.ts", "constants.ts"]);
28
+ const SKIP_DIRS = new Set(["node_modules", ".git", "dist", "release", "resources"]);
29
+
30
+ /** 审计口径的机器可读版——报告与判红信息直接引用,别再手抄一份。 */
31
+ export const AUDIT_CALIBER = {
32
+ prodLines: "src/**/*.{ts,tsx,css} 去 *.test.ts(x),逐文件 split(/\\r?\\n/).length 求和(含末行换行)",
33
+ testLines: "src/**/*.test.ts(x)",
34
+ wideUnits: "src/**/*.ts 去 .d.ts / *.test.ts / barrel(types|index|constants)",
35
+ logicUnits: "宽口径再减 use* hook / 含 window.linkdesk / 含 React 导入",
36
+ hit: "① 同名测试文件(基名 = 单元名 ∨ 目录名)∨ ② 任一测试文件的模块说明符引用(含目录桶)",
37
+ zeroTestNote: "零测 = 待裁决,不是判死——同名判据看不见跨文件覆盖,请逐条核",
38
+ };
39
+
40
+ const isTestFile = (p) => /\.(test|spec)\.tsx?$/.test(p);
41
+ /** 计行 = 含末行换行(逐文件 split 后求和)。 */
42
+ const countLines = (p) => fs.readFileSync(p, "utf8").split(/\r?\n/).length;
43
+ const read = (p) => fs.readFileSync(p, "utf8");
44
+
45
+ function walk(dir, filter, out = []) {
46
+ let ents;
47
+ try {
48
+ ents = fs.readdirSync(dir, { withFileTypes: true });
49
+ } catch {
50
+ return out;
51
+ }
52
+ for (const e of ents) {
53
+ if (SKIP_DIRS.has(e.name)) continue;
54
+ const p = path.join(dir, e.name);
55
+ if (e.isDirectory()) walk(p, filter, out);
56
+ else if (filter(p)) out.push(p);
57
+ }
58
+ return out;
59
+ }
60
+
61
+ /** 严口径:`.ts` 单元再减 `use*` hook / 含 `window.linkdesk` / 含 React 导入。 */
62
+ function isPureLogic(file) {
63
+ if (/^use/.test(path.basename(file))) return false;
64
+ const text = read(file);
65
+ if (text.includes("window.linkdesk")) return false;
66
+ if (/\bfrom\s+["']react["']/.test(text)) return false;
67
+ if (/\brequire\(\s*["']react["']\s*\)/.test(text)) return false;
68
+ if (/\bimport\s+React\b/.test(text)) return false;
69
+ return true;
70
+ }
71
+
72
+ /** 测试文件里出现的模块说明符(from / import() / require() / vi.mock 的字符串)。 */
73
+ function specifiersOf(text) {
74
+ const out = [];
75
+ const re = /(?:from|import|require|vi\.mock|vi\.doMock|vi\.importActual|vi\.importMock)\s*\(?\s*["']([^"']+)["']/g;
76
+ let m;
77
+ while ((m = re.exec(text))) out.push(m[1]);
78
+ return out;
79
+ }
80
+
81
+ /** 一个单元「被引用」的全部可能写法:路径 ∨ 基名 ∨ 目录桶(含 index)。 */
82
+ function unitSpecifiers(unitPath, srcDir) {
83
+ const rel = path.relative(srcDir, unitPath).split(path.sep).join("/").replace(/\.ts$/, "");
84
+ const base = path.posix.basename(rel);
85
+ const dirRel = path.posix.dirname(rel);
86
+ const specs = new Set([rel, base, `${rel}.ts`, `${base}.ts`, `${base}.js`]);
87
+ const dirAbs = path.dirname(unitPath);
88
+ if (fs.existsSync(path.join(dirAbs, "index.ts")) || fs.existsSync(path.join(dirAbs, "index.tsx"))) {
89
+ specs.add(dirRel);
90
+ specs.add(`${dirRel}/index`);
91
+ }
92
+ return { rel, specs: [...specs] };
93
+ }
94
+
95
+ /** 声明式 / 零逻辑仓的**有据豁免**:先看结构事实,再看 entry 头注自述(⛔ 不硬编码白名单)。 */
96
+ function exemptReason(repoDir, srcExists, wideCount) {
97
+ if (!srcExists) return "不适用(纯声明式:无 `src/`,无可测单元;门禁 = `ci-verify.mjs` 的结构与声明判据)";
98
+ if (wideCount === 0) {
99
+ for (const entry of ["src/index.tsx", "src/index.ts"]) {
100
+ const p = path.join(repoDir, entry);
101
+ if (!fs.existsSync(p)) continue;
102
+ const quote = read(p)
103
+ .split(/\r?\n/)
104
+ .slice(0, 40)
105
+ .find((l) => l.includes("零逻辑"));
106
+ if (quote) return `不适用(零逻辑空壳 entry——${entry} 头注自述「${quote.replace(/^[\s*/]+/, "").trim()}」)`;
107
+ }
108
+ return "不适用(零逻辑空壳 entry:`src/` 下无非 barrel `.ts` 单元)";
109
+ }
110
+ return null;
111
+ }
112
+
113
+ /**
114
+ * 审计一只插件仓(只读,不写任何文件)。
115
+ * @param {string} repoDir 插件仓根(含 plugin.json 与 src/)
116
+ * @param {{ official?: boolean }} opts official=false ⇒ 第三方仓形态(kind: "third-party",
117
+ * 消费方应只读报出、不代改);默认 true。仓内结构事实判出的豁免见 `exempt`。
118
+ * @returns {{ kind: "logic"|"exempt"|"third-party", exempt: string|null, prodLines: number,
119
+ * testLines: number, testFiles: number, wideUnits: number, logicUnits: number,
120
+ * zeroTest: Array<{unit: string, evidence: string[]}>, coveredBasenameOnly: string[],
121
+ * coveredReferenceOnly: string[], coveredBoth: string[] }}
122
+ */
123
+ export function analyzeRepo(repoDir, opts = {}) {
124
+ const official = opts.official !== false;
125
+ const srcDir = path.join(repoDir, "src");
126
+ const srcExists = fs.existsSync(srcDir);
127
+ const srcFiles = srcExists ? walk(srcDir, (p) => /\.(ts|tsx|css)$/.test(p)) : [];
128
+ const testFiles = srcFiles.filter(isTestFile);
129
+ const prodFiles = srcFiles.filter((p) => !isTestFile(p));
130
+ const candidates = srcFiles.filter(
131
+ (p) => p.endsWith(".ts") && !p.endsWith(".d.ts") && !isTestFile(p) && !BARRELS.has(path.basename(p)),
132
+ );
133
+ const units = candidates.filter(isPureLogic);
134
+
135
+ const prodLines = prodFiles.reduce((a, p) => a + countLines(p), 0);
136
+ const testLines = testFiles.reduce((a, p) => a + countLines(p), 0);
137
+ const testBaseNames = new Set(testFiles.map((p) => path.basename(p).replace(/\.(test|spec)\.tsx?$/, "")));
138
+ const testSpecs = testFiles.map((p) => specifiersOf(read(p)));
139
+
140
+ const judged = units.map((p) => {
141
+ const base = path.basename(p, ".ts");
142
+ const parent = path.basename(path.dirname(p));
143
+ const hitBasename = testBaseNames.has(base) || testBaseNames.has(parent);
144
+ const { rel, specs } = unitSpecifiers(p, srcDir);
145
+ const hitReference = testSpecs.some((list) => list.some((s) => specs.some((c) => s === c || s.endsWith(`/${c}`))));
146
+ return { unit: rel, hitBasename, hitReference, covered: hitBasename || hitReference };
147
+ });
148
+ const zeroTest = judged.filter((j) => !j.covered);
149
+
150
+ let kind = "logic";
151
+ let exempt = null;
152
+ if (!official) kind = "third-party";
153
+ else if (!srcExists || candidates.length === 0) {
154
+ kind = "exempt";
155
+ exempt = exemptReason(repoDir, srcExists, candidates.length);
156
+ }
157
+
158
+ return {
159
+ kind,
160
+ exempt,
161
+ prodLines,
162
+ testLines,
163
+ testFiles: testFiles.length,
164
+ wideUnits: candidates.length,
165
+ logicUnits: units.length,
166
+ zeroTest: zeroTest.map((j) => ({ unit: j.unit, evidence: ["basename-miss", "reference-miss"] })),
167
+ coveredBasenameOnly: judged.filter((j) => j.hitBasename && !j.hitReference).map((j) => j.unit),
168
+ coveredReferenceOnly: judged.filter((j) => !j.hitBasename && j.hitReference).map((j) => j.unit),
169
+ coveredBoth: judged.filter((j) => j.hitBasename && j.hitReference).map((j) => j.unit),
170
+ };
171
+ }