Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
294 lines
7.0 KiB
TypeScript
294 lines
7.0 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import {
|
|
collectSyncDeletions,
|
|
summarizeSyncChanges,
|
|
withSyncReliabilityMeta,
|
|
} from "./syncReliability.ts";
|
|
import type { SyncPayload } from "./sync.ts";
|
|
|
|
function payload(overrides: Partial<SyncPayload> = {}): SyncPayload {
|
|
return {
|
|
hosts: [],
|
|
keys: [],
|
|
identities: [],
|
|
proxyProfiles: [],
|
|
snippets: [],
|
|
customGroups: [],
|
|
snippetPackages: [],
|
|
portForwardingRules: [],
|
|
groupConfigs: [],
|
|
settings: undefined,
|
|
syncedAt: 0,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
test("summarizeSyncChanges records whether local data changed", () => {
|
|
const base = payload({
|
|
hosts: [{
|
|
id: "host-1",
|
|
label: "Old",
|
|
hostname: "old.example.com",
|
|
username: "root",
|
|
tags: [],
|
|
os: "linux",
|
|
}],
|
|
});
|
|
const local = payload({
|
|
hosts: [{
|
|
id: "host-1",
|
|
label: "New",
|
|
hostname: "old.example.com",
|
|
username: "root",
|
|
tags: [],
|
|
os: "linux",
|
|
}],
|
|
});
|
|
|
|
const summary = summarizeSyncChanges(base, local);
|
|
|
|
assert.equal(summary.hasLocalChanges, true);
|
|
assert.equal(summary.byEntity.hosts.modified.local, 1);
|
|
});
|
|
|
|
test("collectSyncDeletions records deleted entities explicitly", () => {
|
|
const base = payload({
|
|
hosts: [{
|
|
id: "host-1",
|
|
label: "Old",
|
|
hostname: "old.example.com",
|
|
username: "root",
|
|
tags: [],
|
|
os: "linux",
|
|
}],
|
|
customGroups: ["prod"],
|
|
});
|
|
|
|
const deletions = collectSyncDeletions(base, payload(), {
|
|
deletedAt: 123,
|
|
deviceId: "device-a",
|
|
});
|
|
|
|
assert.deepEqual(deletions, [
|
|
{ entityType: "hosts", id: "host-1", deletedAt: 123, deviceId: "device-a" },
|
|
{ entityType: "customGroups", id: "prod", deletedAt: 123, deviceId: "device-a" },
|
|
]);
|
|
});
|
|
|
|
test("collectSyncDeletions records group config deletions by path", () => {
|
|
const deletions = collectSyncDeletions(
|
|
payload({ groupConfigs: [{ path: "prod", username: "root" }] }),
|
|
payload({ groupConfigs: [] }),
|
|
{ deletedAt: 123 },
|
|
);
|
|
|
|
assert.deepEqual(deletions, [{
|
|
entityType: "groupConfigs",
|
|
id: "prod",
|
|
deletedAt: 123,
|
|
}]);
|
|
});
|
|
|
|
test("summarizeSyncChanges treats missing optional arrays as legacy payloads", () => {
|
|
const base = payload({
|
|
groupConfigs: [{ path: "prod", username: "root" }],
|
|
});
|
|
const remote = payload();
|
|
delete remote.groupConfigs;
|
|
|
|
const summary = summarizeSyncChanges(base, base, remote);
|
|
|
|
assert.equal(summary.hasRemoteChanges, false);
|
|
assert.equal(summary.byEntity.groupConfigs, undefined);
|
|
});
|
|
|
|
test("summarizeSyncChanges reports conflict categories", () => {
|
|
const base = payload({
|
|
hosts: [{
|
|
id: "host-1",
|
|
label: "Base",
|
|
hostname: "base.example.com",
|
|
username: "root",
|
|
tags: [],
|
|
os: "linux",
|
|
}],
|
|
});
|
|
const local = payload({
|
|
hosts: [{
|
|
id: "host-1",
|
|
label: "Local",
|
|
hostname: "base.example.com",
|
|
username: "root",
|
|
tags: [],
|
|
os: "linux",
|
|
}],
|
|
});
|
|
const remote = payload({
|
|
hosts: [{
|
|
id: "host-1",
|
|
label: "Remote",
|
|
hostname: "base.example.com",
|
|
username: "root",
|
|
tags: [],
|
|
os: "linux",
|
|
}],
|
|
});
|
|
|
|
const summary = summarizeSyncChanges(base, local, remote);
|
|
|
|
assert.equal(summary.hasConflicts, true);
|
|
assert.deepEqual(summary.conflicts, [{
|
|
entityType: "hosts",
|
|
id: "host-1",
|
|
kind: "both-modified",
|
|
}]);
|
|
});
|
|
|
|
test("summarizeSyncChanges reports both-added conflicts by entity type", () => {
|
|
const local = payload({
|
|
hosts: [{
|
|
id: "host-1",
|
|
label: "Local",
|
|
hostname: "local.example.com",
|
|
username: "root",
|
|
tags: [],
|
|
os: "linux",
|
|
}],
|
|
});
|
|
const remote = payload({
|
|
hosts: [{
|
|
id: "host-1",
|
|
label: "Remote",
|
|
hostname: "remote.example.com",
|
|
username: "root",
|
|
tags: [],
|
|
os: "linux",
|
|
}],
|
|
});
|
|
|
|
const summary = summarizeSyncChanges(payload(), local, remote);
|
|
|
|
assert.equal(summary.hasConflicts, true);
|
|
assert.deepEqual(summary.conflicts, [{
|
|
entityType: "hosts",
|
|
id: "host-1",
|
|
kind: "both-added",
|
|
}]);
|
|
});
|
|
|
|
test("summarizeSyncChanges ignores lastConnectedAt-only host diffs", () => {
|
|
const host = {
|
|
id: "host-1",
|
|
label: "prod",
|
|
hostname: "prod.example.com",
|
|
username: "root",
|
|
tags: [],
|
|
os: "linux" as const,
|
|
};
|
|
const base = payload({ hosts: [{ ...host, lastConnectedAt: 10 }] });
|
|
const local = payload({ hosts: [host] });
|
|
const remote = payload({ hosts: [{ ...host, lastConnectedAt: 99 }] });
|
|
|
|
const summary = summarizeSyncChanges(base, local, remote);
|
|
|
|
assert.equal(summary.hasLocalChanges, false);
|
|
assert.equal(summary.hasRemoteChanges, false);
|
|
assert.equal(summary.hasConflicts, false);
|
|
});
|
|
|
|
test("summarizeSyncChanges still reports a real remote host edit after stripping lastConnectedAt", () => {
|
|
const host = {
|
|
id: "host-1",
|
|
label: "prod",
|
|
hostname: "prod.example.com",
|
|
username: "root",
|
|
tags: [],
|
|
os: "linux" as const,
|
|
};
|
|
const summary = summarizeSyncChanges(
|
|
payload({ hosts: [{ ...host, lastConnectedAt: 10 }] }),
|
|
payload({ hosts: [host] }),
|
|
payload({ hosts: [{ ...host, lastConnectedAt: 10, label: "prod-renamed" }] }),
|
|
);
|
|
|
|
assert.equal(summary.hasLocalChanges, false);
|
|
assert.equal(summary.hasRemoteChanges, true);
|
|
assert.equal(summary.hasConflicts, false);
|
|
});
|
|
|
|
test("withSyncReliabilityMeta carries old deletion records until the entity is recreated", () => {
|
|
const current = payload({
|
|
syncMeta: {
|
|
schemaVersion: 1,
|
|
generatedAt: 100,
|
|
localChanged: true,
|
|
deletions: [{
|
|
entityType: "hosts",
|
|
id: "host-1",
|
|
deletedAt: 100,
|
|
deviceId: "device-a",
|
|
}],
|
|
changeSummary: {
|
|
hasLocalChanges: true,
|
|
hasRemoteChanges: false,
|
|
hasConflicts: false,
|
|
byEntity: {},
|
|
conflicts: [],
|
|
},
|
|
},
|
|
});
|
|
|
|
const enriched = withSyncReliabilityMeta(current, payload(), {
|
|
deviceId: "device-a",
|
|
now: 200,
|
|
});
|
|
|
|
assert.deepEqual(enriched.syncMeta?.deletions, [{
|
|
entityType: "hosts",
|
|
id: "host-1",
|
|
deletedAt: 100,
|
|
deviceId: "device-a",
|
|
}]);
|
|
|
|
const recreated = withSyncReliabilityMeta(
|
|
payload({
|
|
hosts: [{
|
|
id: "host-1",
|
|
label: "Recreated",
|
|
hostname: "new.example.com",
|
|
username: "root",
|
|
tags: [],
|
|
os: "linux",
|
|
}],
|
|
syncMeta: enriched.syncMeta,
|
|
}),
|
|
payload(),
|
|
{ deviceId: "device-a", now: 300 },
|
|
);
|
|
|
|
assert.deepEqual(recreated.syncMeta?.deletions, []);
|
|
});
|
|
|
|
test("withSyncReliabilityMeta attaches change summary and deletion records", () => {
|
|
const base = payload({
|
|
snippets: [{ id: "snippet-1", label: "Old", command: "ls" }],
|
|
});
|
|
const current = payload();
|
|
|
|
const enriched = withSyncReliabilityMeta(current, base, {
|
|
deviceId: "device-a",
|
|
now: 456,
|
|
});
|
|
|
|
assert.equal(enriched.syncMeta?.schemaVersion, 1);
|
|
assert.equal(enriched.syncMeta?.localChanged, true);
|
|
assert.deepEqual(enriched.syncMeta?.deletions, [{
|
|
entityType: "snippets",
|
|
id: "snippet-1",
|
|
deletedAt: 456,
|
|
deviceId: "device-a",
|
|
}]);
|
|
});
|