Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DesktopLocalEnvironmentAuth.test.ts

81 lines2.9 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Option from "effect/Option";
5import * as Ref from "effect/Ref";
6import * as HttpClient from "effect/unstable/http/HttpClient";
7import * as HttpClientResponse from "effect/unstable/http/HttpClientResponse";
8import { PRIMARY_LOCAL_ENVIRONMENT_ID } from "@t3tools/contracts";
9​
10import * as DesktopBackendPool from "./DesktopBackendPool.ts";
11import * as DesktopLocalEnvironmentAuth from "./DesktopLocalEnvironmentAuth.ts";
12​
13const config = {
14 executablePath: "/electron",
15 entryPath: "/server/bin.mjs",
16 cwd: "/server",
17 env: {},
18 bootstrap: {
19 mode: "desktop",
20 noBrowser: true,
21 port: 3773,
22 t3Home: "/tmp/t3",
23 host: "127.0.0.1",
24 desktopBootstrapToken: "desktop-bootstrap-token",
25 tailscaleServeEnabled: false,
26 tailscaleServePort: 443,
27 },
28 httpBaseUrl: new URL("http://127.0.0.1:3773"),
29 captureOutput: true,
30};
31​
32describe("DesktopLocalEnvironmentAuth", () => {
33 it.effect("exchanges the desktop bootstrap credential only once", () =>
34 Effect.gen(function* () {
35 const requestCount = yield* Ref.make(0);
36 const httpClientLayer = Layer.succeed(
37 HttpClient.HttpClient,
38 HttpClient.make((request) =>
39 Ref.update(requestCount, (count) => count + 1).pipe(
40 Effect.as(
41 HttpClientResponse.fromWeb(
42 request,
43 new Response(
44 JSON.stringify({
45 access_token: "desktop-bearer-token",
46 issued_token_type: "urn:ietf:params:oauth:token-type:access_token",
47 token_type: "Bearer",
48 expires_in: 3600,
49 scope: "orchestration:read",
50 }),
51 { status: 200, headers: { "content-type": "application/json" } },
52 ),
53 ),
54 ),
55 ),
56 ),
57 );
58 const poolLayer = Layer.succeed(DesktopBackendPool.DesktopBackendPool, {
59 list: Effect.succeed([
60 {
61 id: PRIMARY_LOCAL_ENVIRONMENT_ID,
62 label: Effect.succeed("Windows"),
63 currentConfig: Effect.succeed(Option.some(config)),
64 },
65 ]),
66 } as unknown as DesktopBackendPool.DesktopBackendPool["Service"]);
67 const testLayer = DesktopLocalEnvironmentAuth.layer.pipe(
68 Layer.provide(Layer.mergeAll(poolLayer, httpClientLayer)),
69 );
70​
71 const [first, second] = yield* Effect.gen(function* () {
72 const auth = yield* DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth;
73 return yield* Effect.all([auth.getBearerToken, auth.getBearerToken]);
74 }).pipe(Effect.provide(testLayer));
75​
76 assert.strictEqual(first, "desktop-bearer-token");
77 assert.strictEqual(second, "desktop-bearer-token");
78 assert.strictEqual(yield* Ref.get(requestCount), 1);
79 }),
80 );
81});