Add full coverage knowledge document synchronization to sync-test
This commit is contained in:
1 parent
43a18d0515
commit
93f61a807a
6 files changed
+302
-14
No files matched your search
@@ -256,6 +256,15 @@ describe("corps sync-test", () => {
|
||||
expect(parsed.rulesPlan.targetExistingDraftRuleCount).toBe(1);
|
||||
expect(parsed.rulesPlan.toDeleteCount).toBe(1);
|
||||
expect(parsed.rulesPlan.toCreateCount).toBe(2);
|
||||
expect(parsed.documentsPlan).toBeDefined();
|
||||
expect(parsed.documentsPlan.sourceDocumentCount).toBe(2);
|
||||
expect(parsed.documentsPlan.targetExistingDocumentCount).toBe(2);
|
||||
expect(parsed.documentsPlan.toCreateCount).toBe(1);
|
||||
expect(parsed.documentsPlan.toCreate[0].title).toBe("Doc Alpha");
|
||||
expect(parsed.documentsPlan.toUpdateCount).toBe(1);
|
||||
expect(parsed.documentsPlan.toUpdate[0].title).toBe("Doc Beta");
|
||||
expect(parsed.documentsPlan.toDeleteCount).toBe(1);
|
||||
expect(parsed.documentsPlan.toDelete[0].title).toBe("Doc Gamma Old");
|
||||
expect(parsed.productionGuarantees.prodRulesStayDraft).toBe(true);
|
||||
expect(parsed.productionGuarantees.prodVersionPublished).toBe(false);
|
||||
|
||||
@@ -304,6 +313,15 @@ describe("corps sync-test", () => {
|
||||
expect(parsed.target.createdDraftRulesCount).toBe(2);
|
||||
expect(parsed.target.publishedVersion.versionName).toBe("v1.5-staging");
|
||||
expect(parsed.target.publishedVersion.versionNumber).toBe(3);
|
||||
expect(parsed.documents).toBeDefined();
|
||||
expect(parsed.documents.sourceCount).toBe(2);
|
||||
expect(parsed.documents.targetExistingCount).toBe(2);
|
||||
expect(parsed.documents.createdCount).toBe(1);
|
||||
expect(parsed.documents.created[0].title).toBe("Doc Alpha");
|
||||
expect(parsed.documents.updatedCount).toBe(1);
|
||||
expect(parsed.documents.updated[0].title).toBe("Doc Beta");
|
||||
expect(parsed.documents.deletedCount).toBe(1);
|
||||
expect(parsed.documents.deleted[0].title).toBe("Doc Gamma Old");
|
||||
expect(parsed.productionGuarantees.prodRulesStayDraft).toBe(true);
|
||||
expect(parsed.productionGuarantees.prodVersionPublished).toBe(false);
|
||||
|
||||
@@ -319,6 +337,17 @@ describe("corps sync-test", () => {
|
||||
expect(postPref).toBeDefined();
|
||||
expect((postPref?.body as any).decisionPreferences?.explicitApprove).toBe("Prod approve note");
|
||||
|
||||
const deleteDoc = requests.find((r) => r.method === "DELETE" && r.path === "/api/v1/knowledge/articles/test-doc-old");
|
||||
expect(deleteDoc).toBeDefined();
|
||||
|
||||
const patchDoc = requests.find((r) => r.method === "PATCH" && r.path === "/api/v1/knowledge/articles/test-doc-2");
|
||||
expect(patchDoc).toBeDefined();
|
||||
expect((patchDoc?.body as any).attributes?.content).toBe("Content Beta Updated");
|
||||
|
||||
const postDoc = requests.find((r) => r.method === "POST" && r.path === "/api/v1/knowledge/articles");
|
||||
expect(postDoc).toBeDefined();
|
||||
expect((postDoc?.body as any).title).toBe("Doc Alpha");
|
||||
|
||||
const deleteRule = requests.find((r) => r.method === "DELETE" && r.path === "/api/v1/rules/test-rule-old");
|
||||
expect(deleteRule).toBeDefined();
|
||||
|
||||
@@ -458,6 +487,97 @@ function mockSealSyncRequests(options: {
|
||||
return jsonResponse({ data: { success: true } });
|
||||
}
|
||||
|
||||
if (url.pathname === "/api/v1/knowledge/articles" && method === "GET") {
|
||||
if (isProd) {
|
||||
return jsonResponse({
|
||||
data: {
|
||||
articles: [
|
||||
{
|
||||
id: "prod-doc-1",
|
||||
title: "Doc Alpha",
|
||||
purpose: "Purpose Alpha",
|
||||
content: "Content Alpha",
|
||||
contentType: "policy",
|
||||
status: "published",
|
||||
enabled: true,
|
||||
scenarios: ["audit"],
|
||||
tags: []
|
||||
},
|
||||
{
|
||||
id: "prod-doc-2",
|
||||
title: "Doc Beta",
|
||||
purpose: "Purpose Beta",
|
||||
content: "Content Beta Updated",
|
||||
contentType: "policy",
|
||||
status: "published",
|
||||
enabled: true,
|
||||
scenarios: ["audit"],
|
||||
tags: []
|
||||
}
|
||||
],
|
||||
total: 2
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return jsonResponse({
|
||||
data: {
|
||||
articles: [
|
||||
{
|
||||
id: "test-doc-2",
|
||||
title: "Doc Beta",
|
||||
purpose: "Purpose Beta",
|
||||
content: "Content Beta Old",
|
||||
contentType: "policy",
|
||||
status: "published",
|
||||
enabled: true,
|
||||
scenarios: ["audit"],
|
||||
tags: []
|
||||
},
|
||||
{
|
||||
id: "test-doc-old",
|
||||
title: "Doc Gamma Old",
|
||||
purpose: "Redundant doc",
|
||||
content: "To be deleted",
|
||||
contentType: "policy",
|
||||
status: "published",
|
||||
enabled: true,
|
||||
scenarios: ["audit"],
|
||||
tags: []
|
||||
}
|
||||
],
|
||||
total: 2
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (url.pathname.startsWith("/api/v1/knowledge/articles/") && method === "DELETE") {
|
||||
options.onMutation?.("DELETE", url.pathname);
|
||||
return jsonResponse({ data: { success: true } });
|
||||
}
|
||||
|
||||
if (url.pathname.startsWith("/api/v1/knowledge/articles/") && method === "PATCH") {
|
||||
const body = JSON.parse(rawBody || "{}");
|
||||
options.onMutation?.("PATCH", url.pathname, body);
|
||||
return jsonResponse({
|
||||
data: {
|
||||
id: url.pathname.split("/").pop(),
|
||||
...body.attributes
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (url.pathname === "/api/v1/knowledge/articles" && method === "POST") {
|
||||
const body = JSON.parse(rawBody || "{}");
|
||||
options.onMutation?.("POST", url.pathname, body);
|
||||
return jsonResponse({
|
||||
data: {
|
||||
id: `new-doc-${Date.now()}`,
|
||||
...body
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (url.pathname === "/api/v1/rules" && method === "GET") {
|
||||
if (isProd) {
|
||||
return jsonResponse({
|
||||
|
||||
+141
-13
@@ -283,14 +283,71 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
|
||||
const prodClient = await createSealClient(prodCorp);
|
||||
const testClient = await createSealClient(testCorp);
|
||||
|
||||
const prodPref = (await api.getApprovalStylePreferences(prodClient, prodCorp.seal.endpoints.approvalStylePreferences)) as any;
|
||||
const testPref = (await api.getApprovalStylePreferences(testClient, testCorp.seal.endpoints.approvalStylePreferences)) as any;
|
||||
const [
|
||||
prodPref,
|
||||
testPref,
|
||||
prodRules,
|
||||
testRules,
|
||||
prodVersions,
|
||||
testVersions,
|
||||
prodDocsRes,
|
||||
testDocsRes
|
||||
] = await Promise.all([
|
||||
api.getApprovalStylePreferences(prodClient, prodCorp.seal.endpoints.approvalStylePreferences).catch(() => ({} as any)),
|
||||
api.getApprovalStylePreferences(testClient, testCorp.seal.endpoints.approvalStylePreferences).catch(() => ({} as any)),
|
||||
api.listApprovalRules(prodClient),
|
||||
api.listApprovalRules(testClient),
|
||||
api.listApprovalRuleVersions(prodClient).catch(() => []),
|
||||
api.listApprovalRuleVersions(testClient).catch(() => []),
|
||||
api.listApprovalDocuments(prodClient, { limit: 100 }).catch(() => ({ articles: [] })),
|
||||
api.listApprovalDocuments(testClient, { limit: 100 }).catch(() => ({ articles: [] }))
|
||||
]);
|
||||
|
||||
const prodRules = await api.listApprovalRules(prodClient);
|
||||
const testRules = await api.listApprovalRules(testClient);
|
||||
const prodDocs = (prodDocsRes.articles || []).filter((d) => !d.isDeleted);
|
||||
const testDocs = (testDocsRes.articles || []).filter((d) => !d.isDeleted);
|
||||
|
||||
const prodVersions = await api.listApprovalRuleVersions(prodClient).catch(() => []);
|
||||
const testVersions = await api.listApprovalRuleVersions(testClient).catch(() => []);
|
||||
const testDocsByTitle = new Map(testDocs.map((d) => [d.title.trim(), d]));
|
||||
const docsToCreate = [];
|
||||
const docsToUpdate = [];
|
||||
const prodTitles = new Set(prodDocs.map((d) => d.title.trim()));
|
||||
|
||||
for (const prodDoc of prodDocs) {
|
||||
const title = prodDoc.title.trim();
|
||||
const existing = testDocsByTitle.get(title);
|
||||
if (!existing) {
|
||||
docsToCreate.push(prodDoc);
|
||||
} else {
|
||||
const contentChanged = existing.content !== prodDoc.content;
|
||||
const purposeChanged = (existing.purpose ?? "") !== (prodDoc.purpose ?? "");
|
||||
const enabledChanged = existing.enabled !== prodDoc.enabled;
|
||||
const contentTypeChanged = existing.contentType !== prodDoc.contentType;
|
||||
const statusChanged = (existing.status ?? "") !== (prodDoc.status ?? "");
|
||||
const scenariosChanged = JSON.stringify([...(existing.scenarios ?? [])].sort()) !==
|
||||
JSON.stringify([...(prodDoc.scenarios ?? [])].sort());
|
||||
const tagsChanged = JSON.stringify([...(existing.tags ?? [])].sort()) !==
|
||||
JSON.stringify([...(prodDoc.tags ?? [])].sort());
|
||||
|
||||
if (contentChanged || purposeChanged || enabledChanged || contentTypeChanged || statusChanged || scenariosChanged || tagsChanged) {
|
||||
docsToUpdate.push({
|
||||
testId: existing.id,
|
||||
prodDoc,
|
||||
reasons: [
|
||||
...(contentChanged ? ["content"] : []),
|
||||
...(purposeChanged ? ["purpose"] : []),
|
||||
...(enabledChanged ? ["enabled"] : []),
|
||||
...(contentTypeChanged ? ["contentType"] : []),
|
||||
...(statusChanged ? ["status"] : []),
|
||||
...(scenariosChanged ? ["scenarios"] : []),
|
||||
...(tagsChanged ? ["tags"] : [])
|
||||
]
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const docsToDelete = testDocs.filter(
|
||||
(td) => !prodTitles.has(td.title.trim())
|
||||
);
|
||||
|
||||
const sortedProdVersions = [...prodVersions].sort((a, b) => b.versionNumber - a.versionNumber);
|
||||
const latestProd = sortedProdVersions[0];
|
||||
@@ -324,7 +381,8 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
|
||||
latestPublishedVersion: latestProd ? {
|
||||
versionNumber: latestProd.versionNumber,
|
||||
versionName: latestProd.versionName
|
||||
} : null
|
||||
} : null,
|
||||
documentsCount: prodDocs.length
|
||||
},
|
||||
target: {
|
||||
id: testCorp.id,
|
||||
@@ -363,6 +421,28 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
|
||||
strictness: r.strictness
|
||||
}))
|
||||
},
|
||||
documentsPlan: {
|
||||
sourceDocumentCount: prodDocs.length,
|
||||
targetExistingDocumentCount: testDocs.length,
|
||||
toCreateCount: docsToCreate.length,
|
||||
toCreate: docsToCreate.map((d) => ({
|
||||
title: d.title,
|
||||
purpose: d.purpose,
|
||||
contentType: d.contentType,
|
||||
scenarios: d.scenarios
|
||||
})),
|
||||
toUpdateCount: docsToUpdate.length,
|
||||
toUpdate: docsToUpdate.map((d) => ({
|
||||
targetDocumentId: d.testId,
|
||||
title: d.prodDoc.title,
|
||||
reasons: d.reasons
|
||||
})),
|
||||
toDeleteCount: docsToDelete.length,
|
||||
toDelete: docsToDelete.map((d) => ({
|
||||
targetDocumentId: d.id,
|
||||
title: d.title
|
||||
}))
|
||||
},
|
||||
publishingPlan: {
|
||||
targetCorpId: testCorp.id,
|
||||
publishVersionName: versionName,
|
||||
@@ -379,7 +459,7 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
|
||||
const confirmed = booleanOption(args.options.yes) === true || booleanOption(args.options.confirm) === true;
|
||||
if (!confirmed) {
|
||||
throw confirmationRequired(
|
||||
`Refusing to overwrite test enterprise '${testCorp.name}' (${testCorp.id}) with preferences and ${prodRules.rules.length} draft rules from production enterprise '${prodCorp.name}' (${prodCorp.id}) without --yes. This will replace test draft rules and publish a new version on the test enterprise.`,
|
||||
`Refusing to overwrite test enterprise '${testCorp.name}' (${testCorp.id}) with preferences, ${prodRules.rules.length} draft rules, and ${prodDocs.length} knowledge documents from production enterprise '${prodCorp.name}' (${prodCorp.id}) without --yes. This will replace test draft rules, synchronize knowledge documents, and publish a new version on the test enterprise.`,
|
||||
`seal-use corps sync-test ${prodCorp.id} --yes`
|
||||
);
|
||||
}
|
||||
@@ -387,12 +467,49 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
|
||||
// 1. Update style & decision preferences in test enterprise
|
||||
await api.updateApprovalStylePreferences(testClient, updatedTestPref, testCorp.seal.endpoints.approvalStylePreferences);
|
||||
|
||||
// 2. Clear old draft rules in test enterprise
|
||||
// 2. Synchronize knowledge documents in test enterprise (full coverage, delete redundant test documents)
|
||||
const deletedDocs: Array<{ id: string; title: string }> = [];
|
||||
for (const doc of docsToDelete) {
|
||||
await api.deleteApprovalDocument(testClient, doc.id);
|
||||
deletedDocs.push({ id: doc.id, title: doc.title });
|
||||
}
|
||||
|
||||
const updatedDocs: Array<{ id: string; title: string; reasons: string[] }> = [];
|
||||
for (const item of docsToUpdate) {
|
||||
await api.updateApprovalDocument(testClient, item.testId, {
|
||||
title: item.prodDoc.title,
|
||||
purpose: item.prodDoc.purpose,
|
||||
content: item.prodDoc.content,
|
||||
contentType: item.prodDoc.contentType,
|
||||
status: item.prodDoc.status,
|
||||
enabled: item.prodDoc.enabled,
|
||||
scenarios: item.prodDoc.scenarios && item.prodDoc.scenarios.length > 0 ? item.prodDoc.scenarios : ["audit"],
|
||||
tags: item.prodDoc.tags
|
||||
});
|
||||
updatedDocs.push({ id: item.testId, title: item.prodDoc.title, reasons: item.reasons });
|
||||
}
|
||||
|
||||
const createdDocs: Array<{ id: string; title: string }> = [];
|
||||
for (const doc of docsToCreate) {
|
||||
const created = await api.createApprovalDocument(testClient, {
|
||||
title: doc.title,
|
||||
purpose: doc.purpose,
|
||||
content: doc.content,
|
||||
contentType: doc.contentType,
|
||||
status: doc.status,
|
||||
enabled: doc.enabled,
|
||||
scenarios: doc.scenarios && doc.scenarios.length > 0 ? doc.scenarios : ["audit"],
|
||||
tags: doc.tags
|
||||
});
|
||||
createdDocs.push({ id: created.id, title: created.title });
|
||||
}
|
||||
|
||||
// 3. Clear old draft rules in test enterprise
|
||||
for (const rule of testRules.rules) {
|
||||
await api.deleteApprovalRule(testClient, rule.id);
|
||||
}
|
||||
|
||||
// 3. Create new draft rules in test enterprise
|
||||
// 4. Create new draft rules in test enterprise
|
||||
const createdRules: any[] = [];
|
||||
for (const rule of prodRules.rules) {
|
||||
const created = await api.createApprovalRule(testClient, {
|
||||
@@ -403,10 +520,10 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
|
||||
createdRules.push(created);
|
||||
}
|
||||
|
||||
// 4. Batch recode rules in test enterprise
|
||||
// 5. Batch recode rules in test enterprise
|
||||
await api.recodeApprovalRules(testClient).catch(() => undefined);
|
||||
|
||||
// 5. Publish new version in test enterprise
|
||||
// 6. Publish new version in test enterprise
|
||||
const published = await api.publishApprovalRuleVersion(testClient, versionName);
|
||||
|
||||
const publishedRules = published.version.rules ?? [];
|
||||
@@ -465,7 +582,8 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
|
||||
versionNumber: latestProd.versionNumber,
|
||||
versionName: latestProd.versionName
|
||||
} : null,
|
||||
draftRulesCount: prodRules.rules.length
|
||||
draftRulesCount: prodRules.rules.length,
|
||||
documentsCount: prodDocs.length
|
||||
},
|
||||
target: {
|
||||
id: testCorp.id,
|
||||
@@ -480,6 +598,16 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
|
||||
rulesCount: published.rulesCount
|
||||
}
|
||||
},
|
||||
documents: {
|
||||
sourceCount: prodDocs.length,
|
||||
targetExistingCount: testDocs.length,
|
||||
createdCount: createdDocs.length,
|
||||
created: createdDocs,
|
||||
updatedCount: updatedDocs.length,
|
||||
updated: updatedDocs,
|
||||
deletedCount: deletedDocs.length,
|
||||
deleted: deletedDocs
|
||||
},
|
||||
versionMapping,
|
||||
ruleMapping,
|
||||
...(mappingSavedTo ? { mappingSavedTo } : {}),
|
||||
|
||||
Reference in new issue
Block a user