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.
- package/lib/adapters/test-helpers.d.ts +7 -0
- package/lib/adapters/test-helpers.js +11 -0
- package/lib/cli/actions.js +1 -1
- package/lib/client.d.ts +9 -2
- package/lib/client.js +685 -649
- package/lib/core/backup.d.ts +9 -1
- package/lib/core/backup.js +21 -0
- package/lib/core/messages-core.d.ts +3 -0
- package/lib/core/messages-core.js +6 -0
- package/lib/core/messages.d.ts +5 -0
- package/lib/core/messages.js +10 -0
- package/lib/core/model-tools.js +2 -0
- package/lib/core/phase4-crash-child.js +1 -1
- package/lib/core/plugin-cli.d.ts +11 -0
- package/lib/core/plugin-cli.js +30 -0
- package/lib/core/restore-wal.d.ts +2 -0
- package/lib/core/restore-wal.js +11 -6
- package/lib/core/restore.d.ts +15 -2
- package/lib/core/restore.js +65 -3
- package/lib/core/snapshot-diff.js +2 -1
- package/lib/core/types.d.ts +32 -0
- package/lib/index.d.ts +6 -1
- package/lib/index.js +17 -16
- package/lib/routes/route-support.d.ts +5 -0
- package/lib/routes/route-support.js +28 -2
- package/lib/routes/sync.js +3 -0
- package/lib/sync/merge-preview-view.d.ts +78 -0
- package/lib/sync/merge-preview-view.js +46 -0
- package/lib/sync/merge.d.ts +2 -1
- package/lib/sync/merge.js +9 -4
- package/lib/sync/plugin-removal-apply.d.ts +95 -0
- package/lib/sync/plugin-removal-apply.js +158 -0
- package/lib/sync/plugin-removals.d.ts +90 -0
- package/lib/sync/plugin-removals.js +167 -0
- package/lib/sync/sync-engine.d.ts +34 -43
- package/lib/sync/sync-engine.js +50 -42
- package/lib/sync/sync-selection.d.ts +8 -0
- package/lib/sync/sync-selection.js +6 -0
- package/lib/sync/workspace-merge.d.ts +8 -1
- package/lib/ui/restore-plan-view.js +1 -0
- package/lib/ui/sync-settings-view.d.ts +10 -0
- package/lib/ui/sync-settings-view.js +2 -0
- package/package.json +1 -1
- package/src/adapters/test-helpers.ts +10 -0
- package/src/cli/actions.ts +1 -1
- package/src/client/common/info-hint-guard.test.ts +2 -1
- package/src/client/run-store.test.ts +7 -5
- package/src/client/run-store.ts +4 -5
- package/src/client/sync/SyncPanel.tsx +10 -0
- package/src/client/sync/SyncSectionPickerDialog.tsx +17 -1
- package/src/client/sync/sync-locales.ts +4 -0
- package/src/client/sync/sync-view.ts +6 -0
- package/src/core/backup.ts +21 -1
- package/src/core/messages-core.ts +6 -0
- package/src/core/messages.ts +10 -0
- package/src/core/model-tools.ts +2 -0
- package/src/core/phase4-crash-child.ts +1 -1
- package/src/core/plugin-cli.ts +37 -0
- package/src/core/restore-plugin-install.test.ts +116 -0
- package/src/core/restore-wal.ts +12 -6
- package/src/core/restore.ts +79 -4
- package/src/core/smoke.test.ts +5 -0
- package/src/core/snapshot-diff.test.ts +1 -1
- package/src/core/snapshot-diff.ts +2 -1
- package/src/core/types.ts +31 -0
- package/src/index.ts +21 -21
- package/src/routes/route-support.ts +27 -2
- package/src/routes/sync.ts +3 -0
- package/src/security/security.test.ts +1 -0
- package/src/sync/merge-preview-view.ts +97 -0
- package/src/sync/merge.test.ts +17 -15
- package/src/sync/merge.ts +10 -3
- package/src/sync/plugin-removal-apply.ts +198 -0
- package/src/sync/plugin-removals.test.ts +221 -0
- package/src/sync/plugin-removals.ts +208 -0
- package/src/sync/sync-engine.test.ts +251 -1
- package/src/sync/sync-engine.ts +84 -83
- package/src/sync/sync-selection.test.ts +29 -7
- package/src/sync/sync-selection.ts +13 -0
- package/src/sync/workspace-merge.ts +9 -0
- package/src/ui/restore-plan-view.test.ts +1 -1
- package/src/ui/restore-plan-view.ts +1 -0
- package/src/ui/snapshots-view.test.ts +1 -1
- package/src/ui/sync-settings-view.test.ts +20 -0
- package/src/ui/sync-settings-view.ts +12 -0
package/src/sync/merge.ts
CHANGED
|
@@ -32,8 +32,9 @@
|
|
|
32
32
|
import type { FilesSection, SectionData, SectionId } from '../schema/types.ts';
|
|
33
33
|
import { hashSection } from './sync-state.ts';
|
|
34
34
|
import { mergeBytesThreeWay, mergeJsonValues, type TextMergeConflict } from './text-merge.ts';
|
|
35
|
+
import { mergePluginsSection } from './plugin-removals.ts';
|
|
35
36
|
import { mergeWorkspacesSection } from './workspace-merge.ts';
|
|
36
|
-
import type { GranularMerger } from './workspace-merge.ts';
|
|
37
|
+
import type { GranularMergeHooks, GranularMerger } from './workspace-merge.ts';
|
|
37
38
|
|
|
38
39
|
/**
|
|
39
40
|
* 分区级决胜结果(三方合并,2026-10 收口):
|
|
@@ -298,6 +299,10 @@ function mergeFilesSectionGranular(
|
|
|
298
299
|
*/
|
|
299
300
|
const GRANULAR_MERGES: Partial<Record<SectionId, GranularMerger>> = {
|
|
300
301
|
workspaces: mergeWorkspacesSection,
|
|
302
|
+
// plugins(issue #132):plugins 是**数组**,通用 JSON 合并对「两侧各自改过」一律判冲突 ——
|
|
303
|
+
// 本机装了 Y、远端删了 X 就会整分区冲突,X 永远删不掉、Y 也永远同步不出去。
|
|
304
|
+
// 钩子按包名做三方逐条判定(本地新增保留 / 远端删除跟随 / 版本冲突交人工)。
|
|
305
|
+
plugins: mergePluginsSection,
|
|
301
306
|
};
|
|
302
307
|
|
|
303
308
|
/** 单分区三方合并(决策链见文件头注释)。 */
|
|
@@ -307,6 +312,7 @@ function mergeSectionTriple(
|
|
|
307
312
|
rData: SectionData | undefined,
|
|
308
313
|
aData: SectionData | undefined,
|
|
309
314
|
h: TripleHashes,
|
|
315
|
+
hooks: GranularMergeHooks,
|
|
310
316
|
): MergeSectionResult {
|
|
311
317
|
// ⓪ 形状闸门(先于一切取舍):远端载荷来自不可信输入(解析层只保证整体是合法 JSON,
|
|
312
318
|
// 不保证每个分区的值形状正确)。值形状不对时**绝不原样采用** —— 那等于把畸形数据
|
|
@@ -366,7 +372,7 @@ function mergeSectionTriple(
|
|
|
366
372
|
// 没有祖先时也应并集(否则首轮同步就整分区冲突),字段取值分歧由钩子逐键如实报冲突。
|
|
367
373
|
const granular = GRANULAR_MERGES[id];
|
|
368
374
|
if (granular !== undefined) {
|
|
369
|
-
const res = granular(local, remote, aData);
|
|
375
|
+
const res = granular(local, remote, aData, hooks);
|
|
370
376
|
if (res !== undefined) {
|
|
371
377
|
if (res.conflicts.length > 0) return { id, decision: 'conflict', conflicts: res.conflicts };
|
|
372
378
|
const merged = res.merged;
|
|
@@ -433,6 +439,7 @@ export function merge(
|
|
|
433
439
|
local: Partial<Record<SectionId, SectionData>>,
|
|
434
440
|
remote: Partial<Record<SectionId, SectionData>>,
|
|
435
441
|
ancestor?: Partial<Record<SectionId, SectionData>>,
|
|
442
|
+
hooks: GranularMergeHooks = {},
|
|
436
443
|
): MergePlan {
|
|
437
444
|
const l = local as Record<string, SectionData>;
|
|
438
445
|
const r = remote as Record<string, SectionData>;
|
|
@@ -441,7 +448,7 @@ export function merge(
|
|
|
441
448
|
const hashes = computeHashes(l, r, a, ids);
|
|
442
449
|
const results: MergeSectionResult[] = [];
|
|
443
450
|
for (const id of ids) {
|
|
444
|
-
results.push(mergeSectionTriple(id, l[id], r[id], a ? a[id] : undefined, hashes.get(id)
|
|
451
|
+
results.push(mergeSectionTriple(id, l[id], r[id], a ? a[id] : undefined, hashes.get(id)!, hooks));
|
|
445
452
|
}
|
|
446
453
|
return { sections: results };
|
|
447
454
|
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* issue #132:跟随远端删除插件的**编排层**(判据 → 卸载 → 告警 → undo 线索)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么从 sync-engine 下沉:引擎已顶到文件体量棘轮(tests/file-size-ratchet.test.ts),
|
|
5
|
+
* 而这部分只依赖「三方载荷 + 本机已装清单 + 卸载函数 + msg」,不碰引擎内部状态;
|
|
6
|
+
* 独立成模块后可单独测,也不会把引擎继续堆高。判据本体仍留在 ./plugin-removals.ts(纯函数、零 IO)。
|
|
7
|
+
*
|
|
8
|
+
* 三条语义在此定型,改之前先读 issue #132 的裁决:
|
|
9
|
+
* 1. 开关关闭 = **不删但也绝不装作没有候选**:逐项发 warning 说明「开着会删谁」;
|
|
10
|
+
* 2. 卸载失败(含 desktop 保留档案被 CLI 拒)逐项进报告 + warning,不阻断其余项、不回滚整轮;
|
|
11
|
+
* 3. undo 线索只写「真的删成功」的那些,写不进去只告警(报告里的 reinstallTarget 仍是人工兜底)。
|
|
12
|
+
*/
|
|
13
|
+
import { registerMessageCatalog } from '../core/engine-locale.ts';
|
|
14
|
+
import { pluginInstallTarget } from '../core/plugin-cli.ts';
|
|
15
|
+
import type { MsgFunc } from '../core/messages.ts';
|
|
16
|
+
import type { SnapshotPluginRemoval } from '../core/types.ts';
|
|
17
|
+
import type { SectionData, SectionId } from '../schema/types.ts';
|
|
18
|
+
import { planPluginRemovals } from './plugin-removals.ts';
|
|
19
|
+
import type { PluginRemovalEntry, PluginRemovalPlan } from './plugin-removals.ts';
|
|
20
|
+
|
|
21
|
+
const zh = {
|
|
22
|
+
'sync.pluginRemoveDone': '已跟随远端删除插件 {name}@{version}(可随时用 "{name}@{version}" 重装)',
|
|
23
|
+
'sync.pluginRemoveFailed': '删除插件 {name} 失败:{reason}(其余项继续,失败项下一轮会重新判定)',
|
|
24
|
+
'sync.pluginRemoveConflict': '插件 {name} 远端已删除,但本机改过(本机 {local},共同祖先 {ancestor}):未删除,请人工确认',
|
|
25
|
+
'sync.pluginRemoveSkipped': '插件 {name} 在远端已删除,但"跟随删除插件"未开启:已保留(开启后会删除)',
|
|
26
|
+
'sync.pluginRemoveUndoUnrecorded': '插件已经删除,但没能写进本次回滚快照的 undo 记录({names}):恢复这份快照不会自动装回它们,需要时请手动执行 dsh plugin add',
|
|
27
|
+
} as const;
|
|
28
|
+
|
|
29
|
+
const en: Record<keyof typeof zh, string> = {
|
|
30
|
+
'sync.pluginRemoveDone': 'Removed plugin {name}@{version} to follow the remote (reinstall anytime with "{name}@{version}")',
|
|
31
|
+
'sync.pluginRemoveFailed': 'Could not remove plugin {name}: {reason} (the remaining items continue; this one is re-evaluated next round)',
|
|
32
|
+
'sync.pluginRemoveConflict': 'Plugin {name} was deleted remotely but changed locally (local {local}, common ancestor {ancestor}): not removed, needs a human decision',
|
|
33
|
+
'sync.pluginRemoveSkipped': 'Plugin {name} was deleted remotely, but "delete plugins to follow the remote" is off: kept (it will be removed once enabled)',
|
|
34
|
+
'sync.pluginRemoveUndoUnrecorded': 'The plugin(s) were removed, but the undo record could not be written into this rollback snapshot ({names}): restoring that snapshot will not reinstall them — run dsh plugin add manually if needed',
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
registerMessageCatalog({ id: 'sync-plugin-removal', zh, en });
|
|
38
|
+
|
|
39
|
+
/** 一条插件卸载结果(reinstallTarget 供 undo 与人工补装使用)。 */
|
|
40
|
+
export interface PluginRemovalReportItem {
|
|
41
|
+
name: string;
|
|
42
|
+
version: string;
|
|
43
|
+
/** 重装目标:`name@version`(版本未知时退回裸包名) */
|
|
44
|
+
reinstallTarget: string;
|
|
45
|
+
ok: boolean;
|
|
46
|
+
/** 失败原因(已由 CLI 分类器给出可操作文案);ok=true 时缺省 */
|
|
47
|
+
message?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 预览侧的删除视图(确认页据此单列「将删除:X@1.2.3」,用户可整轮取消)。 */
|
|
51
|
+
export interface PluginRemovalPreviewView {
|
|
52
|
+
pluginRemovals: { name: string; version: string }[];
|
|
53
|
+
pluginRemovalConflicts: { name: string; ancestorVersion: string; localVersion: string }[];
|
|
54
|
+
pluginRemovalSkipped: { name: string; reason: string }[];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 判据输入侧的「已装插件」最小形状(PluginInfo 与 PluginEntry 都能直接传)。 */
|
|
58
|
+
type InstalledPlugin = { name: string; version: string; enabled?: boolean; spec?: string; bundlesOnly?: boolean };
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 把某个分区载荷里的插件清单抽成判据要的形状(不认识 = undefined)。
|
|
62
|
+
*
|
|
63
|
+
* 为什么直接从「本轮合并的三方输入」读而不是再 export 一次:那三个载荷就是本轮合并的真实输入,
|
|
64
|
+
* 再 export 会拿到「已经写了一半」的状态,判据与合并结论就会打架。
|
|
65
|
+
*/
|
|
66
|
+
export function pluginEntriesOf(data: SectionData | undefined): PluginRemovalEntry[] | undefined {
|
|
67
|
+
if (data === null || typeof data !== 'object') return undefined;
|
|
68
|
+
const obj = data as { version?: unknown; plugins?: unknown };
|
|
69
|
+
if (obj.version !== 1 || !Array.isArray(obj.plugins)) return undefined;
|
|
70
|
+
const out: PluginRemovalEntry[] = [];
|
|
71
|
+
for (const raw of obj.plugins) {
|
|
72
|
+
if (raw === null || typeof raw !== 'object') continue;
|
|
73
|
+
const p = raw as { name?: unknown; version?: unknown; spec?: unknown; bundlesOnly?: unknown };
|
|
74
|
+
if (typeof p.name !== 'string' || p.name === '') continue;
|
|
75
|
+
out.push({
|
|
76
|
+
name: p.name,
|
|
77
|
+
version: typeof p.version === 'string' ? p.version : '',
|
|
78
|
+
...(typeof p.spec === 'string' ? { spec: p.spec } : {}),
|
|
79
|
+
...(p.bundlesOnly === true ? { bundlesOnly: true } : {}),
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** 三方输入 + 本机已装 → 删除计划(预览与执行共用同一份判据)。 */
|
|
86
|
+
export function pluginRemovalPlanOf(args: {
|
|
87
|
+
ancestor?: SectionData;
|
|
88
|
+
remote?: SectionData;
|
|
89
|
+
installed: readonly InstalledPlugin[];
|
|
90
|
+
enabled: boolean;
|
|
91
|
+
selfName?: string;
|
|
92
|
+
}): PluginRemovalPlan {
|
|
93
|
+
return planPluginRemovals({
|
|
94
|
+
enabled: args.enabled,
|
|
95
|
+
ancestor: pluginEntriesOf(args.ancestor),
|
|
96
|
+
remote: pluginEntriesOf(args.remote),
|
|
97
|
+
local: args.installed,
|
|
98
|
+
selfName: args.selfName ?? '',
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 执行删除(逐项失败不阻断)。
|
|
104
|
+
*
|
|
105
|
+
* 为什么只在 plugins 分区真的被应用时才动:分区冲突/跳过 = 本轮还没确定「远端删了它」,
|
|
106
|
+
* 此时删本机插件等于拿未裁决的远端差异动用户机器。
|
|
107
|
+
*/
|
|
108
|
+
export async function applyPluginRemovalStep(args: {
|
|
109
|
+
appliedIds: readonly SectionId[];
|
|
110
|
+
plan: PluginRemovalPlan | undefined;
|
|
111
|
+
enabled: boolean;
|
|
112
|
+
uninstall: (name: string) => Promise<unknown>;
|
|
113
|
+
msg: MsgFunc;
|
|
114
|
+
}): Promise<{ removals: PluginRemovalReportItem[]; warnings: string[] }> {
|
|
115
|
+
const removals: PluginRemovalReportItem[] = [];
|
|
116
|
+
const warnings: string[] = [];
|
|
117
|
+
if (!args.appliedIds.includes('plugins')) return { removals, warnings };
|
|
118
|
+
const plan = args.plan;
|
|
119
|
+
if (plan === undefined) return { removals, warnings };
|
|
120
|
+
for (const c of plan.conflicts) {
|
|
121
|
+
warnings.push(args.msg('sync.pluginRemoveConflict', {
|
|
122
|
+
name: c.name, local: c.localVersion, ancestor: c.ancestorVersion,
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
if (!args.enabled) {
|
|
126
|
+
// 开关关:把「打开后会删什么」如实说出来(不删,但也绝不装作没有候选)
|
|
127
|
+
for (const k of plan.kept) {
|
|
128
|
+
if (k.reason === 'switch-off') warnings.push(args.msg('sync.pluginRemoveSkipped', { name: k.name }));
|
|
129
|
+
}
|
|
130
|
+
return { removals, warnings };
|
|
131
|
+
}
|
|
132
|
+
for (const r of plan.removals) {
|
|
133
|
+
// 重装目标与导入侧同一决策(core/plugin-cli.ts 的 pluginInstallTarget):github:/git+/file:/link:
|
|
134
|
+
// 这类来源只靠 name@version 装不回同一份代码,必须带祖先记录的 spec。
|
|
135
|
+
const reinstallTarget = pluginInstallTarget(r.name, {
|
|
136
|
+
...(r.version === '' ? {} : { version: r.version }),
|
|
137
|
+
...(r.spec === undefined ? {} : { spec: r.spec }),
|
|
138
|
+
});
|
|
139
|
+
try {
|
|
140
|
+
await args.uninstall(r.name);
|
|
141
|
+
removals.push({ name: r.name, version: r.version, reinstallTarget, ok: true });
|
|
142
|
+
warnings.push(args.msg('sync.pluginRemoveDone', { name: r.name, version: r.version }));
|
|
143
|
+
} catch (error) {
|
|
144
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
145
|
+
removals.push({ name: r.name, version: r.version, reinstallTarget, ok: false, message: reason });
|
|
146
|
+
warnings.push(args.msg('sync.pluginRemoveFailed', { name: r.name, reason }));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return { removals, warnings };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 把「本轮删掉哪些插件」写进本次回滚快照(undo 线索)。
|
|
154
|
+
*
|
|
155
|
+
* 写不进去不中止同步(分区已落地且已重导出),但必须留一条可见告警 ——
|
|
156
|
+
* 报告里的 reinstallTarget 仍可人工执行,静默丢失 undo 线索才是真问题。
|
|
157
|
+
*/
|
|
158
|
+
export async function recordPluginRemovalUndo(args: {
|
|
159
|
+
removals: readonly PluginRemovalReportItem[];
|
|
160
|
+
snapshotId: string;
|
|
161
|
+
record?: (id: string, entries: SnapshotPluginRemoval[]) => Promise<void>;
|
|
162
|
+
msg: MsgFunc;
|
|
163
|
+
}): Promise<string[]> {
|
|
164
|
+
const removed = args.removals.filter((r) => r.ok);
|
|
165
|
+
if (removed.length === 0) return [];
|
|
166
|
+
const names = removed.map((r) => r.reinstallTarget).join(', ');
|
|
167
|
+
const entries = removed.map((r) => ({ name: r.name, version: r.version, target: r.reinstallTarget }));
|
|
168
|
+
if (args.record === undefined) return [args.msg('sync.pluginRemoveUndoUnrecorded', { names })];
|
|
169
|
+
try {
|
|
170
|
+
await args.record(args.snapshotId, entries);
|
|
171
|
+
return [];
|
|
172
|
+
} catch (error) {
|
|
173
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
174
|
+
return [args.msg('sync.pluginRemoveUndoUnrecorded', { names }), reason];
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* 预览侧的删除视图:确认页必须能单列「将删除:X@1.2.3」让用户整轮取消,
|
|
180
|
+
* 也必须如实说明「开关关着,所以没删」与「远端删了但本机改过」两种不删的原因。
|
|
181
|
+
*
|
|
182
|
+
* plugins 分区本轮不可应用(conflict / skip)时不列删除项:那时「远端删了它」还没被用户确认。
|
|
183
|
+
*/
|
|
184
|
+
export function pluginRemovalPreviewOf(args: {
|
|
185
|
+
plan: PluginRemovalPlan | undefined;
|
|
186
|
+
sections: readonly { id: SectionId; decision: string }[];
|
|
187
|
+
}): PluginRemovalPreviewView | undefined {
|
|
188
|
+
if (args.plan === undefined) return undefined;
|
|
189
|
+
const section = args.sections.find((s) => s.id === 'plugins');
|
|
190
|
+
const applied = section !== undefined && section.decision !== 'conflict' && section.decision !== 'skip';
|
|
191
|
+
return {
|
|
192
|
+
pluginRemovals: applied ? args.plan.removals.map((r) => ({ name: r.name, version: r.version })) : [],
|
|
193
|
+
pluginRemovalConflicts: args.plan.conflicts.map((c) => ({
|
|
194
|
+
name: c.name, ancestorVersion: c.ancestorVersion, localVersion: c.localVersion,
|
|
195
|
+
})),
|
|
196
|
+
pluginRemovalSkipped: args.plan.kept.map((k) => ({ name: k.name, reason: k.reason })),
|
|
197
|
+
};
|
|
198
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #132 跟随远端删除插件:两个纯函数的行为规格。
|
|
3
|
+
*
|
|
4
|
+
* 接缝(seam):
|
|
5
|
+
* 1. planPluginRemovals —— 「哪些已装插件该被删」的唯一判据(开关、祖先、远端、本机三方);
|
|
6
|
+
* 2. mergePluginsSection —— plugins 分区的按名三方合并(否则通用 JSON 合并对数组两侧各改即冲突,
|
|
7
|
+
* 会把「远端删 X + 本机新装 Y」误报成整分区冲突,让删除永远进不了自动应用)。
|
|
8
|
+
*
|
|
9
|
+
* 两个函数都是纯函数、零 I/O:引擎与 UI 都只读它们的返回值。
|
|
10
|
+
*/
|
|
11
|
+
import { test } from 'node:test';
|
|
12
|
+
import assert from 'node:assert/strict';
|
|
13
|
+
|
|
14
|
+
import { mergePluginsSection, planPluginRemovals } from './plugin-removals.ts';
|
|
15
|
+
import type { PluginEntry, PluginsSection } from '../schema/types.ts';
|
|
16
|
+
|
|
17
|
+
function entry(name: string, version: string, extra: Partial<PluginEntry> = {}): PluginEntry {
|
|
18
|
+
return { name, version, isBundle: false, inBundles: [], enabled: true, ...extra };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const SELF = 'dsh-config-manager';
|
|
22
|
+
|
|
23
|
+
/* ------------------------------------------------------------------ 计划:删除判定 */
|
|
24
|
+
|
|
25
|
+
test('planPluginRemovals: 开关关闭 → 一个都不删,候选降级为 switch-off', () => {
|
|
26
|
+
const plan = planPluginRemovals({
|
|
27
|
+
enabled: false,
|
|
28
|
+
ancestor: [entry('alpha', '1.2.0')],
|
|
29
|
+
remote: [],
|
|
30
|
+
local: [entry('alpha', '1.2.0')],
|
|
31
|
+
selfName: SELF,
|
|
32
|
+
});
|
|
33
|
+
assert.deepEqual(plan.removals, []);
|
|
34
|
+
assert.deepEqual(plan.kept, [{ name: 'alpha', reason: 'switch-off' }]);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('planPluginRemovals: 祖先有、远端没有、本机未改 → 删(issue 行为表 A→B)', () => {
|
|
38
|
+
const plan = planPluginRemovals({
|
|
39
|
+
enabled: true,
|
|
40
|
+
ancestor: [entry('alpha', '1.2.0'), entry('beta', '0.4.0')],
|
|
41
|
+
remote: [entry('beta', '0.4.0')],
|
|
42
|
+
local: [entry('alpha', '1.2.0'), entry('beta', '0.4.0')],
|
|
43
|
+
selfName: SELF,
|
|
44
|
+
});
|
|
45
|
+
assert.deepEqual(plan.removals, [{ name: 'alpha', version: '1.2.0' }]);
|
|
46
|
+
assert.deepEqual(plan.conflicts, []);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('planPluginRemovals: 本机新装的插件(祖先没有)绝不删', () => {
|
|
50
|
+
const plan = planPluginRemovals({
|
|
51
|
+
enabled: true,
|
|
52
|
+
ancestor: [entry('alpha', '1.2.0')],
|
|
53
|
+
remote: [],
|
|
54
|
+
local: [entry('alpha', '1.2.0'), entry('mine', '9.9.9')],
|
|
55
|
+
selfName: SELF,
|
|
56
|
+
});
|
|
57
|
+
assert.deepEqual(plan.removals, [{ name: 'alpha', version: '1.2.0' }]);
|
|
58
|
+
assert.equal(plan.removals.some((r) => r.name === 'mine'), false);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('planPluginRemovals: 本机版本已变(本地升级)→ 报冲突、不删(issue 行为表 B 升级 X)', () => {
|
|
62
|
+
const plan = planPluginRemovals({
|
|
63
|
+
enabled: true,
|
|
64
|
+
ancestor: [entry('alpha', '1.2.0')],
|
|
65
|
+
remote: [],
|
|
66
|
+
local: [entry('alpha', '1.3.0')],
|
|
67
|
+
selfName: SELF,
|
|
68
|
+
});
|
|
69
|
+
assert.deepEqual(plan.removals, []);
|
|
70
|
+
assert.deepEqual(plan.conflicts, [{ name: 'alpha', reason: 'version-changed', ancestorVersion: '1.2.0', localVersion: '1.3.0' }]);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('planPluginRemovals: bundlesOnly 条目永不删(它由 DSH 自身安装位置解析,不是本 profile 的依赖)', () => {
|
|
74
|
+
const plan = planPluginRemovals({
|
|
75
|
+
enabled: true,
|
|
76
|
+
ancestor: [entry('host-layer', '', { bundlesOnly: true })],
|
|
77
|
+
remote: [],
|
|
78
|
+
local: [entry('host-layer', '', { bundlesOnly: true })],
|
|
79
|
+
selfName: SELF,
|
|
80
|
+
});
|
|
81
|
+
assert.deepEqual(plan.removals, []);
|
|
82
|
+
assert.deepEqual(plan.kept, [{ name: 'host-layer', reason: 'bundles-only' }]);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('planPluginRemovals: 本插件自己永不删(删了等于把正在跑的宿主插件卸掉)', () => {
|
|
86
|
+
const plan = planPluginRemovals({
|
|
87
|
+
enabled: true,
|
|
88
|
+
ancestor: [entry(SELF, '0.2.0')],
|
|
89
|
+
remote: [],
|
|
90
|
+
local: [entry(SELF, '0.2.0')],
|
|
91
|
+
selfName: SELF,
|
|
92
|
+
});
|
|
93
|
+
assert.deepEqual(plan.removals, []);
|
|
94
|
+
assert.deepEqual(plan.kept, [{ name: SELF, reason: 'self' }]);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('planPluginRemovals: 远端仍装着 → 不删', () => {
|
|
98
|
+
const plan = planPluginRemovals({
|
|
99
|
+
enabled: true,
|
|
100
|
+
ancestor: [entry('alpha', '1.2.0')],
|
|
101
|
+
remote: [entry('alpha', '1.2.0')],
|
|
102
|
+
local: [entry('alpha', '1.2.0')],
|
|
103
|
+
selfName: SELF,
|
|
104
|
+
});
|
|
105
|
+
assert.deepEqual(plan.removals, []);
|
|
106
|
+
assert.deepEqual(plan.kept, [{ name: 'alpha', reason: 'still-in-remote' }]);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('planPluginRemovals: 本机根本没装 → 无需卸载(不产生动作,也不报错)', () => {
|
|
110
|
+
const plan = planPluginRemovals({
|
|
111
|
+
enabled: true,
|
|
112
|
+
ancestor: [entry('alpha', '1.2.0')],
|
|
113
|
+
remote: [],
|
|
114
|
+
local: [],
|
|
115
|
+
selfName: SELF,
|
|
116
|
+
});
|
|
117
|
+
assert.deepEqual(plan.removals, []);
|
|
118
|
+
assert.deepEqual(plan.kept, [{ name: 'alpha', reason: 'not-installed' }]);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('planPluginRemovals: 没有祖先(首次同步)→ 一律不删(无法区分「对方删了」与「本机独有」)', () => {
|
|
122
|
+
const plan = planPluginRemovals({
|
|
123
|
+
enabled: true,
|
|
124
|
+
ancestor: undefined,
|
|
125
|
+
remote: [],
|
|
126
|
+
local: [entry('alpha', '1.2.0')],
|
|
127
|
+
selfName: SELF,
|
|
128
|
+
});
|
|
129
|
+
assert.deepEqual(plan.removals, []);
|
|
130
|
+
assert.deepEqual(plan.kept, []);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
/* ------------------------------------------------------------------ 合并:按名三方 */
|
|
134
|
+
|
|
135
|
+
/** 钩子只在开关打开时参与;单测里显式开启(关闭路径另有用例)。 */
|
|
136
|
+
const ON = { deletePlugins: true };
|
|
137
|
+
|
|
138
|
+
function section(plugins: PluginEntry[], extra: Partial<PluginsSection> = {}): PluginsSection {
|
|
139
|
+
return { version: 1, plugins, patch: [], ...extra };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
test('mergePluginsSection: 远端删 X、本机新装 Y → 合并结果只留 Y,不报冲突', () => {
|
|
143
|
+
const res = mergePluginsSection(
|
|
144
|
+
section([entry('alpha', '1.2.0'), entry('y', '2.0.0')]),
|
|
145
|
+
section([]),
|
|
146
|
+
section([entry('alpha', '1.2.0')]),
|
|
147
|
+
ON,
|
|
148
|
+
);
|
|
149
|
+
assert.ok(res !== undefined);
|
|
150
|
+
assert.deepEqual(res.conflicts, []);
|
|
151
|
+
assert.deepEqual((res.merged as PluginsSection).plugins.map((p) => p.name), ['y']);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test('mergePluginsSection: 本机升级 X、远端删 X → modify-delete 冲突(不静默删也不静默留)', () => {
|
|
155
|
+
const res = mergePluginsSection(
|
|
156
|
+
section([entry('alpha', '1.3.0')]),
|
|
157
|
+
section([]),
|
|
158
|
+
section([entry('alpha', '1.2.0')]),
|
|
159
|
+
ON,
|
|
160
|
+
);
|
|
161
|
+
assert.ok(res !== undefined);
|
|
162
|
+
assert.equal(res.conflicts.length, 1);
|
|
163
|
+
assert.equal(res.conflicts[0]!.path, 'plugins.alpha');
|
|
164
|
+
assert.equal(res.conflicts[0]!.reason, 'modify-delete');
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test('mergePluginsSection: 两侧同版本 → 保留,两侧同删 → 消失', () => {
|
|
168
|
+
const same = mergePluginsSection(
|
|
169
|
+
section([entry('alpha', '1.2.0')]),
|
|
170
|
+
section([entry('alpha', '1.2.0')]),
|
|
171
|
+
undefined,
|
|
172
|
+
ON,
|
|
173
|
+
);
|
|
174
|
+
assert.deepEqual((same!.merged as PluginsSection).plugins.map((p) => p.name), ['alpha']);
|
|
175
|
+
const both = mergePluginsSection(section([]), section([]), section([entry('alpha', '1.2.0')]), ON);
|
|
176
|
+
assert.deepEqual((both!.merged as PluginsSection).plugins, []);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test('mergePluginsSection: 无祖先时本机独有/远端独有都保留(不按删除解释)', () => {
|
|
180
|
+
const res = mergePluginsSection(section([entry('mine', '1.0.0')]), section([entry('theirs', '1.0.0')]), undefined, ON);
|
|
181
|
+
assert.deepEqual(res!.conflicts, []);
|
|
182
|
+
assert.deepEqual((res!.merged as PluginsSection).plugins.map((p) => p.name).sort(), ['mine', 'theirs']);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test('mergePluginsSection: 两侧把同一插件改到不同版本 → both-modified 冲突', () => {
|
|
186
|
+
const res = mergePluginsSection(
|
|
187
|
+
section([entry('alpha', '1.3.0')]),
|
|
188
|
+
section([entry('alpha', '1.4.0')]),
|
|
189
|
+
section([entry('alpha', '1.2.0')]),
|
|
190
|
+
ON,
|
|
191
|
+
);
|
|
192
|
+
assert.equal(res!.conflicts.length, 1);
|
|
193
|
+
assert.equal(res!.conflicts[0]!.reason, 'both-modified');
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test('mergePluginsSection: 插件列表以外的字段(patch 行等)两侧不必相同 —— 不同即交回通用合并', () => {
|
|
197
|
+
const res = mergePluginsSection(
|
|
198
|
+
section([entry('alpha', '1.2.0')], { pnpmWorkspace: 'onlyBuiltDependencies: [a]\n' }),
|
|
199
|
+
section([entry('alpha', '1.2.0')], { pnpmWorkspace: 'onlyBuiltDependencies: [b]\n' }),
|
|
200
|
+
section([entry('alpha', '1.2.0')]),
|
|
201
|
+
ON,
|
|
202
|
+
);
|
|
203
|
+
assert.equal(res, undefined, 'patch/pnpmWorkspace 的口径由通用路径负责,这里不自造第二套');
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test('mergePluginsSection: 形状不认识 → undefined(畸形数据交通用路径的形状闸门)', () => {
|
|
207
|
+
assert.equal(mergePluginsSection({ version: 2 } as unknown as PluginsSection, section([]), undefined, ON), undefined);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test('mergePluginsSection: 开关关着 → 钩子不参与(undefined = 通用合并,行为与 #132 之前逐字一致)', () => {
|
|
211
|
+
assert.equal(
|
|
212
|
+
mergePluginsSection(
|
|
213
|
+
section([entry('alpha', '1.2.0')]),
|
|
214
|
+
section([]),
|
|
215
|
+
section([entry('alpha', '1.2.0')]),
|
|
216
|
+
{ deletePlugins: false },
|
|
217
|
+
),
|
|
218
|
+
undefined,
|
|
219
|
+
'开关关闭时绝不能丢掉本机条目:包还在盘上,下一轮导出会把它当本机独有推回远端 = 复活',
|
|
220
|
+
);
|
|
221
|
+
});
|