Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
188 lines
7.0 KiB
TypeScript
188 lines
7.0 KiB
TypeScript
import { afterEach, describe, expect, it } from "vite-plus/test";
|
|
import {
|
|
AuthOrchestrationReadScope,
|
|
AuthOrchestrationOperateScope,
|
|
AuthSessionId,
|
|
LOCAL_DEVICE_HOST_ID,
|
|
type AuthEnvironmentScope,
|
|
} from "@t3tools/contracts";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Layer from "effect/Layer";
|
|
import { HttpClient, HttpClientResponse, HttpRouter } from "effect/http";
|
|
import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
|
|
import * as DeviceService from "./DeviceService.ts";
|
|
import * as DeviceHubProxy from "./DeviceHubProxy.ts";
|
|
|
|
const disposers: Array<() => Promise<void>> = [];
|
|
afterEach(async () => {
|
|
for (const dispose of disposers.splice(0)) await dispose();
|
|
});
|
|
|
|
const fixture = (
|
|
scopes: ReadonlyArray<AuthEnvironmentScope>,
|
|
fail = false,
|
|
authError?: EnvironmentAuth.ServerAuthCredentialError | EnvironmentAuth.ServerAuthInternalError,
|
|
) => {
|
|
let finalized = 0;
|
|
const requests: string[] = [];
|
|
const client = HttpClient.make((request, _url, signal) =>
|
|
Effect.gen(function* () {
|
|
requests.push(request.url);
|
|
signal.addEventListener("abort", () => {
|
|
finalized++;
|
|
});
|
|
if (fail) return yield* Effect.die(new Error("upstream failed"));
|
|
return HttpClientResponse.fromWeb(request, new Response("frame"));
|
|
}),
|
|
);
|
|
const { handler, dispose } = HttpRouter.toWebHandler(
|
|
DeviceHubProxy.layer.pipe(
|
|
Layer.provideMerge(
|
|
Layer.succeed(EnvironmentAuth.EnvironmentAuth, {
|
|
authenticateWebSocketUpgrade: () =>
|
|
authError
|
|
? Effect.fail(authError)
|
|
: Effect.succeed({
|
|
sessionId: AuthSessionId.make("test"),
|
|
subject: "test",
|
|
method: "bearer-access-token",
|
|
scopes,
|
|
}),
|
|
} as unknown as EnvironmentAuth.EnvironmentAuth["Service"]),
|
|
),
|
|
Layer.provideMerge(
|
|
Layer.succeed(DeviceService.DeviceService, {
|
|
currentReadiness: () =>
|
|
Effect.succeed({ hostId: LOCAL_DEVICE_HOST_ID, hub: { origin: "http://hub.test" } }),
|
|
} as DeviceService.DeviceService["Service"]),
|
|
),
|
|
Layer.provideMerge(Layer.succeed(HttpClient.HttpClient, client)),
|
|
),
|
|
{ disableLogger: true },
|
|
);
|
|
disposers.push(dispose);
|
|
return { handler, requests, finalized: () => finalized };
|
|
};
|
|
|
|
describe("device hub proxy", () => {
|
|
it("releases the upstream response after forwarding its body and strips tickets", async () => {
|
|
const { handler, requests, finalized } = fixture([AuthOrchestrationReadScope]);
|
|
const response = await handler(
|
|
new Request("http://t3.test/api/device-hub/api/devices?wsTicket=secret"),
|
|
);
|
|
expect(response.status).toBe(200);
|
|
expect(await response.text()).toBe("frame");
|
|
expect(requests).toEqual(["http://hub.test/api/devices"]);
|
|
expect(finalized()).toBe(1);
|
|
});
|
|
|
|
it("releases resources when upstream acquisition fails", async () => {
|
|
const { handler, finalized } = fixture([AuthOrchestrationReadScope], true);
|
|
const response = await handler(new Request("http://t3.test/api/device-hub/api/devices"));
|
|
expect(response.status).toBe(500);
|
|
expect(finalized()).toBe(1);
|
|
});
|
|
|
|
it.each(["/vendor/serve-sim/helper/ws", "/vendor/serve-emu/ws"])(
|
|
"rejects input socket %s for a read-only session",
|
|
async (path) => {
|
|
const { handler, requests } = fixture([AuthOrchestrationReadScope]);
|
|
const response = await handler(
|
|
new Request(`http://t3.test/api/device-hub${path}`, { headers: { upgrade: "websocket" } }),
|
|
);
|
|
expect(response.status).toBe(403);
|
|
expect(requests).toEqual([]);
|
|
},
|
|
);
|
|
|
|
it("requires operate scope for stream tuning", async () => {
|
|
const readOnly = fixture([AuthOrchestrationReadScope]);
|
|
const path = "http://t3.test/api/device-hub/vendor/serve-emu/api/stream-settings";
|
|
expect((await readOnly.handler(new Request(path, { method: "POST" }))).status).toBe(403);
|
|
const operator = fixture([AuthOrchestrationOperateScope]);
|
|
const response = await operator.handler(new Request(path, { method: "POST" }));
|
|
expect(response.status).toBe(200);
|
|
await response.text();
|
|
});
|
|
|
|
it("reads Android fold state but requires operate scope to change it", async () => {
|
|
const path = "http://t3.test/api/device-hub/vendor/serve-emu/api/fold?device=emulator-5554";
|
|
const reader = fixture([AuthOrchestrationReadScope]);
|
|
const read = await reader.handler(new Request(path));
|
|
expect(read.status).toBe(200);
|
|
await read.text();
|
|
expect(reader.requests).toEqual([
|
|
"http://hub.test/vendor/serve-emu/api/fold?device=emulator-5554",
|
|
]);
|
|
const denied = await reader.handler(
|
|
new Request(path, { method: "POST", body: '{"posture":"closed"}' }),
|
|
);
|
|
expect(denied.status).toBe(403);
|
|
expect(reader.requests).toHaveLength(1);
|
|
|
|
const operator = fixture([AuthOrchestrationOperateScope]);
|
|
const changed = await operator.handler(
|
|
new Request(path, { method: "POST", body: '{"posture":"closed"}' }),
|
|
);
|
|
expect(changed.status).toBe(200);
|
|
await changed.text();
|
|
expect(operator.requests).toEqual([
|
|
"http://hub.test/vendor/serve-emu/api/fold?device=emulator-5554",
|
|
]);
|
|
});
|
|
|
|
it("never forwards the vendor shell endpoint", async () => {
|
|
const { handler, requests } = fixture([AuthOrchestrationOperateScope]);
|
|
expect(
|
|
(
|
|
await handler(
|
|
new Request("http://t3.test/api/device-hub/vendor/serve-sim/exec", { method: "POST" }),
|
|
)
|
|
).status,
|
|
).toBe(404);
|
|
expect(requests).toEqual([]);
|
|
});
|
|
});
|
|
|
|
it.each([
|
|
[new EnvironmentAuth.ServerAuthMissingCredentialError({}), 401],
|
|
[
|
|
new EnvironmentAuth.ServerAuthSessionCredentialValidationError({
|
|
cause: new Error("private credential diagnostic"),
|
|
}),
|
|
500,
|
|
],
|
|
] as const)("translates authentication failure to HTTP %s", async (error, status) => {
|
|
const { handler, requests } = fixture([], false, error);
|
|
const response = await handler(new Request("http://t3.test/api/device-hub/api/devices"));
|
|
expect(response.status).toBe(status);
|
|
expect(await response.text()).not.toContain("private credential diagnostic");
|
|
expect(requests).toEqual([]);
|
|
});
|
|
|
|
it.each([1, 3])(
|
|
"forwards fixed Duo display %s through the authenticated read proxy",
|
|
async (panel) => {
|
|
const { handler, requests } = fixture([AuthOrchestrationReadScope]);
|
|
const route = `/vendor/serve-sim/helper/duo/panel/${panel}/stream.avcc`;
|
|
const response = await handler(
|
|
new Request(`http://t3.test/api/device-hub${route}?wsTicket=secret`),
|
|
);
|
|
expect(response.status).toBe(200);
|
|
await response.text();
|
|
expect(requests).toEqual([`http://hub.test${route}`]);
|
|
},
|
|
);
|
|
|
|
it.each(["/panel/2/stream.avcc", "/panel/1/webrtc/offer", "/panel/3/exec"])(
|
|
"rejects unsupported Duo route %s",
|
|
async (route) => {
|
|
const { handler, requests } = fixture([AuthOrchestrationReadScope]);
|
|
const response = await handler(
|
|
new Request(`http://t3.test/api/device-hub/vendor/serve-sim/helper/duo${route}`),
|
|
);
|
|
expect(response.status).toBe(404);
|
|
expect(requests).toEqual([]);
|
|
},
|
|
);
|