dsh-config-manager 0.7.0 → 0.8.0

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.
Files changed (85) hide show
  1. package/lib/adapters/test-helpers.d.ts +7 -0
  2. package/lib/adapters/test-helpers.js +11 -0
  3. package/lib/cli/actions.js +1 -1
  4. package/lib/client.d.ts +9 -2
  5. package/lib/client.js +685 -649
  6. package/lib/core/backup.d.ts +9 -1
  7. package/lib/core/backup.js +21 -0
  8. package/lib/core/messages-core.d.ts +3 -0
  9. package/lib/core/messages-core.js +6 -0
  10. package/lib/core/messages.d.ts +5 -0
  11. package/lib/core/messages.js +10 -0
  12. package/lib/core/model-tools.js +2 -0
  13. package/lib/core/phase4-crash-child.js +1 -1
  14. package/lib/core/plugin-cli.d.ts +11 -0
  15. package/lib/core/plugin-cli.js +30 -0
  16. package/lib/core/restore-wal.d.ts +2 -0
  17. package/lib/core/restore-wal.js +11 -6
  18. package/lib/core/restore.d.ts +15 -2
  19. package/lib/core/restore.js +65 -3
  20. package/lib/core/snapshot-diff.js +2 -1
  21. package/lib/core/types.d.ts +32 -0
  22. package/lib/index.d.ts +6 -1
  23. package/lib/index.js +17 -16
  24. package/lib/routes/route-support.d.ts +5 -0
  25. package/lib/routes/route-support.js +28 -2
  26. package/lib/routes/sync.js +3 -0
  27. package/lib/sync/merge-preview-view.d.ts +78 -0
  28. package/lib/sync/merge-preview-view.js +46 -0
  29. package/lib/sync/merge.d.ts +2 -1
  30. package/lib/sync/merge.js +9 -4
  31. package/lib/sync/plugin-removal-apply.d.ts +95 -0
  32. package/lib/sync/plugin-removal-apply.js +158 -0
  33. package/lib/sync/plugin-removals.d.ts +90 -0
  34. package/lib/sync/plugin-removals.js +167 -0
  35. package/lib/sync/sync-engine.d.ts +34 -43
  36. package/lib/sync/sync-engine.js +50 -42
  37. package/lib/sync/sync-selection.d.ts +8 -0
  38. package/lib/sync/sync-selection.js +6 -0
  39. package/lib/sync/workspace-merge.d.ts +8 -1
  40. package/lib/ui/restore-plan-view.js +1 -0
  41. package/lib/ui/sync-settings-view.d.ts +10 -0
  42. package/lib/ui/sync-settings-view.js +2 -0
  43. package/package.json +1 -1
  44. package/src/adapters/test-helpers.ts +10 -0
  45. package/src/cli/actions.ts +1 -1
  46. package/src/client/common/info-hint-guard.test.ts +2 -1
  47. package/src/client/run-store.test.ts +7 -5
  48. package/src/client/run-store.ts +4 -5
  49. package/src/client/sync/SyncPanel.tsx +10 -0
  50. package/src/client/sync/SyncSectionPickerDialog.tsx +17 -1
  51. package/src/client/sync/sync-locales.ts +4 -0
  52. package/src/client/sync/sync-view.ts +6 -0
  53. package/src/core/backup.ts +21 -1
  54. package/src/core/messages-core.ts +6 -0
  55. package/src/core/messages.ts +10 -0
  56. package/src/core/model-tools.ts +2 -0
  57. package/src/core/phase4-crash-child.ts +1 -1
  58. package/src/core/plugin-cli.ts +37 -0
  59. package/src/core/restore-plugin-install.test.ts +116 -0
  60. package/src/core/restore-wal.ts +12 -6
  61. package/src/core/restore.ts +79 -4
  62. package/src/core/smoke.test.ts +5 -0
  63. package/src/core/snapshot-diff.test.ts +1 -1
  64. package/src/core/snapshot-diff.ts +2 -1
  65. package/src/core/types.ts +31 -0
  66. package/src/index.ts +21 -21
  67. package/src/routes/route-support.ts +27 -2
  68. package/src/routes/sync.ts +3 -0
  69. package/src/security/security.test.ts +1 -0
  70. package/src/sync/merge-preview-view.ts +97 -0
  71. package/src/sync/merge.test.ts +17 -15
  72. package/src/sync/merge.ts +10 -3
  73. package/src/sync/plugin-removal-apply.ts +198 -0
  74. package/src/sync/plugin-removals.test.ts +221 -0
  75. package/src/sync/plugin-removals.ts +208 -0
  76. package/src/sync/sync-engine.test.ts +251 -1
  77. package/src/sync/sync-engine.ts +84 -83
  78. package/src/sync/sync-selection.test.ts +29 -7
  79. package/src/sync/sync-selection.ts +13 -0
  80. package/src/sync/workspace-merge.ts +9 -0
  81. package/src/ui/restore-plan-view.test.ts +1 -1
  82. package/src/ui/restore-plan-view.ts +1 -0
  83. package/src/ui/snapshots-view.test.ts +1 -1
  84. package/src/ui/sync-settings-view.test.ts +20 -0
  85. package/src/ui/sync-settings-view.ts +12 -0
@@ -0,0 +1,208 @@
1
+ /**
2
+ * #132:同步「跟随远端删除插件」的**唯一判据**与 plugins 分区的按名三方合并。
3
+ *
4
+ * 为什么必须单独一个模块:
5
+ * 1. 删除判定是纯逻辑(开关 × 祖先 × 远端 × 本机),散进引擎里既测不了也说不清;
6
+ * 2. plugins 分区不能走通用 JSON 合并 —— `merge.ts` 的 `mergeJsonValues` 对数组一律「两侧都改
7
+ * 且不逐字相等 → 冲突」,而本分区的日常形态恰恰是「A 机卸载了 X、B 机新装了 Y」,通用路径
8
+ * 会把它报成整分区冲突,于是删除永远进不了自动应用(issue #132 的行为表 A→B 走不通)。
9
+ *
10
+ * 删除判据(缺一不可,全部满足才删):
11
+ * - 开关打开(`SyncSelection.deletePlugins`,缺省关);
12
+ * - **祖先**里有它(A 机那轮同步见过的状态)——没有祖先就没有「谁删了它」的证据;
13
+ * - **远端**没有它(= 有人在另一台机器上卸载了);
14
+ * - **本机装着**(卸载才有意义);
15
+ * - 本机版本**仍等于祖先版本**(本地改过 = 用户在自己这台机器上动过它,绝不替用户做决定,
16
+ * 报 `version-changed` 冲突交人裁决);
17
+ * - 不是本插件自己、不是 bundlesOnly 条目(后者由 DSH 自身安装位置解析,`dsh plugin remove`
18
+ * 对它无意义,见 `core/plugin-cli.ts` 的 `isBundlesOnlyEntry`)。
19
+ *
20
+ * 纯函数、零 I/O:引擎调用它拿计划,UI 调用它拿「将删除」清单。
21
+ */
22
+ import type { PluginEntry, PluginsSection, SectionData } from '../schema/types.ts';
23
+ import type { MergeConflict } from './merge.ts';
24
+ import type { GranularMergeOutcome } from './workspace-merge.ts';
25
+ import { isBundlesOnlyEntry } from '../core/plugin-cli.ts';
26
+
27
+ /** 一条「该卸载」的结论(version = 祖先/本机一致的版本,用于报告与回滚重装目标)。 */
28
+ export interface PluginRemoval {
29
+ name: string;
30
+ version: string;
31
+ /** 祖先条目里的安装源(github:/git+/file:/link: 等);重装目标要用它,否则装不回同一来源 */
32
+ spec?: string;
33
+ }
34
+
35
+ /** 未删的原因:每一条都要能对用户解释(报告里逐项列出,绝不静默跳过)。 */
36
+ export type PluginRemovalKeptReason = 'still-in-remote' | 'self' | 'bundles-only' | 'not-installed' | 'switch-off';
37
+
38
+ export interface PluginRemovalConflict {
39
+ name: string;
40
+ reason: 'version-changed';
41
+ ancestorVersion: string;
42
+ localVersion: string;
43
+ }
44
+
45
+ export interface PluginRemovalPlan {
46
+ /** 该执行 `dsh plugin remove` 的插件(顺序 = 祖先顺序,便于报告比对) */
47
+ removals: PluginRemoval[];
48
+ /** 本机改过、远端删了 → 交人裁决(不删也不静默保留) */
49
+ conflicts: PluginRemovalConflict[];
50
+ /** 考察过但不删的条目及原因 */
51
+ kept: { name: string; reason: PluginRemovalKeptReason }[];
52
+ }
53
+
54
+ /**
55
+ * 判据只需要这几个字段:`PluginEntry` 与宿主的 `PluginInfo`(listInstalled 的返回值)都能直接传入,
56
+ * 不必为了调用它先拼一个完整的分区条目。
57
+ */
58
+ export interface PluginRemovalEntry {
59
+ name: string;
60
+ version: string;
61
+ spec?: string;
62
+ bundlesOnly?: boolean;
63
+ }
64
+
65
+ export interface PluginRemovalInput {
66
+ /** 同步选择里的开关(缺省关);关 = 一个都不删 */
67
+ enabled: boolean;
68
+ /** 共同祖先快照里的插件清单(undefined = 没有祖先副本) */
69
+ ancestor: readonly PluginRemovalEntry[] | undefined;
70
+ /** 远端快照里的插件清单(undefined = 远端没有该分区) */
71
+ remote: readonly PluginRemovalEntry[] | undefined;
72
+ /** 本机当前已装(plugins 分区导出的口径) */
73
+ local: readonly PluginRemovalEntry[];
74
+ /** 本插件自己的包名(永不删) */
75
+ selfName: string | undefined;
76
+ }
77
+
78
+ function byName<T extends { name: string }>(list: readonly T[] | undefined): Map<string, T> {
79
+ const map = new Map<string, T>();
80
+ for (const p of list ?? []) {
81
+ if (p !== null && typeof p === 'object' && typeof p.name === 'string' && p.name !== '') map.set(p.name, p);
82
+ }
83
+ return map;
84
+ }
85
+
86
+ /** 判定「该不该删」。调用方只在 `applied` 含 plugins 时才真正执行。 */
87
+ export function planPluginRemovals(input: PluginRemovalInput): PluginRemovalPlan {
88
+ const plan: PluginRemovalPlan = { removals: [], conflicts: [], kept: [] };
89
+ if (input.ancestor === undefined) return plan; // 没有祖先:无删除语义(无法区分「对方删了」与「本机独有」)
90
+ const remote = byName(input.remote);
91
+ const local = byName(input.local);
92
+ for (const a of input.ancestor) {
93
+ const name = a.name;
94
+ if (typeof name !== 'string' || name === '') continue;
95
+ if (remote.has(name)) { plan.kept.push({ name, reason: 'still-in-remote' }); continue; }
96
+ if (input.selfName !== undefined && name === input.selfName) { plan.kept.push({ name, reason: 'self' }); continue; }
97
+ const l = local.get(name);
98
+ if (isBundlesOnlyEntry(a) || (l !== undefined && isBundlesOnlyEntry(l))) {
99
+ plan.kept.push({ name, reason: 'bundles-only' });
100
+ continue;
101
+ }
102
+ if (l === undefined) { plan.kept.push({ name, reason: 'not-installed' }); continue; }
103
+ if (!input.enabled) { plan.kept.push({ name, reason: 'switch-off' }); continue; }
104
+ if (l.version !== a.version) {
105
+ plan.conflicts.push({ name, reason: 'version-changed', ancestorVersion: a.version, localVersion: l.version });
106
+ continue;
107
+ }
108
+ plan.removals.push({ name, version: a.version, ...(a.spec === undefined ? {} : { spec: a.spec }) });
109
+ }
110
+ return plan;
111
+ }
112
+
113
+ /* --------------------------------------------------------------- 分区级三方合并 */
114
+
115
+ /** 形状闸门:不认识就交回通用路径(畸形数据由通用路径拒绝,这里不另造一套判定)。 */
116
+ function asPlugins(data: SectionData | undefined): PluginsSection | undefined {
117
+ if (data === null || typeof data !== 'object') return undefined;
118
+ const obj = data as { version?: unknown; plugins?: unknown };
119
+ if (obj.version !== 1 || !Array.isArray(obj.plugins)) return undefined;
120
+ return data as PluginsSection;
121
+ }
122
+
123
+ /** 决定「同一个插件是否还是同一个东西」的身份字段(version + 来源 spec + bundlesOnly 形态)。 */
124
+ function entryKey(e: PluginEntry): string {
125
+ return [e.version ?? '', e.spec ?? '', e.bundlesOnly === true ? '1' : '0'].join('\u0000');
126
+ }
127
+
128
+ /** 插件列表以外的字段(patch 行 / pnpm-workspace 原文 / 本地 tarball / patch 文件)必须两侧相等才介入。 */
129
+ function sameShell(l: PluginsSection, r: PluginsSection): boolean {
130
+ const pick = (s: PluginsSection): string => JSON.stringify({
131
+ patch: s.patch ?? [],
132
+ pnpmWorkspace: s.pnpmWorkspace ?? null,
133
+ localTarballs: s.localTarballs ?? [],
134
+ patchFiles: s.patchFiles ?? [],
135
+ });
136
+ return pick(l) === pick(r);
137
+ }
138
+
139
+ /**
140
+ * plugins 分区的按名三方合并(`merge.ts` 的 `GRANULAR_MERGES.plugins`)。
141
+ *
142
+ * 逐名规则与 git 同口径,祖先只用于判断「谁动了它」,绝不猜:
143
+ * - 两侧都有:身份相同 → 保留;某一侧与祖先相同 → 取另一侧;两侧都变(或没有祖先)→ both-modified 冲突;
144
+ * - 只有本机:有祖先且与祖先相同 → 跟随远端删除(真删除);无祖先 → 本机独有,保留;有祖先且不同 →
145
+ * modify-delete 冲突(用户在 A 机卸载、B 机却升级过它,不能替用户选);
146
+ * - 只有远端:有祖先且与祖先相同 → 本机删除,跟随(不复活);无祖先 → 远端独有,采纳;有祖先且不同 →
147
+ * delete-modify 冲突;
148
+ * - 两侧都没有:消失。
149
+ *
150
+ * 插件列表以外的字段不一致时返回 `undefined`:patch 行/pnpm-workspace 有各自的既有口径,
151
+ * 在这里再造一套合并只会和通用路径打架(通用路径会整分区处理,其中包含 plugins 数组的旧行为)。
152
+ */
153
+ export function mergePluginsSection(
154
+ local: SectionData,
155
+ remote: SectionData,
156
+ ancestor: SectionData | undefined,
157
+ hooks: { deletePlugins?: boolean } = {},
158
+ ): GranularMergeOutcome | undefined {
159
+ // 开关关着 → 钩子不参与(交回通用合并 = 与 #132 之前逐字一致)。若照常「跟随远端删除」,
160
+ // 合并结果会丢掉本机条目:插件包还在盘上,下一轮导出会把它当「本机独有」推回远端 = 复活。
161
+ if (hooks.deletePlugins !== true) return undefined;
162
+ const l = asPlugins(local);
163
+ const r = asPlugins(remote);
164
+ if (l === undefined || r === undefined) return undefined;
165
+ const a = ancestor === undefined ? undefined : asPlugins(ancestor);
166
+ if (ancestor !== undefined && a === undefined) return undefined;
167
+ if (!sameShell(l, r)) return undefined;
168
+
169
+ const aMap = byName(a?.plugins);
170
+ const rMap = byName(r.plugins);
171
+ const merged: PluginEntry[] = [];
172
+ const conflicts: MergeConflict[] = [];
173
+ let mergePoints = 0;
174
+ const conflict = (path: string, reason: 'both-modified' | 'modify-delete' | 'delete-modify', lp: PluginEntry | undefined, rp: PluginEntry | undefined, ap: PluginEntry | undefined): void => {
175
+ conflicts.push({ path, kind: 'key', reason, autoMergeAttempted: false, local: lp, remote: rp, ancestor: ap });
176
+ };
177
+
178
+ for (const lp of l.plugins) {
179
+ const name = lp.name;
180
+ const rp = rMap.get(name);
181
+ const ap = aMap.get(name);
182
+ if (rp !== undefined) {
183
+ if (entryKey(lp) === entryKey(rp)) { merged.push(lp); continue; }
184
+ const lChanged = ap === undefined || entryKey(lp) !== entryKey(ap);
185
+ const rChanged = ap === undefined || entryKey(rp) !== entryKey(ap);
186
+ if (lChanged && rChanged) { conflict(`plugins.${name}`, 'both-modified', lp, rp, ap); continue; }
187
+ if (lChanged) { merged.push(lp); mergePoints += 1; continue; }
188
+ merged.push(rp);
189
+ mergePoints += 1;
190
+ continue;
191
+ }
192
+ // 只有本机有
193
+ if (ap === undefined) { merged.push(lp); mergePoints += 1; continue; } // 本机独有
194
+ if (entryKey(lp) === entryKey(ap)) { mergePoints += 1; continue; } // 本机未改、远端删 → 跟随删除
195
+ conflict(`plugins.${name}`, 'modify-delete', lp, undefined, ap);
196
+ }
197
+ // 只有远端有的
198
+ for (const rp of r.plugins) {
199
+ const name = rp.name;
200
+ if (l.plugins.some((p) => p.name === name)) continue;
201
+ const ap = aMap.get(name);
202
+ if (ap === undefined) { merged.push(rp); mergePoints += 1; continue; } // 远端独有
203
+ if (entryKey(rp) === entryKey(ap)) { mergePoints += 1; continue; } // 远端未改、本机删 → 跟随删除
204
+ conflict(`plugins.${name}`, 'delete-modify', undefined, rp, ap);
205
+ }
206
+ if (conflicts.length > 0) return { merged: l as SectionData, conflicts, mergePoints };
207
+ return { merged: { ...l, plugins: merged } as SectionData, conflicts: [], mergePoints };
208
+ }
@@ -782,12 +782,14 @@ function makeEngineWithMockImporter(opts: {
782
782
  stateDir: string;
783
783
  localSnapshotsDir?: string;
784
784
  mockImporter: MockImporter;
785
+ /** 追加引擎选项(issue #132:deletePlugins / selfPluginName 等) */
786
+ extra?: Partial<ConstructorParameters<typeof SyncEngine>[0]>;
785
787
  }) {
786
788
  // 把 mock 注入到 SyncEngine 的 importer 槽位 —— Importer 是类,类型上不能完全替换;
787
789
  // 这里用对象字面量 duck-type 兼容 Importer 的三个方法。
788
790
  return makeEngine({
789
791
  ...opts,
790
- extra: { importer: opts.mockImporter as unknown as Importer },
792
+ extra: { ...opts.extra, importer: opts.mockImporter as unknown as Importer },
791
793
  });
792
794
  }
793
795
 
@@ -2055,6 +2057,28 @@ test('P0-23 源码守卫:autosync-scheduler 以 snapshotBinding 调 applyMerge
2055
2057
  );
2056
2058
  });
2057
2059
 
2060
+ /**
2061
+ * issue #132 源码守卫:跟远端删插件只有**一条**执行通道 —— applyMergePlan。
2062
+ * 自动同步(autosync-scheduler Phase B)直接调 applyMergePlan,因此手动「合并并应用」与自动同步
2063
+ * 天然共享同一份删除判据(用户勾开关即授权,见 maintainer 裁决);一旦有人在 scheduler 里
2064
+ * 另开一条卸载路径,判据就会分叉成两套(开关关也删 / 不记 undo)。
2065
+ */
2066
+ test('issue #132 源码守卫:跟删只在 applyMergePlan 内执行,自动同步不得另开卸载通道', async () => {
2067
+ const scheduler = await fs.readFile(new URL('./autosync-scheduler.ts', import.meta.url), 'utf8');
2068
+ assert.ok(
2069
+ !scheduler.includes('uninstall') && !scheduler.includes('plugin-removals'),
2070
+ 'autosync-scheduler 不得自己判删/卸载:它只经 applyMergePlan 落盘',
2071
+ );
2072
+ const engine = await fs.readFile(new URL('./sync-engine.ts', import.meta.url), 'utf8');
2073
+ const stepIdx = engine.indexOf('await applyPluginRemovalStep({');
2074
+ const reexportIdx = engine.indexOf('exportAppliedSections(appliedIds)');
2075
+ assert.ok(stepIdx > 0, 'applyMergePlan 内应有 applyPluginRemovalStep 调用');
2076
+ assert.ok(
2077
+ reexportIdx > stepIdx,
2078
+ '删除必须发生在 exportAppliedSections 之前:否则基线记的还是刚删掉的包,每轮重复提删除',
2079
+ );
2080
+ });
2081
+
2058
2082
  // ─── P0-2:跨机基础路径重定基(快照携带 sourceHome → 拉取计划自动重定基) ──────────
2059
2083
 
2060
2084
 
@@ -3326,3 +3350,229 @@ test('issue #127: apply 阶段会话必须伴随工作区落地 —— 只应用
3326
3350
  await fs.rm(tmp, { recursive: true, force: true });
3327
3351
  }
3328
3352
  });
3353
+
3354
+
3355
+ /* ---------------- issue #132:跟随远端删除插件(开关 + 三方判据 + undo 线索) ---------------- */
3356
+
3357
+ /** 本轮被远端删掉的插件(seedSource 装的那一个) */
3358
+ const GONE_PLUGIN = '@deepseek-ai/dsh-ssh';
3359
+
3360
+ /** 远端快照:settings 变了,plugins 分区里**没有** GONE_PLUGIN(= 另一台机器卸载了它)。 */
3361
+ function mkRemoteWithoutPlugin(id: string, createdAt: string): SyncSnapshot {
3362
+ return {
3363
+ id,
3364
+ createdAt,
3365
+ manifest: { schemaVersion: 1, dshVersion: '1.2.3', platform: 'win32', sectionIds: ['settings', 'plugins'], containsSecrets: false },
3366
+ sections: {
3367
+ settings: {
3368
+ version: 1,
3369
+ namespaces: { general: { value: { theme: 'light', language: 'zh-CN' }, revision: 9, secrets: [] } },
3370
+ },
3371
+ plugins: { version: 1, plugins: [], patch: [] },
3372
+ },
3373
+ };
3374
+ }
3375
+
3376
+ /**
3377
+ * 摆好「本机推过基线(祖先副本里有 GONE_PLUGIN)→ 远端把它删了」的局面。
3378
+ * 返回的引擎尚未 merge —— 每个用例自己决定 merge / previewMerge / applyMergePlan。
3379
+ */
3380
+ async function arrangeRemoteDeletedPlugin(opts: {
3381
+ tmp: string;
3382
+ engineExtra?: Partial<ConstructorParameters<typeof SyncEngine>[0]>;
3383
+ /** 推基线之后再动本机(模拟「推完又在本机升级了它」) */
3384
+ afterBaseline?: (ctx: ReturnType<typeof makeContext>) => void;
3385
+ }) {
3386
+ const ctx = makeContext('win32', 'C:\\Users\\alice');
3387
+ seedSource(ctx);
3388
+ const transport = new MemSyncTransport();
3389
+ const mock = new MockImporter();
3390
+ mock.ok = true;
3391
+ const engine = makeEngineWithMockImporter({
3392
+ ctx,
3393
+ transport,
3394
+ stateDir: opts.tmp,
3395
+ localSnapshotsDir: path.join(opts.tmp, 'snapshots'),
3396
+ mockImporter: mock,
3397
+ ...(opts.engineExtra === undefined ? {} : { extra: opts.engineExtra }),
3398
+ });
3399
+ const baseline = await engine.push({ snapshotId: 'sync-132-base' });
3400
+ assert.equal(baseline.ok, true, '基线推送必须成功: ' + baseline.warnings.join(' | '));
3401
+ assert.ok(baseline.sections.includes('plugins'), 'plugins 是 portable 分区,必须进基线');
3402
+ const remote = mkRemoteWithoutPlugin('sync-132-remote', '2026-08-16T13:00:00.000Z');
3403
+ transport.snapshots.set(remote.id, remote);
3404
+ transport.metas.push(computeSnapshotMeta(remote));
3405
+ opts.afterBaseline?.(ctx);
3406
+ return { ctx, transport, engine, mock };
3407
+ }
3408
+
3409
+ /**
3410
+ * 把 plugins 分区塞进 autoApply:真实流程里 riskTier=medium 会先落到 review(确认页),
3411
+ * 用户确认后 applyMergeResolutions 复用同一条 applyMergePlan 路径 —— 删除判定就在那一步。
3412
+ * 这里直接构造「用户已确认」的 apply 计划,等价于确认后的调用形态。
3413
+ */
3414
+ function applyPlanIncludingPlugins(plan: Awaited<ReturnType<SyncEngine['merge']>>): SyncApplyPlan {
3415
+ const base = buildAutoApplyPlan(plan);
3416
+ const plugins = plan.sections.find((s) => s.id === 'plugins');
3417
+ return {
3418
+ autoApply: [...base.autoApply.filter((r) => r.id !== 'plugins'), ...(plugins === undefined ? [] : [plugins])],
3419
+ review: [],
3420
+ skipped: [],
3421
+ };
3422
+ }
3423
+
3424
+ test('issue #132: 开关开 + 祖先有远端无 → 卸载插件,报告给出重装目标', async () => {
3425
+ const tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-sync-132-remove-'));
3426
+ try {
3427
+ const { ctx, engine } = await arrangeRemoteDeletedPlugin({
3428
+ tmp,
3429
+ engineExtra: { deletePlugins: true },
3430
+ });
3431
+ const plan = await engine.merge();
3432
+ const plugins = plan.sections.find((s) => s.id === 'plugins');
3433
+ assert.ok(plugins, 'plugins 分区必须参与合并');
3434
+ assert.notEqual(plugins!.decision, 'conflict', '本机未改、远端删了 → 不是冲突: ' + JSON.stringify(plugins!.conflicts));
3435
+ const report = await engine.applyMergePlan(applyPlanIncludingPlugins(plan));
3436
+ assert.equal(report.ok, true, '整轮同步必须成功: ' + report.warnings.join(' | '));
3437
+ assert.equal(ctx.plugins.installed.has(GONE_PLUGIN), false, '必须跟随远端删除');
3438
+ assert.equal(ctx.plugins.lastUninstalled, GONE_PLUGIN, '只允许删那一个(不得误删本机新装的)');
3439
+ assert.deepEqual(report.pluginRemovals, [{
3440
+ name: GONE_PLUGIN,
3441
+ version: '1.0.0',
3442
+ reinstallTarget: GONE_PLUGIN + '@1.0.0',
3443
+ ok: true,
3444
+ }], '报告必须逐项列明删了什么、怎么装回');
3445
+ assert.ok(
3446
+ report.warnings.some((w) => w.includes('已跟随远端删除插件')),
3447
+ '用户必须能看到删了哪个: ' + report.warnings.join(' | '),
3448
+ );
3449
+ assert.equal(report.needsRestart, true, '删过插件必须要求重启,报告不能只说「已应用」');
3450
+ } finally {
3451
+ await fs.rm(tmp, { recursive: true, force: true });
3452
+ }
3453
+ });
3454
+
3455
+ test('issue #132: 开关关(缺省)→ 一个都不删,但如实说明「开启后会删」', async () => {
3456
+ const tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-sync-132-off-'));
3457
+ try {
3458
+ const { ctx, engine } = await arrangeRemoteDeletedPlugin({ tmp });
3459
+ const plan = await engine.merge();
3460
+ const report = await engine.applyMergePlan(applyPlanIncludingPlugins(plan));
3461
+ assert.equal(report.ok, true, report.warnings.join(' | '));
3462
+ assert.equal(ctx.plugins.installed.has(GONE_PLUGIN), true, '开关关 → 绝不卸载');
3463
+ assert.equal(ctx.plugins.lastUninstalled, undefined, '开关关 → 不得调用 uninstall');
3464
+ assert.equal(report.pluginRemovals, undefined, '没删任何东西 → 报告不列删除项');
3465
+ assert.ok(
3466
+ report.warnings.some((w) => w.includes('未开启')),
3467
+ '必须提示候选而不是静默保留: ' + report.warnings.join(' | '),
3468
+ );
3469
+ } finally {
3470
+ await fs.rm(tmp, { recursive: true, force: true });
3471
+ }
3472
+ });
3473
+
3474
+ test('issue #132: 卸载被拒(desktop 保留档案)→ 逐项 warning、整轮仍 ok、不阻断其余项', async () => {
3475
+ const tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-sync-132-fail-'));
3476
+ try {
3477
+ const { ctx, engine } = await arrangeRemoteDeletedPlugin({
3478
+ tmp,
3479
+ engineExtra: { deletePlugins: true },
3480
+ });
3481
+ ctx.plugins.failUninstall = true; // 模拟 CLI 拒绝:profile "desktop" is managed exclusively…
3482
+ const plan = await engine.merge();
3483
+ const report = await engine.applyMergePlan(applyPlanIncludingPlugins(plan));
3484
+ assert.equal(report.ok, true, '一次卸载失败不该让整轮回滚: ' + report.warnings.join(' | '));
3485
+ assert.equal(ctx.plugins.installed.has(GONE_PLUGIN), true, '卸载失败 = 本机还装着(不做本地假删)');
3486
+ assert.equal(report.pluginRemovals?.length, 1, '失败项也要进报告(逐项可见)');
3487
+ assert.equal(report.pluginRemovals![0]!.ok, false);
3488
+ assert.match(report.pluginRemovals![0]!.message ?? '', /desktop/, '原因要原样带出(用户据此判断是不是保留档案)');
3489
+ assert.ok(
3490
+ report.warnings.some((w) => w.includes('删除插件') && w.includes('失败')),
3491
+ '必须逐项告警: ' + report.warnings.join(' | '),
3492
+ );
3493
+ assert.ok(report.applied.includes('settings'), '其余分区照旧落地');
3494
+ } finally {
3495
+ await fs.rm(tmp, { recursive: true, force: true });
3496
+ }
3497
+ });
3498
+
3499
+ test('issue #132: 本机改过版本 + 远端删了 → 冲突、不删、不猜', async () => {
3500
+ const tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-sync-132-conflict-'));
3501
+ try {
3502
+ const { ctx, engine } = await arrangeRemoteDeletedPlugin({
3503
+ tmp,
3504
+ engineExtra: { deletePlugins: true },
3505
+ // 推完基线又在本机升级了它 → 与祖先不同 = 用户在动它,不许替用户卸载
3506
+ afterBaseline: (c) => {
3507
+ c.plugins.installed.set(GONE_PLUGIN, { name: GONE_PLUGIN, version: '1.1.0', enabled: true });
3508
+ },
3509
+ });
3510
+ const preview = await engine.previewMerge();
3511
+ assert.equal(preview.ok, true, 'preview 必须成功: ' + (preview.message ?? ''));
3512
+ assert.deepEqual(preview.pluginRemovalConflicts, [{
3513
+ name: GONE_PLUGIN,
3514
+ ancestorVersion: '1.0.0',
3515
+ localVersion: '1.1.0',
3516
+ }], '确认页必须说明「远端删了、本机改过」');
3517
+ const plan = await engine.merge();
3518
+ const plugins = plan.sections.find((s) => s.id === 'plugins');
3519
+ assert.equal(plugins!.decision, 'conflict', 'modify-delete 必须交人工裁决');
3520
+ const report = await engine.applyMergePlan(buildAutoApplyPlan(plan));
3521
+ assert.equal(ctx.plugins.installed.has(GONE_PLUGIN), true, '有冲突 → 一个都不删');
3522
+ assert.equal(ctx.plugins.lastUninstalled, undefined);
3523
+ assert.equal(report.pluginRemovals, undefined, '冲突项不进删除报告(在 review 里)');
3524
+ } finally {
3525
+ await fs.rm(tmp, { recursive: true, force: true });
3526
+ }
3527
+ });
3528
+
3529
+ test('issue #132: 无祖先(首次同步)→ 不删任何插件(无法区分「对方删了」与「本机独有」)', async () => {
3530
+ const tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-sync-132-noanc-'));
3531
+ try {
3532
+ const ctx = makeContext('win32', 'C:\\Users\\alice');
3533
+ seedSource(ctx);
3534
+ const transport = new MemSyncTransport();
3535
+ const mock = new MockImporter();
3536
+ mock.ok = true;
3537
+ // 从未 push → 没有祖先副本
3538
+ const remote = mkRemoteWithoutPlugin('sync-132-first', '2026-08-16T13:00:00.000Z');
3539
+ transport.snapshots.set(remote.id, remote);
3540
+ transport.metas.push(computeSnapshotMeta(remote));
3541
+ const engine = makeEngineWithMockImporter({
3542
+ ctx,
3543
+ transport,
3544
+ stateDir: tmp,
3545
+ localSnapshotsDir: path.join(tmp, 'snapshots'),
3546
+ mockImporter: mock,
3547
+ extra: { deletePlugins: true },
3548
+ });
3549
+ const preview = await engine.previewMerge();
3550
+ assert.equal(preview.ok, true);
3551
+ assert.deepEqual(preview.pluginRemovals, [], '没有祖先 → 不给删除清单(空清单,不是「未知」)');
3552
+ const plan = await engine.merge();
3553
+ const report = await engine.applyMergePlan(applyPlanIncludingPlugins(plan));
3554
+ assert.equal(ctx.plugins.installed.has(GONE_PLUGIN), true, '无祖先不得删除');
3555
+ assert.equal(ctx.plugins.lastUninstalled, undefined);
3556
+ assert.equal(report.pluginRemovals, undefined);
3557
+ } finally {
3558
+ await fs.rm(tmp, { recursive: true, force: true });
3559
+ }
3560
+ });
3561
+
3562
+ test('issue #132: previewMerge 单列「将删除」清单(且预览绝不真的删)', async () => {
3563
+ const tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-sync-132-preview-'));
3564
+ try {
3565
+ const { ctx, engine } = await arrangeRemoteDeletedPlugin({
3566
+ tmp,
3567
+ engineExtra: { deletePlugins: true },
3568
+ });
3569
+ const preview = await engine.previewMerge();
3570
+ assert.equal(preview.ok, true);
3571
+ assert.deepEqual(preview.pluginRemovals, [{ name: GONE_PLUGIN, version: '1.0.0' }], '确认页必须能列出将删除项');
3572
+ assert.deepEqual(preview.pluginRemovalConflicts, [], '本机没改过 → 无冲突');
3573
+ assert.equal(ctx.plugins.installed.has(GONE_PLUGIN), true, '预览是只读的,绝不卸载');
3574
+ assert.equal(ctx.plugins.lastUninstalled, undefined);
3575
+ } finally {
3576
+ await fs.rm(tmp, { recursive: true, force: true });
3577
+ }
3578
+ });