Skip to content

Commit 9a7c600

Browse files
test: guard sparsekernel mapping uniqueness
1 parent a8164a1 commit 9a7c600

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

scripts/check-sparsekernel-openapi.mjs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ export function checkSparseKernelOpenApi({ openapiText, daemonSource, clientSour
198198
const clientPaths = collectClientPaths(clientSource);
199199
const clientRouteKeys = collectClientRouteKeys(clientSource);
200200
const openapiRouteKeys = collectOpenApiRouteKeys(paths);
201+
checkClientSchemaMappingUniqueness(errors, CLIENT_SCHEMA_MAPPINGS);
201202

202203
pushSetDiff(
203204
errors,
@@ -574,6 +575,48 @@ function checkClientSchemaProperties(errors, clientSource, schemas) {
574575
}
575576
}
576577

578+
function checkClientSchemaMappingUniqueness(errors, mappings) {
579+
const { duplicateClientTypes, duplicateSchemaNames } =
580+
collectClientSchemaMappingProblems(mappings);
581+
if (duplicateClientTypes.length > 0) {
582+
errors.push(
583+
formatList(
584+
"SparseKernel client parity mappings duplicate client types",
585+
duplicateClientTypes,
586+
),
587+
);
588+
}
589+
if (duplicateSchemaNames.length > 0) {
590+
errors.push(
591+
formatList(
592+
"SparseKernel client parity mappings duplicate schema names",
593+
duplicateSchemaNames,
594+
),
595+
);
596+
}
597+
}
598+
599+
export function collectClientSchemaMappingProblems(mappings) {
600+
return {
601+
duplicateClientTypes: collectDuplicateMappingValues(mappings, "clientType"),
602+
duplicateSchemaNames: collectDuplicateMappingValues(mappings, "schemaName"),
603+
};
604+
}
605+
606+
function collectDuplicateMappingValues(mappings, key) {
607+
const firstSeen = new Set();
608+
const duplicates = new Set();
609+
for (const item of mappings) {
610+
const value = item[key];
611+
if (firstSeen.has(value)) {
612+
duplicates.add(value);
613+
continue;
614+
}
615+
firstSeen.add(value);
616+
}
617+
return [...duplicates].toSorted(compareStrings);
618+
}
619+
577620
function schemaPropertiesFor(schemas, schemaName) {
578621
const properties = schemas?.[schemaName]?.properties;
579622
if (!properties || typeof properties !== "object" || Array.isArray(properties)) {

test/scripts/check-sparsekernel-openapi.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from "vitest";
22
import {
3+
collectClientSchemaMappingProblems,
34
collectOpenApiInlineArrayResponseItemRoutes,
45
collectOpenApiInlineObjectResponseSchemaRoutes,
56
collectOpenApiInlineRequestBodyRoutes,
@@ -10,6 +11,20 @@ import {
1011
} from "../../scripts/check-sparsekernel-openapi.mjs";
1112

1213
describe("scripts/check-sparsekernel-openapi", () => {
14+
it("finds duplicate client parity mappings", () => {
15+
expect(
16+
collectClientSchemaMappingProblems([
17+
{ clientType: "SparseKernelTask", schemaName: "Task" },
18+
{ clientType: "SparseKernelSession", schemaName: "Session" },
19+
{ clientType: "SparseKernelTask", schemaName: "TaskInput" },
20+
{ clientType: "SparseKernelTaskInput", schemaName: "Task" },
21+
]),
22+
).toEqual({
23+
duplicateClientTypes: ["SparseKernelTask"],
24+
duplicateSchemaNames: ["Task"],
25+
});
26+
});
27+
1328
it("collects component-backed request body schema names", () => {
1429
const paths = {
1530
"/tasks/enqueue": {

0 commit comments

Comments
 (0)