feat(client): define digital sync contract

This commit is contained in:
baiyanyun
2026-06-08 17:10:38 +08:00
parent ff25c2fef7
commit 3bab6af63c
3 changed files with 150 additions and 7 deletions

View File

@@ -89,6 +89,33 @@ describe("digital employee sync service", () => {
const { flushDigitalEmployeeSyncOutbox } = await import("./syncService");
const status = await flushDigitalEmployeeSyncOutbox({ force: true });
expect(lastSyncRequestBody()).toMatchObject({
client_key: "client-key-001",
device_id: "device-001",
contract_version: "qimingclaw.digital_employee.sync.v1",
items: [
expect.objectContaining({
outbox_id: "plan:upsert:plan-1",
entity_type: "plan",
entity_id: "plan-1",
operation: "upsert",
contract_version: "qimingclaw.digital_employee.sync.v1",
entity_summary: {
title: "同步测试计划 plan-1",
status: "pending",
summary: "用于验证同步失败诊断的业务摘要",
},
business_view: expect.objectContaining({
entity_type: "plan",
local_id: "plan-1",
title: "同步测试计划 plan-1",
status: "pending",
objective: "用于验证同步失败诊断的业务摘要",
}),
payload: { ok: true },
}),
],
});
expect(status.pending).toBe(0);
expect(status.failed).toBe(0);
expect(readOutbox("plan:upsert:plan-1")).toMatchObject({
@@ -292,6 +319,11 @@ function jsonResponse(body: unknown): Response {
} as unknown as Response;
}
function lastSyncRequestBody(): Record<string, unknown> {
const init = mockState.fetch.mock.calls.at(-1)?.[1] as RequestInit | undefined;
return JSON.parse(String(init?.body ?? "{}")) as Record<string, unknown>;
}
interface TestOutboxRow {
id: string;
entity_type: string;
@@ -427,6 +459,16 @@ class TestDb {
return this.plans.get(id);
}
if (sql.startsWith("SELECT name AS title, kind AS status, uri AS summary, payload FROM digital_artifacts")) {
const [id] = args as [string];
return this.artifacts.get(id);
}
if (sql.startsWith("SELECT title, status, NULL AS objective, payload FROM digital_approvals")) {
const [id] = args as [string];
return this.approvals.get(id);
}
if (
sql.includes("COUNT(*) AS count FROM digital_sync_outbox WHERE status = ?")
) {