|
3 | 3 | isDirectBindingConfig, |
4 | 4 | isDirectCredentialsConfig, |
5 | 5 | isGatewayConfig, |
| 6 | + validateWorkersAiConfig, |
6 | 7 | type WorkersAiAdapterConfig, |
7 | 8 | } from "../src/utils/create-fetcher"; |
8 | 9 |
|
@@ -90,3 +91,71 @@ describe("config detection", () => { |
90 | 91 | expect(isGatewayConfig(config)).toBe(false); |
91 | 92 | }); |
92 | 93 | }); |
| 94 | + |
| 95 | +// --------------------------------------------------------------------------- |
| 96 | +// validateWorkersAiConfig |
| 97 | +// --------------------------------------------------------------------------- |
| 98 | + |
| 99 | +describe("validateWorkersAiConfig", () => { |
| 100 | + it("accepts plain Workers AI binding config", () => { |
| 101 | + const binding = { |
| 102 | + run: (_model: string, _inputs: Record<string, unknown>) => Promise.resolve({}), |
| 103 | + gateway: (_id: string) => ({ |
| 104 | + run: (_req: unknown) => Promise.resolve(new Response("ok")), |
| 105 | + }), |
| 106 | + }; |
| 107 | + expect(() => validateWorkersAiConfig({ binding })).not.toThrow(); |
| 108 | + }); |
| 109 | + |
| 110 | + it("accepts plain REST credentials config", () => { |
| 111 | + expect(() => validateWorkersAiConfig({ accountId: "abc", apiKey: "key" })).not.toThrow(); |
| 112 | + }); |
| 113 | + |
| 114 | + it("accepts gateway binding config", () => { |
| 115 | + const binding = { |
| 116 | + run: (_request: unknown) => Promise.resolve(new Response("ok")), |
| 117 | + }; |
| 118 | + expect(() => validateWorkersAiConfig({ binding })).not.toThrow(); |
| 119 | + }); |
| 120 | + |
| 121 | + it("accepts gateway credentials config", () => { |
| 122 | + const config = { |
| 123 | + accountId: "abc", |
| 124 | + gatewayId: "gw-1", |
| 125 | + } as WorkersAiAdapterConfig; |
| 126 | + expect(() => validateWorkersAiConfig(config)).not.toThrow(); |
| 127 | + }); |
| 128 | + |
| 129 | + it("throws for empty config", () => { |
| 130 | + expect(() => validateWorkersAiConfig({} as WorkersAiAdapterConfig)).toThrow( |
| 131 | + /Invalid Workers AI configuration/, |
| 132 | + ); |
| 133 | + }); |
| 134 | + |
| 135 | + it("throws for config with only unrelated properties", () => { |
| 136 | + expect(() => |
| 137 | + validateWorkersAiConfig({ foo: "bar" } as unknown as WorkersAiAdapterConfig), |
| 138 | + ).toThrow(/Invalid Workers AI configuration/); |
| 139 | + }); |
| 140 | + |
| 141 | + it("throws for config with only accountId (missing apiKey)", () => { |
| 142 | + expect(() => |
| 143 | + validateWorkersAiConfig({ accountId: "abc" } as unknown as WorkersAiAdapterConfig), |
| 144 | + ).toThrow(/Invalid Workers AI configuration/); |
| 145 | + }); |
| 146 | + |
| 147 | + it("throws for config with only apiKey (missing accountId)", () => { |
| 148 | + expect(() => |
| 149 | + validateWorkersAiConfig({ apiKey: "key" } as unknown as WorkersAiAdapterConfig), |
| 150 | + ).toThrow(/Invalid Workers AI configuration/); |
| 151 | + }); |
| 152 | + |
| 153 | + it("error message mentions binding and credentials", () => { |
| 154 | + try { |
| 155 | + validateWorkersAiConfig({} as WorkersAiAdapterConfig); |
| 156 | + } catch (e) { |
| 157 | + expect((e as Error).message).toContain("binding"); |
| 158 | + expect((e as Error).message).toContain("credentials"); |
| 159 | + } |
| 160 | + }); |
| 161 | +}); |
0 commit comments