test: type gateway protocol literal union guard

This commit is contained in:
Tideclaw
2026-06-15 10:14:40 +00:00
parent 122c26f4fd
commit c33834e5a7
@@ -19,6 +19,10 @@ type ProtocolLevels = {
max: number;
};
type LiteralUnionBranch = {
readonly const?: unknown;
};
const expectedLevels: ProtocolLevels = {
min: MIN_CLIENT_PROTOCOL_VERSION,
max: PROTOCOL_VERSION,
@@ -68,6 +72,25 @@ function assertPattern(
throw new Error(`${relativePath}: ${message}`);
}
function literalUnionBranches(schema: unknown): readonly LiteralUnionBranch[] | undefined {
if (!schema || typeof schema !== "object") {
return undefined;
}
const unionSchema = schema as {
readonly anyOf?: unknown;
readonly oneOf?: unknown;
};
const branches = Array.isArray(unionSchema.anyOf)
? unionSchema.anyOf
: Array.isArray(unionSchema.oneOf)
? unionSchema.oneOf
: undefined;
if (!branches || branches.length < 2) {
return undefined;
}
return branches as readonly LiteralUnionBranch[];
}
describe("native Gateway protocol levels", () => {
it("match the TypeScript source of truth", async () => {
if (MIN_CLIENT_PROTOCOL_VERSION > PROTOCOL_VERSION) {
@@ -186,8 +209,8 @@ describe("native Gateway protocol levels", () => {
const swiftGenerated = await readRepoFile(swiftGeneratedPath);
for (const [name, schema] of Object.entries(ProtocolSchemas)) {
const branches = schema.anyOf ?? schema.oneOf;
if (!branches || branches.length < 2) {
const branches = literalUnionBranches(schema);
if (!branches) {
continue;
}
const values = branches.map((branch) => branch.const);