mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-27 21:07:01 -06:00
test: type gateway protocol literal union guard
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user