@mycadet/cli 0.13.0-dev.202611171301.b3e2e5c → 0.13.0-dev.202611182119.bb60b70
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/node_modules/@mycadet/assistant-api/node_modules/@mycadet/service-contracts/package.json +1 -1
- package/node_modules/@mycadet/avatar-manifest/package.json +1 -1
- package/node_modules/@mycadet/ipc-server-utils/package.json +1 -1
- package/node_modules/@mycadet/local-mode/node_modules/@mycadet/avatar-manifest/package.json +1 -1
- package/node_modules/@mycadet/local-mode/node_modules/@mycadet/ipc-server-utils/package.json +1 -1
- package/node_modules/@mycadet/local-mode/node_modules/@mycadet/service-contracts/package.json +1 -1
- package/node_modules/@mycadet/local-mode/node_modules/@mycadet/shared/eslint/base.mjs +57 -0
- package/node_modules/@mycadet/local-mode/node_modules/@mycadet/shared/fetch.ts +11 -4
- package/node_modules/@mycadet/local-mode/node_modules/@mycadet/shared/package.json +2 -1
- package/node_modules/@mycadet/local-mode/package.json +1 -1
- package/node_modules/@mycadet/local-mode/src/__tests__/helpers/memory-cookie-jar.ts +23 -0
- package/node_modules/@mycadet/local-mode/src/__tests__/operations-cloud-forward.test.ts +485 -0
- package/node_modules/@mycadet/local-mode/src/__tests__/operations-doctor.test.ts +28 -16
- package/node_modules/@mycadet/local-mode/src/__tests__/operations-migrations.test.ts +11 -12
- package/node_modules/@mycadet/local-mode/src/__tests__/operations-profile.test.ts +41 -29
- package/node_modules/@mycadet/local-mode/src/__tests__/operations-proxy.test.ts +96 -28
- package/node_modules/@mycadet/local-mode/src/__tests__/operations-resolution.test.ts +72 -45
- package/node_modules/@mycadet/local-mode/src/__tests__/operations-retire.test.ts +43 -38
- package/node_modules/@mycadet/local-mode/src/__tests__/operations-routes.test.ts +178 -59
- package/node_modules/@mycadet/local-mode/src/__tests__/operations-sleep.test.ts +39 -50
- package/node_modules/@mycadet/local-mode/src/__tests__/operations-teleport.test.ts +22 -11
- package/node_modules/@mycadet/local-mode/src/__tests__/operations-wake.test.ts +32 -32
- package/node_modules/@mycadet/local-mode/src/index.ts +2 -1
- package/node_modules/@mycadet/local-mode/src/operations/cloud-forward.ts +166 -0
- package/node_modules/@mycadet/local-mode/src/operations/cookie-jar.ts +102 -0
- package/node_modules/@mycadet/local-mode/src/operations/doctor.ts +7 -9
- package/node_modules/@mycadet/local-mode/src/operations/profile.ts +1 -1
- package/node_modules/@mycadet/local-mode/src/operations/resolve.ts +2 -2
- package/node_modules/@mycadet/local-mode/src/operations/routes.ts +174 -80
- package/node_modules/@mycadet/local-mode/src/served-config.test.ts +0 -2
- package/node_modules/@mycadet/local-mode/src/served-config.ts +0 -1
- package/node_modules/@mycadet/service-contracts/package.json +1 -1
- package/node_modules/@mycadet/shared/eslint/base.mjs +57 -0
- package/node_modules/@mycadet/shared/fetch.ts +11 -4
- package/node_modules/@mycadet/shared/package.json +2 -1
- package/package.json +1 -1
- package/src/__tests__/web-dist.test.ts +54 -1
- package/src/lib/web-dist.ts +28 -6
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { defineConfig, globalIgnores } from "eslint/config";
|
|
3
|
+
import tseslint from "typescript-eslint";
|
|
4
|
+
|
|
5
|
+
import { importsFirst } from "./imports-first.mjs";
|
|
6
|
+
import { noEmptyImports } from "./no-empty-imports.mjs";
|
|
7
|
+
import { noReExports } from "./no-re-exports.mjs";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The `@mycadet` plugin, wired once. Every shared custom rule registers
|
|
11
|
+
* here; a service opts in by naming the rule (`@mycadet/imports-first`)
|
|
12
|
+
* rather than re-assembling the plugin — flat config would overwrite a
|
|
13
|
+
* same-named plugin definition wholesale, so one shared object identity
|
|
14
|
+
* is what keeps adoption declarative.
|
|
15
|
+
*/
|
|
16
|
+
export const mycadetPlugin = {
|
|
17
|
+
rules: {
|
|
18
|
+
"imports-first": importsFirst,
|
|
19
|
+
"no-empty-imports": noEmptyImports,
|
|
20
|
+
"no-re-exports": noReExports,
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The repo's lint floor: the rules every service already enforces,
|
|
26
|
+
* declared once. Spread it first and add only what's yours — later
|
|
27
|
+
* config items win.
|
|
28
|
+
*
|
|
29
|
+
* Re-export shims are outlawed everywhere: consumers import from the
|
|
30
|
+
* defining module, and only index files aggregate a public surface.
|
|
31
|
+
* Warning-level countdowns (cast bans, mock.module, namespace imports)
|
|
32
|
+
* stay per-service — their pins are local.
|
|
33
|
+
*
|
|
34
|
+
* @param {{ ignores?: string[] }} [options]
|
|
35
|
+
* Extra global ignores for service build output (e.g. `drizzle/**`).
|
|
36
|
+
* `dist/**` is always ignored.
|
|
37
|
+
*/
|
|
38
|
+
export function baseEslintConfig(options = {}) {
|
|
39
|
+
const { ignores = [] } = options;
|
|
40
|
+
return defineConfig([
|
|
41
|
+
...tseslint.configs.recommended,
|
|
42
|
+
globalIgnores(["dist/**", ...ignores]),
|
|
43
|
+
{
|
|
44
|
+
plugins: { "@mycadet": mycadetPlugin },
|
|
45
|
+
rules: {
|
|
46
|
+
// Unused symbols are noise the compiler can't see (params kept
|
|
47
|
+
// for signature shape, type-only imports); an underscore-prefixed
|
|
48
|
+
// name declares the intent to keep.
|
|
49
|
+
"@typescript-eslint/no-unused-vars": [
|
|
50
|
+
"error",
|
|
51
|
+
{ argsIgnorePattern: "^_", varsIgnorePattern: "^_" },
|
|
52
|
+
],
|
|
53
|
+
"@mycadet/no-re-exports": "error",
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
]);
|
|
57
|
+
}
|
|
@@ -6,7 +6,14 @@
|
|
|
6
6
|
* the caller). Every injectable fetch stand-in and fetcher parameter takes
|
|
7
7
|
* THIS type.
|
|
8
8
|
*/
|
|
9
|
-
export type Fetch = (
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
export type Fetch = (input: string, init?: RequestInit) => Promise<Response>;
|
|
10
|
+
|
|
11
|
+
/** The methods the operator surface speaks — route rows and requests both. */
|
|
12
|
+
export type HTTPMethod =
|
|
13
|
+
| "GET"
|
|
14
|
+
| "POST"
|
|
15
|
+
| "PUT"
|
|
16
|
+
| "PATCH"
|
|
17
|
+
| "DELETE"
|
|
18
|
+
| "HEAD"
|
|
19
|
+
| "OPTIONS";
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mycadet/shared",
|
|
3
|
-
"version": "0.13.0-dev.
|
|
3
|
+
"version": "0.13.0-dev.202611182119.bb60b70",
|
|
4
4
|
"private": true,
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./index.ts",
|
|
7
7
|
"./eslint/imports-first": "./eslint/imports-first.mjs",
|
|
8
|
+
"./eslint/base": "./eslint/base.mjs",
|
|
8
9
|
"./eslint/no-empty-imports": "./eslint/no-empty-imports.mjs",
|
|
9
10
|
"./eslint/no-re-exports": "./eslint/no-re-exports.mjs",
|
|
10
11
|
"./fetch": "./fetch.ts",
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { CookieJar, OriginCookie } from "../../operations/cookie-jar";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A Map-backed `CookieJar` fake: cookies keyed by origin URL, a re-set of
|
|
5
|
+
* a held name replacing it like a real store. The route-table tests seed
|
|
6
|
+
* `store` directly to arrange a jar state, then read it to assert what a
|
|
7
|
+
* row stored.
|
|
8
|
+
*/
|
|
9
|
+
export class MemoryCookieJar implements CookieJar {
|
|
10
|
+
readonly store = new Map<string, OriginCookie[]>();
|
|
11
|
+
|
|
12
|
+
async get(url: string): Promise<OriginCookie[]> {
|
|
13
|
+
return [...(this.store.get(url) ?? [])];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async set(url: string, cookie: OriginCookie): Promise<void> {
|
|
17
|
+
const list = this.store.get(url) ?? [];
|
|
18
|
+
const existing = list.findIndex((held) => held.name === cookie.name);
|
|
19
|
+
if (existing >= 0) list[existing] = cookie;
|
|
20
|
+
else list.push(cookie);
|
|
21
|
+
this.store.set(url, list);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { forwardOperatorRequest } from "../operations/routes";
|
|
4
|
+
import {
|
|
5
|
+
buildProxyNetworkErrorResponse,
|
|
6
|
+
forwardCloudRequest,
|
|
7
|
+
isTransientNetworkError,
|
|
8
|
+
PROXY_ERROR_HEADER,
|
|
9
|
+
PROXY_NETWORK_ERROR_CODE,
|
|
10
|
+
} from "../operations/cloud-forward";
|
|
11
|
+
import type { Fetch } from "@mycadet/shared/fetch";
|
|
12
|
+
|
|
13
|
+
const CLOUD = "https://cloud.mycadet.ai";
|
|
14
|
+
|
|
15
|
+
const request = (
|
|
16
|
+
pathname: string,
|
|
17
|
+
init: {
|
|
18
|
+
method?: string;
|
|
19
|
+
origin?: string;
|
|
20
|
+
headers?: Record<string, string>;
|
|
21
|
+
} = {},
|
|
22
|
+
) =>
|
|
23
|
+
new Request(`app://mycadet.ai${pathname}`, {
|
|
24
|
+
method: init.method ?? "GET",
|
|
25
|
+
headers: new Headers({
|
|
26
|
+
...(init.origin !== undefined ? { origin: init.origin } : {}),
|
|
27
|
+
...init.headers,
|
|
28
|
+
}),
|
|
29
|
+
body: null,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
/** Fake fetch recording what the arm would have sent. */
|
|
33
|
+
const recorder = () => {
|
|
34
|
+
const calls: Array<{ url: string; init: RequestInit | undefined }> = [];
|
|
35
|
+
const fetcher: Fetch = async (url, init) => {
|
|
36
|
+
calls.push({ url, init });
|
|
37
|
+
return new Response("forwarded", { status: 200 });
|
|
38
|
+
};
|
|
39
|
+
return { calls, fetcher };
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const header = (init: RequestInit | undefined, name: string) =>
|
|
43
|
+
new Headers(init?.headers).get(name);
|
|
44
|
+
|
|
45
|
+
/** Dispatches through the real dispatcher with the arm's dial stubbed. */
|
|
46
|
+
const dispatch = (req: Request, fetcher?: Fetch): Promise<Response | null> =>
|
|
47
|
+
forwardOperatorRequest(req, { fetch: fetcher });
|
|
48
|
+
|
|
49
|
+
// The arm resolves its target through getCloudBaseUrl at request time —
|
|
50
|
+
// pin it to the cloud base for every test (the afterEach restores the
|
|
51
|
+
// ambient env; the staging/local cases overwrite it explicitly).
|
|
52
|
+
const SAVED_CLOUD_URL = process.env.CADET_CLOUD_URL;
|
|
53
|
+
const setPlatform = (): void => {
|
|
54
|
+
process.env.CADET_CLOUD_URL = CLOUD;
|
|
55
|
+
};
|
|
56
|
+
beforeEach(setPlatform);
|
|
57
|
+
afterEach(() => {
|
|
58
|
+
if (SAVED_CLOUD_URL === undefined) {
|
|
59
|
+
delete process.env.CADET_CLOUD_URL;
|
|
60
|
+
} else {
|
|
61
|
+
process.env.CADET_CLOUD_URL = SAVED_CLOUD_URL;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe("the operator's cloud arm", () => {
|
|
66
|
+
test("passes non-cloud requests through to the host chain", async () => {
|
|
67
|
+
const { calls, fetcher } = recorder();
|
|
68
|
+
expect(
|
|
69
|
+
await dispatch(request("/assistant/assets/app.js"), fetcher),
|
|
70
|
+
).toBeNull();
|
|
71
|
+
expect(calls).toHaveLength(0);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("passes gateway requests through", async () => {
|
|
75
|
+
const { calls, fetcher } = recorder();
|
|
76
|
+
expect(
|
|
77
|
+
await dispatch(request("/__gateway/8080/v1/foo"), fetcher),
|
|
78
|
+
).toBeNull();
|
|
79
|
+
expect(calls).toHaveLength(0);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test("does not forward paths that merely start with a prefix string", async () => {
|
|
83
|
+
const { calls, fetcher } = recorder();
|
|
84
|
+
expect(await dispatch(request("/_allauthx/none"), fetcher)).toBeNull();
|
|
85
|
+
expect(await dispatch(request("/accountsettings"), fetcher)).toBeNull();
|
|
86
|
+
expect(calls).toHaveLength(0);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("forwards the releases catalog; the bare /v1 rides the host chain", async () => {
|
|
90
|
+
const { calls, fetcher } = recorder();
|
|
91
|
+
// The catalog is the cloud's own read — the caller's auth rides the
|
|
92
|
+
// forward. The bare /v1 is nobody's path here; the host chain answers.
|
|
93
|
+
expect(await dispatch(request("/v1/releases/"), fetcher)).not.toBeNull();
|
|
94
|
+
expect(await dispatch(request("/v1"), fetcher)).toBeNull();
|
|
95
|
+
expect(calls).toHaveLength(1);
|
|
96
|
+
expect(calls[0]?.url).toBe("https://cloud.mycadet.ai/v1/releases/");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("a scoped request for an unknown id answers 404 from the dispatcher", async () => {
|
|
100
|
+
const { calls, fetcher } = recorder();
|
|
101
|
+
// The catch-all matches the stale id, but the dispatcher refuses to
|
|
102
|
+
// pass an id it cannot resolve — no row run, no arm dial.
|
|
103
|
+
const response = await dispatch(
|
|
104
|
+
request("/v1/assistants/ghost/conversations"),
|
|
105
|
+
fetcher,
|
|
106
|
+
);
|
|
107
|
+
expect(response?.status).toBe(404);
|
|
108
|
+
expect(calls).toHaveLength(0);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("forwards /_allauth/* requests to the cloud", async () => {
|
|
112
|
+
const { calls, fetcher } = recorder();
|
|
113
|
+
const response = await dispatch(
|
|
114
|
+
request("/_allauth/browser/v1/auth/session"),
|
|
115
|
+
fetcher,
|
|
116
|
+
);
|
|
117
|
+
expect(response?.status).toBe(200);
|
|
118
|
+
expect(calls[0]?.url).toBe(
|
|
119
|
+
"https://cloud.mycadet.ai/_allauth/browser/v1/auth/session",
|
|
120
|
+
);
|
|
121
|
+
expect(calls[0]?.init?.method).toBe("GET");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("forwards /accounts/* requests to the cloud", async () => {
|
|
125
|
+
const { calls, fetcher } = recorder();
|
|
126
|
+
await dispatch(request("/accounts/login"), fetcher);
|
|
127
|
+
expect(calls[0]?.url).toBe("https://cloud.mycadet.ai/accounts/login");
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("forwards the exact account prefixes without a trailing slash", async () => {
|
|
131
|
+
const { calls, fetcher } = recorder();
|
|
132
|
+
await dispatch(request("/_allauth"), fetcher);
|
|
133
|
+
expect(calls[0]?.url).toBe("https://cloud.mycadet.ai/_allauth");
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("preserves query string on forwarded requests", async () => {
|
|
137
|
+
const { calls, fetcher } = recorder();
|
|
138
|
+
await dispatch(request("/accounts/login?next=/dashboard"), fetcher);
|
|
139
|
+
expect(calls[0]?.url).toBe(
|
|
140
|
+
"https://cloud.mycadet.ai/accounts/login?next=/dashboard",
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("rewrites Origin from app:// to the cloud origin", async () => {
|
|
145
|
+
const { calls, fetcher } = recorder();
|
|
146
|
+
await dispatch(
|
|
147
|
+
request("/_allauth/browser/v1/auth/session", {
|
|
148
|
+
method: "POST",
|
|
149
|
+
origin: "app://mycadet.ai",
|
|
150
|
+
}),
|
|
151
|
+
fetcher,
|
|
152
|
+
);
|
|
153
|
+
expect(header(calls[0]?.init, "origin")).toBe("https://cloud.mycadet.ai");
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("sets a cloud Origin even when the renderer sent none", async () => {
|
|
157
|
+
const { calls, fetcher } = recorder();
|
|
158
|
+
await dispatch(request("/_allauth/browser/v1/config"), fetcher);
|
|
159
|
+
expect(header(calls[0]?.init, "origin")).toBe("https://cloud.mycadet.ai");
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test("forwards with manual redirects and the ambient credentials mode", async () => {
|
|
163
|
+
const { calls, fetcher } = recorder();
|
|
164
|
+
await dispatch(request("/_allauth/browser/v1/config"), fetcher);
|
|
165
|
+
expect(calls[0]?.init?.redirect).toBe("manual");
|
|
166
|
+
expect(calls[0]?.init?.credentials).toBeUndefined();
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test("forwards unsafe requests from the trusted app origin", async () => {
|
|
170
|
+
const { calls, fetcher } = recorder();
|
|
171
|
+
const response = await dispatch(
|
|
172
|
+
request("/_allauth/browser/v1/auth/session", {
|
|
173
|
+
method: "POST",
|
|
174
|
+
origin: "app://mycadet.ai",
|
|
175
|
+
}),
|
|
176
|
+
fetcher,
|
|
177
|
+
);
|
|
178
|
+
expect(response?.status).toBe(200);
|
|
179
|
+
expect(header(calls[0]?.init, "origin")).toBe("https://cloud.mycadet.ai");
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("rejects cloud requests with a foreign Origin", async () => {
|
|
183
|
+
const { calls, fetcher } = recorder();
|
|
184
|
+
const response = await dispatch(
|
|
185
|
+
request("/_allauth/browser/v1/auth/session", {
|
|
186
|
+
method: "POST",
|
|
187
|
+
origin: "https://evil.example",
|
|
188
|
+
}),
|
|
189
|
+
fetcher,
|
|
190
|
+
);
|
|
191
|
+
expect(response?.status).toBe(403);
|
|
192
|
+
expect(await response?.text()).toBe("Forbidden cloud proxy request");
|
|
193
|
+
expect(calls).toHaveLength(0);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("rejects cloud requests with a foreign Referer when Origin is absent", async () => {
|
|
197
|
+
const { calls, fetcher } = recorder();
|
|
198
|
+
const response = await dispatch(
|
|
199
|
+
request("/_allauth/browser/v1/auth/session", {
|
|
200
|
+
method: "POST",
|
|
201
|
+
headers: { referer: "https://evil.example/form" },
|
|
202
|
+
}),
|
|
203
|
+
fetcher,
|
|
204
|
+
);
|
|
205
|
+
expect(response?.status).toBe(403);
|
|
206
|
+
expect(calls).toHaveLength(0);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test("trusts source-less unsafe requests with Sec-Fetch-Site same-origin", async () => {
|
|
210
|
+
const { calls, fetcher } = recorder();
|
|
211
|
+
const response = await dispatch(
|
|
212
|
+
request("/_allauth/app/v1/auth/sessions", {
|
|
213
|
+
method: "DELETE",
|
|
214
|
+
headers: { "sec-fetch-site": "same-origin" },
|
|
215
|
+
}),
|
|
216
|
+
fetcher,
|
|
217
|
+
);
|
|
218
|
+
expect(response?.status).toBe(200);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test("rejects source-less unsafe requests with cross-site fetch metadata", async () => {
|
|
222
|
+
const { calls, fetcher } = recorder();
|
|
223
|
+
const response = await dispatch(
|
|
224
|
+
request("/_allauth/app/v1/auth/sessions", {
|
|
225
|
+
method: "DELETE",
|
|
226
|
+
headers: { "sec-fetch-site": "cross-site" },
|
|
227
|
+
}),
|
|
228
|
+
fetcher,
|
|
229
|
+
);
|
|
230
|
+
expect(response?.status).toBe(403);
|
|
231
|
+
expect(calls).toHaveLength(0);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
test("rejects source-less unsafe requests without any trust signal", async () => {
|
|
235
|
+
const { calls, fetcher } = recorder();
|
|
236
|
+
const response = await dispatch(
|
|
237
|
+
request("/_allauth/app/v1/auth/sessions", { method: "DELETE" }),
|
|
238
|
+
fetcher,
|
|
239
|
+
);
|
|
240
|
+
expect(response?.status).toBe(403);
|
|
241
|
+
expect(calls).toHaveLength(0);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test("forwards source-less safe cloud requests", async () => {
|
|
245
|
+
const { calls, fetcher } = recorder();
|
|
246
|
+
const response = await dispatch(
|
|
247
|
+
request("/_allauth/browser/v1/config", { method: "GET" }),
|
|
248
|
+
fetcher,
|
|
249
|
+
);
|
|
250
|
+
expect(response?.status).toBe(200);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test("preserves non-Origin headers", async () => {
|
|
254
|
+
const { calls, fetcher } = recorder();
|
|
255
|
+
await dispatch(
|
|
256
|
+
request("/_allauth/browser/v1/auth/session", {
|
|
257
|
+
method: "POST",
|
|
258
|
+
origin: "app://mycadet.ai",
|
|
259
|
+
headers: {
|
|
260
|
+
authorization: "Bearer api-token",
|
|
261
|
+
"content-type": "application/json",
|
|
262
|
+
"x-csrftoken": "csrf123",
|
|
263
|
+
},
|
|
264
|
+
}),
|
|
265
|
+
fetcher,
|
|
266
|
+
);
|
|
267
|
+
expect(header(calls[0]?.init, "authorization")).toBe("Bearer api-token");
|
|
268
|
+
expect(header(calls[0]?.init, "content-type")).toBe("application/json");
|
|
269
|
+
expect(header(calls[0]?.init, "x-csrftoken")).toBe("csrf123");
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test("marks POST/PUT/PATCH/DELETE as having a body, GET/HEAD as none", async () => {
|
|
273
|
+
for (const method of ["POST", "PUT", "PATCH", "DELETE"]) {
|
|
274
|
+
const { calls, fetcher } = recorder();
|
|
275
|
+
await dispatch(
|
|
276
|
+
request("/_allauth/app/v1/auth/sessions", {
|
|
277
|
+
method,
|
|
278
|
+
origin: "app://mycadet.ai",
|
|
279
|
+
}),
|
|
280
|
+
fetcher,
|
|
281
|
+
);
|
|
282
|
+
expect(calls[0]?.init?.body).toBeNull();
|
|
283
|
+
expect((calls[0]?.init as { duplex?: unknown } | undefined)?.duplex).toBe(
|
|
284
|
+
"half",
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
for (const method of ["GET", "HEAD"]) {
|
|
288
|
+
const { calls, fetcher } = recorder();
|
|
289
|
+
await dispatch(
|
|
290
|
+
request("/_allauth/app/v1/auth/sessions", { method }),
|
|
291
|
+
fetcher,
|
|
292
|
+
);
|
|
293
|
+
expect(calls[0]?.init?.body).toBeUndefined();
|
|
294
|
+
expect(
|
|
295
|
+
(calls[0]?.init as { duplex?: unknown } | undefined)?.duplex,
|
|
296
|
+
).toBeUndefined();
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
test("resolves the target through the same CADET_CLOUD_URL override (staging)", async () => {
|
|
301
|
+
process.env.CADET_CLOUD_URL = "https://staging-cloud.mycadet.ai";
|
|
302
|
+
const { calls, fetcher } = recorder();
|
|
303
|
+
await dispatch(request("/_allauth/browser/v1/config"), fetcher);
|
|
304
|
+
expect(calls[0]?.url).toBe(
|
|
305
|
+
"https://staging-cloud.mycadet.ai/_allauth/browser/v1/config",
|
|
306
|
+
);
|
|
307
|
+
expect(header(calls[0]?.init, "origin")).toBe(
|
|
308
|
+
"https://staging-cloud.mycadet.ai",
|
|
309
|
+
);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
test("resolves the target through a local cloud URL override", async () => {
|
|
313
|
+
process.env.CADET_CLOUD_URL = "http://localhost:8000";
|
|
314
|
+
const { calls, fetcher } = recorder();
|
|
315
|
+
await dispatch(request("/_allauth/browser/v1/config"), fetcher);
|
|
316
|
+
expect(calls[0]?.url).toBe(
|
|
317
|
+
"http://localhost:8000/_allauth/browser/v1/config",
|
|
318
|
+
);
|
|
319
|
+
expect(header(calls[0]?.init, "origin")).toBe("http://localhost:8000");
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
test("a row's answer wins over the arm", async () => {
|
|
323
|
+
const { calls, fetcher } = recorder();
|
|
324
|
+
// The assistants list row owns this path; the arm never dials.
|
|
325
|
+
const response = await dispatch(request("/v1/assistants"), fetcher);
|
|
326
|
+
expect(await response?.text()).not.toBe("forwarded");
|
|
327
|
+
expect(calls).toHaveLength(0);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
test("reports failed attempts through console with module-owned wording", async () => {
|
|
331
|
+
const errors: string[] = [];
|
|
332
|
+
const original = console.error;
|
|
333
|
+
console.error = (...args: unknown[]) => {
|
|
334
|
+
errors.push(args.map(String).join(" "));
|
|
335
|
+
};
|
|
336
|
+
try {
|
|
337
|
+
await forwardCloudRequest(request("/_allauth/browser/v1/config"), () =>
|
|
338
|
+
Promise.reject(new Error("boom")),
|
|
339
|
+
);
|
|
340
|
+
} finally {
|
|
341
|
+
console.error = original;
|
|
342
|
+
}
|
|
343
|
+
expect(errors).toHaveLength(1);
|
|
344
|
+
expect(errors[0]).toContain("[cloud-forward] fetch failed (attempt 1)");
|
|
345
|
+
expect(errors[0]).toContain(
|
|
346
|
+
"GET https://cloud.mycadet.ai/_allauth/browser/v1/config",
|
|
347
|
+
);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
test("transient net failures log as warnings, the rest as errors", async () => {
|
|
351
|
+
const levels: string[] = [];
|
|
352
|
+
const originalWarn = console.warn;
|
|
353
|
+
const originalError = console.error;
|
|
354
|
+
console.warn = () => levels.push("warning");
|
|
355
|
+
console.error = () => levels.push("error");
|
|
356
|
+
try {
|
|
357
|
+
// DELETE never retries — one attempt each, so the level selection
|
|
358
|
+
// is the pin, not the retry count.
|
|
359
|
+
await forwardCloudRequest(
|
|
360
|
+
request("/_allauth/app/v1/auth/sessions", {
|
|
361
|
+
method: "DELETE",
|
|
362
|
+
origin: "app://mycadet.ai",
|
|
363
|
+
}),
|
|
364
|
+
() => Promise.reject(new Error("net::ERR_NETWORK_CHANGED")),
|
|
365
|
+
);
|
|
366
|
+
await forwardCloudRequest(
|
|
367
|
+
request("/_allauth/app/v1/auth/sessions", {
|
|
368
|
+
method: "DELETE",
|
|
369
|
+
origin: "app://mycadet.ai",
|
|
370
|
+
}),
|
|
371
|
+
() => Promise.reject(new Error("boom")),
|
|
372
|
+
);
|
|
373
|
+
} finally {
|
|
374
|
+
console.warn = originalWarn;
|
|
375
|
+
console.error = originalError;
|
|
376
|
+
}
|
|
377
|
+
expect(levels).toEqual(["warning", "error"]);
|
|
378
|
+
});
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
describe("the arm's transient-failure retry", () => {
|
|
382
|
+
test("retries a GET through a transient failure and returns the eventual success", async () => {
|
|
383
|
+
let attempts = 0;
|
|
384
|
+
const { fetcher } = recorder();
|
|
385
|
+
const flaky: Fetch = async (url, init) => {
|
|
386
|
+
attempts += 1;
|
|
387
|
+
if (attempts === 1) throw new Error("net::ERR_NETWORK_CHANGED");
|
|
388
|
+
return fetcher(url, init);
|
|
389
|
+
};
|
|
390
|
+
const response = await forwardCloudRequest(
|
|
391
|
+
request("/_allauth/browser/v1/config"),
|
|
392
|
+
flaky,
|
|
393
|
+
);
|
|
394
|
+
expect(response?.status).toBe(200);
|
|
395
|
+
expect(attempts).toBe(2);
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
test("returns the structured 502 after exhausting retries", async () => {
|
|
399
|
+
let attempts = 0;
|
|
400
|
+
const response = await forwardCloudRequest(
|
|
401
|
+
request("/_allauth/browser/v1/config"),
|
|
402
|
+
async () => {
|
|
403
|
+
attempts += 1;
|
|
404
|
+
throw new Error("net::ERR_NETWORK_CHANGED");
|
|
405
|
+
},
|
|
406
|
+
);
|
|
407
|
+
expect(attempts).toBe(3);
|
|
408
|
+
expect(response?.status).toBe(502);
|
|
409
|
+
const body = (await response?.json()) as Record<string, unknown>;
|
|
410
|
+
expect(body.code).toBe(PROXY_NETWORK_ERROR_CODE);
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
test("does not retry requests with a body — their stream is single-use", async () => {
|
|
414
|
+
let attempts = 0;
|
|
415
|
+
const response = await forwardCloudRequest(
|
|
416
|
+
request("/_allauth/browser/v1/auth/session", {
|
|
417
|
+
method: "POST",
|
|
418
|
+
origin: "app://mycadet.ai",
|
|
419
|
+
}),
|
|
420
|
+
async () => {
|
|
421
|
+
attempts += 1;
|
|
422
|
+
throw new Error("net::ERR_NETWORK_CHANGED");
|
|
423
|
+
},
|
|
424
|
+
);
|
|
425
|
+
expect(attempts).toBe(1);
|
|
426
|
+
expect(response?.status).toBe(502);
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
test("does not retry non-transient failures", async () => {
|
|
430
|
+
let attempts = 0;
|
|
431
|
+
const response = await forwardCloudRequest(
|
|
432
|
+
request("/_allauth/browser/v1/config"),
|
|
433
|
+
async () => {
|
|
434
|
+
attempts += 1;
|
|
435
|
+
throw new Error("net::ERR_CERT_AUTHORITY_INVALID");
|
|
436
|
+
},
|
|
437
|
+
);
|
|
438
|
+
expect(attempts).toBe(1);
|
|
439
|
+
expect(response?.status).toBe(502);
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
test("a successful response passes through untouched on the first attempt", async () => {
|
|
443
|
+
const upstream = new Response("hello", { status: 201 });
|
|
444
|
+
const response = await forwardCloudRequest(
|
|
445
|
+
request("/_allauth/browser/v1/config"),
|
|
446
|
+
async () => upstream,
|
|
447
|
+
);
|
|
448
|
+
expect(response).toBe(upstream);
|
|
449
|
+
});
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
describe("isTransientNetworkError", () => {
|
|
453
|
+
test("matches Chromium transient net errors", () => {
|
|
454
|
+
for (const code of [
|
|
455
|
+
"ERR_NETWORK_CHANGED",
|
|
456
|
+
"ERR_INTERNET_DISCONNECTED",
|
|
457
|
+
"ERR_NAME_NOT_RESOLVED",
|
|
458
|
+
"ERR_CONNECTION_RESET",
|
|
459
|
+
]) {
|
|
460
|
+
expect(isTransientNetworkError(new Error(`net::${code}`))).toBe(true);
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
test("rejects other errors and non-Error values", () => {
|
|
465
|
+
expect(isTransientNetworkError(new Error("net::ERR_CERT_INVALID"))).toBe(
|
|
466
|
+
false,
|
|
467
|
+
);
|
|
468
|
+
expect(isTransientNetworkError(new Error("boom"))).toBe(false);
|
|
469
|
+
expect(isTransientNetworkError("ERR_NETWORK_CHANGED")).toBe(false);
|
|
470
|
+
expect(isTransientNetworkError(undefined)).toBe(false);
|
|
471
|
+
});
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
describe("buildProxyNetworkErrorResponse", () => {
|
|
475
|
+
test("returns a structured JSON 502 with the marker header", async () => {
|
|
476
|
+
const response = buildProxyNetworkErrorResponse();
|
|
477
|
+
expect(response.status).toBe(502);
|
|
478
|
+
expect(response.headers.get("content-type")).toBe("application/json");
|
|
479
|
+
expect(response.headers.get(PROXY_ERROR_HEADER)).toBe("network");
|
|
480
|
+
const body = (await response.json()) as Record<string, unknown>;
|
|
481
|
+
expect(body.code).toBe(PROXY_NETWORK_ERROR_CODE);
|
|
482
|
+
expect(typeof body.detail).toBe("string");
|
|
483
|
+
expect(body.detail).not.toContain("net::");
|
|
484
|
+
});
|
|
485
|
+
});
|