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

DesktopLocalEnvironmentAuth.test.ts

147 lines5.4 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Ref from "effect/Ref";
5import * as HttpClient from "effect/http/HttpClient";
6import * as HttpClientResponse from "effect/http/HttpClientResponse";
7import { PRIMARY_LOCAL_ENVIRONMENT_ID } from "@t3tools/contracts";
8import {
9 DESKTOP_BOOTSTRAP_TOKEN_WINDOW_MS,
10 currentDesktopBootstrapToken,
11} from "@t3tools/shared/desktopBootstrapToken";
12import * as TestClock from "effect/testing/TestClock";
13​
14import * as DesktopBackendPool from "./DesktopBackendPool.ts";
15import * as DesktopLocalEnvironmentAuth from "./DesktopLocalEnvironmentAuth.ts";
16​
17const config = {
18 executablePath: "/electron",
19 entryPath: "/server/bin.mjs",
20 cwd: "/server",
21 env: {},
22 bootstrap: {
23 mode: "desktop",
24 noBrowser: true,
25 port: 3773,
26 t3Home: "/tmp/t3",
27 host: "127.0.0.1",
28 desktopBootstrapToken: "desktop-bootstrap-token",
29 tailscaleServeEnabled: false,
30 tailscaleServePort: 443,
31 },
32 httpBaseUrl: new URL("http://127.0.0.1:3773"),
33 captureOutput: true,
34};
35​
36describe("DesktopLocalEnvironmentAuth", () => {
37 it.effect("exchanges the desktop bootstrap credential only once", () =>
38 Effect.gen(function* () {
39 const requestCount = yield* Ref.make(0);
40 const layerHttpClient = Layer.succeed(
41 HttpClient.HttpClient,
42 HttpClient.make((request) =>
43 Ref.update(requestCount, (count) => count + 1).pipe(
44 Effect.as(
45 HttpClientResponse.fromWeb(
46 request,
47 new Response(
48 JSON.stringify({
49 access_token: "desktop-bearer-token",
50 issued_token_type: "urn:ietf:params:oauth:token-type:access_token",
51 token_type: "Bearer",
52 expires_in: 3600,
53 scope: "orchestration:read",
54 }),
55 { status: 200, headers: { "content-type": "application/json" } },
56 ),
57 ),
58 ),
59 ),
60 ),
61 );
62 const layerPool = Layer.succeed(DesktopBackendPool.DesktopBackendPool, {
63 list: Effect.succeed([
64 {
65 id: PRIMARY_LOCAL_ENVIRONMENT_ID,
66 label: Effect.succeed("Windows"),
67 currentConfig: Effect.succeedSome(config),
68 },
69 ]),
70 } as unknown as DesktopBackendPool.DesktopBackendPool["Service"]);
71 const layerTest = DesktopLocalEnvironmentAuth.layer.pipe(
72 Layer.provide(Layer.mergeAll(layerPool, layerHttpClient)),
73 );
74​
75 const [first, second] = yield* Effect.gen(function* () {
76 const auth = yield* DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth;
77 return yield* Effect.all([auth.getBearerToken, auth.getBearerToken]);
78 }).pipe(Effect.provide(layerTest));
79​
80 assert.strictEqual(first, "desktop-bearer-token");
81 assert.strictEqual(second, "desktop-bearer-token");
82 assert.strictEqual(yield* Ref.get(requestCount), 1);
83 }),
84 );
85​
86 it.effect(
87 "exchanges the current window's token when the backend was launched with a secret",
88 () =>
89 Effect.gen(function* () {
90 const presented = yield* Ref.make<string | null>(null);
91 const httpClientLayer = Layer.succeed(
92 HttpClient.HttpClient,
93 HttpClient.make((request) => {
94 const body =
95 request.body._tag === "Uint8Array" ? new TextDecoder().decode(request.body.body) : "";
96 return Ref.set(presented, new URLSearchParams(body).get("subject_token")).pipe(
97 Effect.as(
98 HttpClientResponse.fromWeb(
99 request,
100 new Response(
101 JSON.stringify({
102 access_token: "desktop-bearer-token",
103 issued_token_type: "urn:ietf:params:oauth:token-type:access_token",
104 token_type: "Bearer",
105 expires_in: 3600,
106 scope: "orchestration:read",
107 }),
108 { status: 200, headers: { "content-type": "application/json" } },
109 ),
110 ),
111 ),
112 );
113 }),
114 );
115 const poolLayer = Layer.succeed(DesktopBackendPool.DesktopBackendPool, {
116 list: Effect.succeed([
117 {
118 id: PRIMARY_LOCAL_ENVIRONMENT_ID,
119 label: Effect.succeed("Windows"),
120 currentConfig: Effect.succeedSome({
121 ...config,
122 bootstrap: { ...config.bootstrap, desktopBootstrapSecret: "desktop-secret" },
123 }),
124 },
125 ]),
126 } as unknown as DesktopBackendPool.DesktopBackendPool["Service"]);
127​
128 // The first exchange happens a day after launch, past the launch token's windows.
129 yield* TestClock.setTime(DESKTOP_BOOTSTRAP_TOKEN_WINDOW_MS * 2 + 1);
130 yield* Effect.gen(function* () {
131 const auth = yield* DesktopLocalEnvironmentAuth.DesktopLocalEnvironmentAuth;
132 return yield* auth.getBearerToken;
133 }).pipe(
134 Effect.provide(
135 DesktopLocalEnvironmentAuth.layer.pipe(
136 Layer.provide(Layer.mergeAll(poolLayer, httpClientLayer)),
137 ),
138 ),
139 );
140​
141 assert.strictEqual(
142 yield* Ref.get(presented),
143 currentDesktopBootstrapToken("desktop-secret", DESKTOP_BOOTSTRAP_TOKEN_WINDOW_MS * 2 + 1),
144 );
145 }).pipe(Effect.provide(TestClock.layer())),
146 );
147});