|
1 | 1 | // Cron protocol conformance tests cover schema compatibility for cron messages. |
2 | | -import fs from "node:fs/promises"; |
3 | | -import path from "node:path"; |
4 | | -import { expectDefined } from "@openclaw/normalization-core"; |
| 2 | +import { Value } from "typebox/value"; |
5 | 3 | import { describe, expect, it } from "vitest"; |
6 | 4 | import { |
7 | 5 | CronDeliverySchema, |
8 | 6 | CronJobStateSchema, |
9 | 7 | CronRunLogEntrySchema, |
10 | 8 | } from "../../packages/gateway-protocol/src/schema.js"; |
11 | | - |
12 | | -type SchemaLike = { |
13 | | - anyOf?: Array<SchemaLike>; |
14 | | - properties?: Record<string, unknown>; |
15 | | - const?: unknown; |
16 | | -}; |
17 | | - |
18 | | -function extractDeliveryModes(schema: SchemaLike): string[] { |
19 | | - const modeSchema = schema.properties?.mode as SchemaLike | undefined; |
20 | | - const directModes = (modeSchema?.anyOf ?? []) |
21 | | - .map((entry) => entry?.const) |
22 | | - .filter((value): value is string => typeof value === "string"); |
23 | | - if (directModes.length > 0) { |
24 | | - return directModes; |
25 | | - } |
26 | | - |
27 | | - const unionModes = (schema.anyOf ?? []) |
28 | | - .map((entry) => { |
29 | | - const mode = entry.properties?.mode as SchemaLike | undefined; |
30 | | - return mode?.const; |
31 | | - }) |
32 | | - .filter((value): value is string => typeof value === "string"); |
33 | | - |
34 | | - return Array.from(new Set(unionModes)); |
35 | | -} |
36 | | - |
37 | | -function extractConstUnionValues(schema: SchemaLike): string[] { |
38 | | - return (schema.anyOf ?? []) |
39 | | - .map((entry) => entry?.const) |
40 | | - .filter((value): value is string => typeof value === "string"); |
41 | | -} |
42 | | - |
43 | | -const UI_FILES = ["ui/src/api/types.ts", "ui/src/lib/cron/index.ts", "ui/src/pages/cron/view.ts"]; |
44 | | - |
45 | | -const MACOS_APP_SOURCES_DIR = "apps/macos/Sources/OpenClaw"; |
46 | | -const SWIFT_MODEL_CANDIDATES = [`${MACOS_APP_SOURCES_DIR}/CronModels.swift`]; |
47 | | -const SWIFT_STATUS_CANDIDATES = [`${MACOS_APP_SOURCES_DIR}/GatewayConnection.swift`]; |
48 | | - |
49 | | -async function resolveSwiftFiles(cwd: string, candidates: string[]): Promise<string[]> { |
50 | | - const matches: string[] = []; |
51 | | - for (const relPath of candidates) { |
52 | | - try { |
53 | | - await fs.access(path.join(cwd, relPath)); |
54 | | - matches.push(relPath); |
55 | | - } catch { |
56 | | - // ignore missing path |
57 | | - } |
58 | | - } |
59 | | - if (matches.length === 0) { |
60 | | - throw new Error(`Missing Swift cron definition. Tried: ${candidates.join(", ")}`); |
61 | | - } |
62 | | - return matches; |
63 | | -} |
| 9 | +import type { CronJob } from "../../packages/gateway-protocol/src/schema/cron.types.js"; |
| 10 | +import type { FailoverReason } from "../agents/embedded-agent-helpers/types.js"; |
| 11 | + |
| 12 | +type CronDelivery = NonNullable<CronJob["delivery"]>; |
| 13 | + |
| 14 | +const DELIVERY_FIXTURES = { |
| 15 | + none: { mode: "none", threadId: 42 }, |
| 16 | + announce: { |
| 17 | + mode: "announce", |
| 18 | + channel: "telegram", |
| 19 | + threadId: "topic-42", |
| 20 | + completionDestination: { mode: "webhook", to: "https://example.test/complete" }, |
| 21 | + failureDestination: { mode: "announce", channel: "last", to: "ops" }, |
| 22 | + }, |
| 23 | + webhook: { mode: "webhook", to: "https://example.test/result" }, |
| 24 | +} satisfies Record<CronDelivery["mode"], CronDelivery>; |
| 25 | + |
| 26 | +const FAILOVER_REASONS = Object.keys({ |
| 27 | + auth: true, |
| 28 | + auth_permanent: true, |
| 29 | + format: true, |
| 30 | + rate_limit: true, |
| 31 | + overloaded: true, |
| 32 | + billing: true, |
| 33 | + server_error: true, |
| 34 | + timeout: true, |
| 35 | + tls_certificate: true, |
| 36 | + context_overflow: true, |
| 37 | + model_not_found: true, |
| 38 | + session_expired: true, |
| 39 | + empty_response: true, |
| 40 | + no_error_details: true, |
| 41 | + unclassified: true, |
| 42 | + unknown: true, |
| 43 | +} satisfies Record<FailoverReason, true>) as FailoverReason[]; |
64 | 44 |
|
65 | 45 | describe("cron protocol conformance", () => { |
66 | | - it("ui + swift include all cron delivery modes from gateway schema", async () => { |
67 | | - const modes = extractDeliveryModes(CronDeliverySchema as SchemaLike); |
68 | | - expect(modes.length).toBeGreaterThan(0); |
69 | | - |
70 | | - const cwd = process.cwd(); |
71 | | - for (const relPath of UI_FILES) { |
72 | | - const content = await fs.readFile(path.join(cwd, relPath), "utf-8"); |
73 | | - for (const mode of modes) { |
74 | | - expect(content, `${relPath} missing delivery mode ${mode}`).toContain(`"${mode}"`); |
75 | | - } |
76 | | - } |
77 | | - |
78 | | - const swiftModelFiles = await resolveSwiftFiles(cwd, SWIFT_MODEL_CANDIDATES); |
79 | | - for (const relPath of swiftModelFiles) { |
80 | | - const content = await fs.readFile(path.join(cwd, relPath), "utf-8"); |
81 | | - for (const mode of modes) { |
82 | | - const pattern = new RegExp(`\\bcase\\s+${mode}\\b`); |
83 | | - expect(content, `${relPath} missing case ${mode}`).toMatch(pattern); |
84 | | - } |
| 46 | + it("accepts every typed delivery mode and rejects unknown modes", () => { |
| 47 | + for (const delivery of Object.values(DELIVERY_FIXTURES)) { |
| 48 | + expect(Value.Check(CronDeliverySchema, delivery)).toBe(true); |
85 | 49 | } |
| 50 | + expect(Value.Check(CronDeliverySchema, { mode: "email" })).toBe(false); |
86 | 51 | }); |
87 | 52 |
|
88 | | - it("cron status shape matches gateway fields in UI + Swift", async () => { |
89 | | - const cwd = process.cwd(); |
90 | | - const uiTypes = await fs.readFile(path.join(cwd, "ui/src/api/types.ts"), "utf-8"); |
91 | | - expect(uiTypes).toContain("export type CronStatus"); |
92 | | - expect(uiTypes).toContain("jobs:"); |
93 | | - expect(uiTypes).not.toContain("jobCount"); |
94 | | - |
95 | | - const [swiftRelPath] = await resolveSwiftFiles(cwd, SWIFT_STATUS_CANDIDATES); |
96 | | - const swiftPath = path.join(cwd, expectDefined(swiftRelPath, "swiftRelPath test invariant")); |
97 | | - const swift = await fs.readFile(swiftPath, "utf-8"); |
98 | | - expect(swift).toContain("struct CronSchedulerStatus"); |
99 | | - expect(swift).toContain("let jobs:"); |
100 | | - }); |
101 | | - |
102 | | - it("cron public schemas keep the full failover reason set", () => { |
103 | | - const expectedReasons = [ |
104 | | - "auth", |
105 | | - "auth_permanent", |
106 | | - "format", |
107 | | - "rate_limit", |
108 | | - "overloaded", |
109 | | - "billing", |
110 | | - "server_error", |
111 | | - "timeout", |
112 | | - "context_overflow", |
113 | | - "model_not_found", |
114 | | - "session_expired", |
115 | | - "empty_response", |
116 | | - "no_error_details", |
117 | | - "unclassified", |
118 | | - "unknown", |
119 | | - ]; |
120 | | - |
121 | | - const stateProperties = (CronJobStateSchema as SchemaLike).properties ?? {}; |
122 | | - const lastErrorReason = stateProperties.lastErrorReason as SchemaLike | undefined; |
123 | | - expect(lastErrorReason).toBeDefined(); |
124 | | - expect(extractConstUnionValues(lastErrorReason ?? {})).toEqual(expectedReasons); |
| 53 | + it("accepts every core failover reason in state and run-history schemas", () => { |
| 54 | + for (const reason of FAILOVER_REASONS) { |
| 55 | + expect(Value.Check(CronJobStateSchema, { lastErrorReason: reason })).toBe(true); |
| 56 | + expect( |
| 57 | + Value.Check(CronRunLogEntrySchema, { |
| 58 | + ts: 1, |
| 59 | + jobId: "job-1", |
| 60 | + action: "finished", |
| 61 | + errorReason: reason, |
| 62 | + }), |
| 63 | + ).toBe(true); |
| 64 | + } |
125 | 65 |
|
126 | | - const runLogProperties = (CronRunLogEntrySchema as SchemaLike).properties ?? {}; |
127 | | - const errorReason = runLogProperties.errorReason as SchemaLike | undefined; |
128 | | - expect(errorReason).toBeDefined(); |
129 | | - expect(extractConstUnionValues(errorReason ?? {})).toEqual(expectedReasons); |
| 66 | + expect(Value.Check(CronJobStateSchema, { lastErrorReason: "not-a-reason" })).toBe(false); |
| 67 | + expect( |
| 68 | + Value.Check(CronRunLogEntrySchema, { |
| 69 | + ts: 1, |
| 70 | + jobId: "job-1", |
| 71 | + action: "finished", |
| 72 | + errorReason: "not-a-reason", |
| 73 | + }), |
| 74 | + ).toBe(false); |
130 | 75 | }); |
131 | 76 | }); |
0 commit comments