|
2 | 2 | import fs from "node:fs/promises"; |
3 | 3 | import path from "node:path"; |
4 | 4 | import { describe, it } from "vitest"; |
| 5 | +import { ProtocolSchemas } from "./schema/protocol-schemas.js"; |
5 | 6 | import { MIN_CLIENT_PROTOCOL_VERSION, PROTOCOL_VERSION } from "./version.js"; |
6 | 7 |
|
7 | 8 | /** |
@@ -178,4 +179,37 @@ describe("native Gateway protocol levels", () => { |
178 | 179 | ); |
179 | 180 | } |
180 | 181 | }); |
| 182 | + |
| 183 | + it("emits named string-literal unions as Swift enums", async () => { |
| 184 | + const swiftGeneratedPath = |
| 185 | + "apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift"; |
| 186 | + const swiftGenerated = await readRepoFile(swiftGeneratedPath); |
| 187 | + |
| 188 | + for (const [name, schema] of Object.entries(ProtocolSchemas)) { |
| 189 | + const branches = schema.anyOf ?? schema.oneOf; |
| 190 | + if (!branches || branches.length < 2) { |
| 191 | + continue; |
| 192 | + } |
| 193 | + const values = branches.map((branch) => branch.const); |
| 194 | + if (values.some((value) => typeof value !== "string")) { |
| 195 | + continue; |
| 196 | + } |
| 197 | + |
| 198 | + const enumStart = `public enum ${name}: String, Codable, Sendable {`; |
| 199 | + const start = swiftGenerated.indexOf(enumStart); |
| 200 | + if (start < 0) { |
| 201 | + throw new Error(`${swiftGeneratedPath}: missing Swift enum for ${name}.`); |
| 202 | + } |
| 203 | + const end = swiftGenerated.indexOf("\n}\n", start); |
| 204 | + const enumSource = swiftGenerated.slice(start, end); |
| 205 | + for (const value of values) { |
| 206 | + assertPattern( |
| 207 | + enumSource, |
| 208 | + swiftGeneratedPath, |
| 209 | + new RegExp(`= ${JSON.stringify(value)}$`, "m"), |
| 210 | + `${name} must include the ${JSON.stringify(value)} literal.`, |
| 211 | + ); |
| 212 | + } |
| 213 | + } |
| 214 | + }); |
181 | 215 | }); |
0 commit comments