






















@@ -0,0 +1,136 @@
1+import fs from "node:fs/promises";
2+import path from "node:path";
3+import { describe, it } from "vitest";
4+import { MIN_CLIENT_PROTOCOL_VERSION, PROTOCOL_VERSION } from "./version.js";
5+6+type ProtocolLevels = {
7+min: number;
8+max: number;
9+};
10+11+const expectedLevels: ProtocolLevels = {
12+min: MIN_CLIENT_PROTOCOL_VERSION,
13+max: PROTOCOL_VERSION,
14+};
15+16+async function readRepoFile(relativePath: string): Promise<string> {
17+return fs.readFile(path.join(process.cwd(), relativePath), "utf8");
18+}
19+20+function extractInteger(
21+content: string,
22+pattern: RegExp,
23+relativePath: string,
24+label: string,
25+): number {
26+const match = pattern.exec(content);
27+if (!match) {
28+throw new Error(
29+`${relativePath}: missing ${label}; keep native Gateway protocol levels in sync with src/gateway/protocol/version.ts.`,
30+);
31+}
32+return Number.parseInt(match[1], 10);
33+}
34+35+function assertLevelsMatch(relativePath: string, actual: ProtocolLevels): void {
36+if (actual.min === expectedLevels.min && actual.max === expectedLevels.max) {
37+return;
38+}
39+throw new Error(
40+`${relativePath}: Gateway protocol level mismatch: expected min=${expectedLevels.min} max=${expectedLevels.max} from src/gateway/protocol/version.ts, got min=${actual.min} max=${actual.max}. Update the native constants/generated artifacts before shipping.`,
41+);
42+}
43+44+function assertPattern(
45+content: string,
46+relativePath: string,
47+pattern: RegExp,
48+message: string,
49+): void {
50+if (pattern.test(content)) {
51+return;
52+}
53+throw new Error(`${relativePath}: ${message}`);
54+}
55+56+describe("native Gateway protocol levels", () => {
57+it("match the TypeScript source of truth", async () => {
58+if (MIN_CLIENT_PROTOCOL_VERSION > PROTOCOL_VERSION) {
59+throw new Error(
60+`src/gateway/protocol/version.ts: MIN_CLIENT_PROTOCOL_VERSION (${MIN_CLIENT_PROTOCOL_VERSION}) must not exceed PROTOCOL_VERSION (${PROTOCOL_VERSION}).`,
61+);
62+}
63+64+const swiftGeneratedPath =
65+"apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift";
66+const swiftGenerated = await readRepoFile(swiftGeneratedPath);
67+assertLevelsMatch(swiftGeneratedPath, {
68+min: extractInteger(
69+swiftGenerated,
70+/public let GATEWAY_MIN_PROTOCOL_VERSION = (\d+)/,
71+swiftGeneratedPath,
72+"GATEWAY_MIN_PROTOCOL_VERSION",
73+),
74+max: extractInteger(
75+swiftGenerated,
76+/public let GATEWAY_PROTOCOL_VERSION = (\d+)/,
77+swiftGeneratedPath,
78+"GATEWAY_PROTOCOL_VERSION",
79+),
80+});
81+82+const androidPath = "apps/android/app/src/main/java/ai/openclaw/app/gateway/GatewayProtocol.kt";
83+const android = await readRepoFile(androidPath);
84+assertLevelsMatch(androidPath, {
85+min: extractInteger(
86+android,
87+/const val GATEWAY_MIN_PROTOCOL_VERSION = (\d+)/,
88+androidPath,
89+"GATEWAY_MIN_PROTOCOL_VERSION",
90+),
91+max: extractInteger(
92+android,
93+/const val GATEWAY_PROTOCOL_VERSION = (\d+)/,
94+androidPath,
95+"GATEWAY_PROTOCOL_VERSION",
96+),
97+});
98+});
99+100+it("uses the min constant for native connect compatibility ranges", async () => {
101+const swiftConnectFiles = [
102+"apps/shared/OpenClawKit/Sources/OpenClawKit/GatewayChannel.swift",
103+"apps/macos/Sources/OpenClawMacCLI/WizardCommand.swift",
104+];
105+for (const relativePath of swiftConnectFiles) {
106+const content = await readRepoFile(relativePath);
107+assertPattern(
108+content,
109+relativePath,
110+/"minProtocol": ProtoAnyCodable\(GATEWAY_MIN_PROTOCOL_VERSION\)/,
111+"connect params must advertise GATEWAY_MIN_PROTOCOL_VERSION as minProtocol.",
112+);
113+assertPattern(
114+content,
115+relativePath,
116+/"maxProtocol": ProtoAnyCodable\(GATEWAY_PROTOCOL_VERSION\)/,
117+"connect params must advertise GATEWAY_PROTOCOL_VERSION as maxProtocol.",
118+);
119+}
120+121+const androidPath = "apps/android/app/src/main/java/ai/openclaw/app/gateway/GatewaySession.kt";
122+const android = await readRepoFile(androidPath);
123+assertPattern(
124+android,
125+androidPath,
126+/put\("minProtocol", JsonPrimitive\(GATEWAY_MIN_PROTOCOL_VERSION\)\)/,
127+"connect params must advertise GATEWAY_MIN_PROTOCOL_VERSION as minProtocol.",
128+);
129+assertPattern(
130+android,
131+androidPath,
132+/put\("maxProtocol", JsonPrimitive\(GATEWAY_PROTOCOL_VERSION\)\)/,
133+"connect params must advertise GATEWAY_PROTOCOL_VERSION as maxProtocol.",
134+);
135+});
136+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。