dsh-config-manager 0.6.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 +14 -2
- package/lib/client.js +722 -647
- package/lib/core/backup.d.ts +9 -1
- package/lib/core/backup.js +21 -0
- package/lib/core/local-plugin-host.d.ts +9 -0
- package/lib/core/local-plugin-host.js +174 -1
- package/lib/core/messages-core.d.ts +3 -0
- package/lib/core/messages-core.js +6 -0
- package/lib/core/messages.d.ts +8 -0
- package/lib/core/messages.js +18 -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/run-registry.d.ts +10 -0
- package/lib/core/run-registry.js +17 -0
- package/lib/core/snapshot-diff.js +2 -1
- package/lib/core/types.d.ts +32 -0
- package/lib/index.d.ts +8 -2
- package/lib/index.js +24 -21
- package/lib/routes/import.js +6 -1
- package/lib/routes/route-support.d.ts +7 -1
- package/lib/routes/route-support.js +36 -5
- package/lib/routes/sync.js +8 -2
- package/lib/sync/autosync-config.d.ts +9 -1
- package/lib/sync/autosync-config.js +10 -2
- package/lib/sync/autosync-scheduler.js +18 -6
- package/lib/sync/git/git-transport.d.ts +31 -1
- package/lib/sync/git/git-transport.js +119 -23
- 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-config.d.ts +34 -3
- package/lib/sync/sync-config.js +38 -5
- 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 +14 -0
- package/lib/ui/sync-settings-view.js +5 -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 +8 -6
- package/src/client/run-store.ts +9 -5
- package/src/client/sync/ChannelConfigDialog.tsx +15 -1
- package/src/client/sync/SyncPanel.tsx +16 -0
- package/src/client/sync/SyncSectionPickerDialog.tsx +17 -1
- package/src/client/sync/sync-api.ts +2 -0
- package/src/client/sync/sync-locales.ts +10 -0
- package/src/client/sync/sync-view.ts +6 -0
- package/src/core/backup.ts +21 -1
- package/src/core/local-plugin-host.test.ts +31 -2
- package/src/core/local-plugin-host.ts +175 -2
- package/src/core/messages-core.ts +6 -0
- package/src/core/messages.ts +18 -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/run-registry-runs.test.ts +26 -1
- package/src/core/run-registry.ts +18 -0
- 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.sync.test.ts +27 -0
- package/src/index.ts +30 -28
- package/src/routes/import.ts +6 -1
- package/src/routes/route-support.ts +34 -5
- package/src/routes/sync.ts +8 -2
- package/src/security/security.test.ts +1 -0
- package/src/sync/autosync-config.ts +10 -2
- package/src/sync/autosync-scheduler.test.ts +61 -1
- package/src/sync/autosync-scheduler.ts +17 -5
- package/src/sync/git/git-transport.test.ts +117 -0
- package/src/sync/git/git-transport.ts +131 -23
- 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-config.test.ts +59 -1
- package/src/sync/sync-config.ts +51 -5
- 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 +21 -0
- package/src/ui/sync-settings-view.ts +19 -0
|
@@ -26,7 +26,7 @@ import {
|
|
|
26
26
|
SYNC_CONFIG_FILE, SYNC_CONFIG_SCHEMA_VERSION, SYNC_CONFIG_SUPPORTED_VERSIONS, CLOUD_SYNC_CONFIG_FILE,
|
|
27
27
|
validateWebDavUrl, validateRepoUrl, type SyncConfig, type SyncConfigIssue,
|
|
28
28
|
SYNC_CHANNELS, channelOf, channelMap, isSyncTransportType, parseSyncChannel, clearSyncChannel,
|
|
29
|
-
S3_COMPAT_PROVIDERS, CLOUD_SYNC_PROVIDERS, cloudSecretRef,
|
|
29
|
+
S3_COMPAT_PROVIDERS, CLOUD_SYNC_PROVIDERS, cloudSecretRef, normalizeGitSubdir,
|
|
30
30
|
} from './sync-config.ts';
|
|
31
31
|
// t88 跨侧常量守卫:共享零依赖常量 + 客户端镜像(见文件末尾的 t88 用例)
|
|
32
32
|
import { S3_COMPAT_PROVIDERS as SHARED_S3_COMPAT_PROVIDERS, cloudSecretRefName } from '../utils/shared-constants.ts';
|
|
@@ -919,3 +919,61 @@ test('validateRepoUrl(RV1-F7):带 scheme 的 userinfo —— ssh 的 token
|
|
|
919
919
|
assert.ok(half !== null);
|
|
920
920
|
assert.ok(!half.includes('ghp_halfsecret'), '无法解析的地址同样不得回显凭据: ' + half);
|
|
921
921
|
});
|
|
922
|
+
|
|
923
|
+
test('issue #131:normalizeGitSubdir —— 中文与嵌套放行,绝对路径/../反斜杠/空段/.git 段拒绝', () => {
|
|
924
|
+
const ok = (raw: unknown): string => {
|
|
925
|
+
const r = normalizeGitSubdir(raw);
|
|
926
|
+
assert.equal(r.ok, true, '应放行: ' + String(raw));
|
|
927
|
+
return r.ok ? r.value : '';
|
|
928
|
+
};
|
|
929
|
+
const bad = (raw: unknown): void => {
|
|
930
|
+
assert.equal(normalizeGitSubdir(raw).ok, false, '应拒绝: ' + String(raw));
|
|
931
|
+
};
|
|
932
|
+
assert.equal(ok(undefined), '', '缺省 = 仓库根');
|
|
933
|
+
assert.equal(ok(''), '', '空串 = 仓库根');
|
|
934
|
+
assert.equal(ok(' '), '', '纯空白 = 仓库根');
|
|
935
|
+
assert.equal(ok(' DSH备份 '), 'DSH备份', '去首尾空白,保留 UTF-8');
|
|
936
|
+
assert.equal(ok('tools/dsh'), 'tools/dsh', '允许嵌套');
|
|
937
|
+
for (const raw of ['/abs', 'a/', '/', 'a//b', '..', 'a/../b', './a', 'a/.', '.git', 'a/.git/b', 'a/.GIT', 'a\\b', 'a\u0000b', 123, true]) bad(raw);
|
|
938
|
+
});
|
|
939
|
+
|
|
940
|
+
test('issue #131:git 子目录前缀往返 —— 写入保留字段,空/缺省按仓库根读回,旧 v2 文件不受影响', async () => {
|
|
941
|
+
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'dsh-sync-cfg-subdir-'));
|
|
942
|
+
try {
|
|
943
|
+
const cfg: SyncConfig = {
|
|
944
|
+
schemaVersion: 2,
|
|
945
|
+
transport: 'git',
|
|
946
|
+
git: { repoUrl: 'git@github.com:foo/bar.git', subdir: 'DSH备份' },
|
|
947
|
+
};
|
|
948
|
+
await writeSyncConfig(dir, cfg);
|
|
949
|
+
assert.deepEqual(await readSyncConfig(dir), cfg, '子目录前缀必须原样往返');
|
|
950
|
+
const raw = JSON.parse(await fs.readFile(path.join(dir, SYNC_CONFIG_FILE), 'utf8'));
|
|
951
|
+
assert.equal(raw.git.subdir, 'DSH备份');
|
|
952
|
+
assert.equal(raw.schemaVersion, SYNC_CONFIG_SCHEMA_VERSION, '带新字段的文件按当前版本写');
|
|
953
|
+
|
|
954
|
+
// 旧 v2 文件(无 subdir)→ 读成仓库根布局,不因新字段缺失而丢配置
|
|
955
|
+
await fs.writeFile(path.join(dir, SYNC_CONFIG_FILE), JSON.stringify({
|
|
956
|
+
schemaVersion: 2,
|
|
957
|
+
transport: 'git',
|
|
958
|
+
git: { repoUrl: 'git@github.com:foo/bar.git' },
|
|
959
|
+
}));
|
|
960
|
+
assert.deepEqual(await readSyncConfig(dir), {
|
|
961
|
+
schemaVersion: 2,
|
|
962
|
+
transport: 'git',
|
|
963
|
+
git: { repoUrl: 'git@github.com:foo/bar.git' },
|
|
964
|
+
});
|
|
965
|
+
|
|
966
|
+
// 非法 subdir(历史文件被人改坏)→ 丢弃该字段而不是整份配置报错
|
|
967
|
+
await fs.writeFile(path.join(dir, SYNC_CONFIG_FILE), JSON.stringify({
|
|
968
|
+
schemaVersion: 2,
|
|
969
|
+
transport: 'git',
|
|
970
|
+
git: { repoUrl: 'git@github.com:foo/bar.git', subdir: '../escape' },
|
|
971
|
+
}));
|
|
972
|
+
assert.deepEqual(await readSyncConfig(dir), {
|
|
973
|
+
schemaVersion: 2,
|
|
974
|
+
transport: 'git',
|
|
975
|
+
git: { repoUrl: 'git@github.com:foo/bar.git' },
|
|
976
|
+
}, '非法前缀按仓库根处理(不拿坏值去拼路径)');
|
|
977
|
+
} finally { await fs.rm(dir, { recursive: true, force: true }); }
|
|
978
|
+
});
|
|
979
|
+
|
package/src/sync/sync-config.ts
CHANGED
|
@@ -87,14 +87,26 @@ export function channelMap<T>(make: (channel: SyncTransportType) => T): Record<S
|
|
|
87
87
|
return out;
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
/**
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
90
|
+
/**
|
|
91
|
+
* 当前 sync-config.json schema 版本号(v4:git 通道新增可选 `subdir` 子目录前缀,issue #131)。
|
|
92
|
+
*
|
|
93
|
+
* 为什么必须升版而不是只加一个可选字段:老版本插件读到带 `subdir` 的配置会**丢掉这个字段**
|
|
94
|
+
* 然后照旧往仓库根写快照 —— 在「多工具共用一个备份仓库」的现场,那等于把快照撒进别人的目录布局。
|
|
95
|
+
* 升版后老版本读到 v4 直接判为不支持(显示未配置),宁可让用户看见「要升级」,也不静默写错位置。
|
|
96
|
+
*/
|
|
97
|
+
export const SYNC_CONFIG_SCHEMA_VERSION = 4;
|
|
98
|
+
/** 历史可读取版本:v1、v2、v3、v4。 */
|
|
99
|
+
export const SYNC_CONFIG_SUPPORTED_VERSIONS: readonly number[] = [1, 2, 3, 4];
|
|
94
100
|
|
|
95
101
|
/** git 通道配置(不含任何凭据;git 可执行文件固定使用系统 PATH 中的 git) */
|
|
96
102
|
export interface GitConfig {
|
|
97
103
|
repoUrl: string;
|
|
104
|
+
/**
|
|
105
|
+
* 仓库内子目录前缀(issue #131,可缺省):快照布局落在 `<subdir>/snapshots`、
|
|
106
|
+
* `<subdir>/snapshots-encrypted`、`<subdir>/blobs`。缺省 = 仓库根(历史行为逐字不变)。
|
|
107
|
+
* 多工具共用同一个私有备份仓库时,每个工具各占一个子目录。
|
|
108
|
+
*/
|
|
109
|
+
subdir?: string;
|
|
98
110
|
}
|
|
99
111
|
|
|
100
112
|
/** webdav 通道配置(不含 password;password 走 DSH credentials) */
|
|
@@ -149,7 +161,12 @@ export function isGistConfig(cfg: SyncConfig): cfg is Extract<SyncConfig, { tran
|
|
|
149
161
|
/** 从 v1 扁平形态解析 git 配置;缺 repoUrl → null(gitBin 已废弃:始终使用系统 PATH 中的 git) */
|
|
150
162
|
function parseV1Git(obj: Record<string, unknown>): GitConfig | null {
|
|
151
163
|
if (typeof obj['repoUrl'] !== 'string' || obj['repoUrl'] === '') return null;
|
|
152
|
-
|
|
164
|
+
// issue #131:子目录前缀必须在这里**显式搬运** —— 本函数只构造已知字段,漏搬就是静默丢掉用户配置。
|
|
165
|
+
// 读侧同口径校验:非法值当作「未设置」(落在仓库根),绝不把可疑路径继续交给 git 子进程。
|
|
166
|
+
const subdir = normalizeGitSubdir(obj['subdir']);
|
|
167
|
+
return subdir.ok && subdir.value !== ''
|
|
168
|
+
? { repoUrl: obj['repoUrl'], subdir: subdir.value }
|
|
169
|
+
: { repoUrl: obj['repoUrl'] };
|
|
153
170
|
}
|
|
154
171
|
|
|
155
172
|
/** 从 v2 git 命名空间解析;缺有效 repoUrl → null */
|
|
@@ -917,6 +934,35 @@ export function validateCloudBucket(bucket: string): CloudConfigIssueCode | null
|
|
|
917
934
|
return null;
|
|
918
935
|
}
|
|
919
936
|
|
|
937
|
+
/**
|
|
938
|
+
* git 通道子目录前缀的归一化与校验(issue #131)。
|
|
939
|
+
*
|
|
940
|
+
* 为什么单独一个函数而不是复用 validateCloudPrefix:对象键前缀与仓库内目录名不是同一套约束 ——
|
|
941
|
+
* 目录名要允许 UTF-8(报告者现场就是 `DSH备份/` 这种中文目录)与多级嵌套,而对象键前缀按
|
|
942
|
+
* 存储桶语义只关心「不是绝对路径、没有 ..」。
|
|
943
|
+
*
|
|
944
|
+
* 接受:空串(= 仓库根,历史行为)、UTF-8 段、多级嵌套(`a/b`)。
|
|
945
|
+
* 拒绝:绝对路径(首部 /)、尾部 /、反斜杠、空段(含 `a//b`)、`.` / `..` 段、控制字符、
|
|
946
|
+
* `.git` 段(写进去会撞 git 自己的元数据目录名,任何大小写都算)。
|
|
947
|
+
*/
|
|
948
|
+
export type GitSubdirCheck = { ok: true; value: string } | { ok: false; reason: 'gitSubdirInvalid' };
|
|
949
|
+
|
|
950
|
+
export function normalizeGitSubdir(raw: unknown): GitSubdirCheck {
|
|
951
|
+
// 缺省(字段不存在)与空串同义:仓库根。只有「显式写了但不是字符串」才算非法。
|
|
952
|
+
if (raw === undefined || raw === null) return { ok: true, value: '' };
|
|
953
|
+
if (typeof raw !== 'string') return { ok: false, reason: 'gitSubdirInvalid' };
|
|
954
|
+
const p = raw.trim();
|
|
955
|
+
if (p === '') return { ok: true, value: '' };
|
|
956
|
+
if (p.startsWith('/') || p.endsWith('/')) return { ok: false, reason: 'gitSubdirInvalid' };
|
|
957
|
+
if (p.includes('\\') || /[\u0000-\u001f\u007f]/.test(p)) return { ok: false, reason: 'gitSubdirInvalid' };
|
|
958
|
+
const segments = p.split('/');
|
|
959
|
+
for (const seg of segments) {
|
|
960
|
+
if (seg === '' || seg === '.' || seg === '..') return { ok: false, reason: 'gitSubdirInvalid' };
|
|
961
|
+
if (seg.toLowerCase() === '.git') return { ok: false, reason: 'gitSubdirInvalid' };
|
|
962
|
+
}
|
|
963
|
+
return { ok: true, value: segments.join('/') };
|
|
964
|
+
}
|
|
965
|
+
|
|
920
966
|
/** 对象键前缀校验(可缺省 = 根前缀;不得以 / 开头、不得含 .. / 反斜杠 / 空段)。 */
|
|
921
967
|
export function validateCloudPrefix(prefix: string): CloudConfigIssueCode | null {
|
|
922
968
|
if (typeof prefix !== 'string') return 'cloud.prefixInvalid';
|
|
@@ -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
|
+
});
|