|
1 | 1 | // @effect-diagnostics nodeBuiltinImport:off |
2 | 2 |
|
3 | 3 | import { it } from "@effect/vitest"; |
| 4 | +import * as NodeTls from "node:tls"; |
| 5 | +import * as NodeCrypto from "node:crypto"; |
| 6 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
4 | 7 | import * as Effect from "effect/Effect"; |
5 | | -import { describe, expect, it as test } from "vite-plus/test"; |
| 8 | +import * as FileSystem from "effect/FileSystem"; |
| 9 | +import * as Path from "effect/Path"; |
| 10 | +import { describe, expect } from "vite-plus/test"; |
6 | 11 |
|
7 | 12 | import { |
8 | | - applyDesktopShellEnvironmentSnapshot, |
| 13 | + makeApplyDesktopShellEnvironmentSnapshot, |
9 | 14 | requireDesktopTelemetryWriteProgress, |
10 | 15 | } from "./DesktopTelemetryReceiver.ts"; |
11 | 16 |
|
12 | 17 | describe("DesktopTelemetryReceiver", () => { |
13 | | - test.each([ |
| 18 | + it.effect("updates live TLS trust when shell certificates arrive after startup", () => |
| 19 | + Effect.gen(function* () { |
| 20 | + const fs = yield* FileSystem.FileSystem; |
| 21 | + const path = yield* Path.Path; |
| 22 | + const directory = yield* fs.makeTempDirectoryScoped(); |
| 23 | + const certificatePath = path.join(directory, "extra-ca.pem"); |
| 24 | + const original = NodeTls.getCACertificates("default"); |
| 25 | + yield* Effect.addFinalizer(() => |
| 26 | + Effect.sync(() => NodeTls.setDefaultCACertificates(original)), |
| 27 | + ); |
| 28 | + const baseline = NodeTls.rootCertificates[0]!; |
| 29 | + const extra = NodeTls.rootCertificates[1]!; |
| 30 | + const replacement = NodeTls.rootCertificates[2]!; |
| 31 | + NodeTls.setDefaultCACertificates([baseline]); |
| 32 | + yield* fs.writeFileString(certificatePath, `${extra}\n${replacement}`); |
| 33 | + const environment: NodeJS.ProcessEnv = { T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING: "1" }; |
| 34 | + const applySnapshot = makeApplyDesktopShellEnvironmentSnapshot(environment, "darwin"); |
| 35 | + const message = { |
| 36 | + version: 1, |
| 37 | + type: "shellEnvironment", |
| 38 | + platform: "darwin", |
| 39 | + discovered: true, |
| 40 | + environment: { NODE_EXTRA_CA_CERTS: certificatePath }, |
| 41 | + } as const; |
| 42 | + const fingerprints = (certificates: ReadonlyArray<string>) => |
| 43 | + certificates.map((pem) => new NodeCrypto.X509Certificate(pem).fingerprint256).sort(); |
| 44 | + yield* applySnapshot({ ...message, platform: "win32" }); |
| 45 | + expect(fingerprints(NodeTls.getCACertificates("default"))).toEqual(fingerprints([baseline])); |
| 46 | + expect(environment.T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING).toBe("1"); |
| 47 | + yield* applySnapshot(message); |
| 48 | + expect(fingerprints(NodeTls.getCACertificates("default"))).toEqual( |
| 49 | + fingerprints([baseline, extra, replacement]), |
| 50 | + ); |
| 51 | + expect(environment.NODE_EXTRA_CA_CERTS).toBe(certificatePath); |
| 52 | + expect(environment.T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING).toBeUndefined(); |
| 53 | + |
| 54 | + yield* fs.writeFileString(certificatePath, replacement); |
| 55 | + yield* applySnapshot(message); |
| 56 | + expect(fingerprints(NodeTls.getCACertificates("default"))).toEqual( |
| 57 | + fingerprints([baseline, replacement]), |
| 58 | + ); |
| 59 | + |
| 60 | + yield* fs.writeFileString( |
| 61 | + certificatePath, |
| 62 | + "-----BEGIN CERTIFICATE-----\ninvalid\n-----END CERTIFICATE-----", |
| 63 | + ); |
| 64 | + yield* applySnapshot(message); |
| 65 | + expect(fingerprints(NodeTls.getCACertificates("default"))).toEqual( |
| 66 | + fingerprints([baseline, replacement]), |
| 67 | + ); |
| 68 | + yield* fs.remove(certificatePath); |
| 69 | + yield* applySnapshot(message); |
| 70 | + expect(fingerprints(NodeTls.getCACertificates("default"))).toEqual( |
| 71 | + fingerprints([baseline, replacement]), |
| 72 | + ); |
| 73 | + |
| 74 | + yield* applySnapshot({ ...message, environment: { NODE_EXTRA_CA_CERTS: null } }); |
| 75 | + expect(fingerprints(NodeTls.getCACertificates("default"))).toEqual(fingerprints([baseline])); |
| 76 | + expect(environment.NODE_EXTRA_CA_CERTS).toBeUndefined(); |
| 77 | + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 78 | + ); |
| 79 | + |
| 80 | + it.effect.each([ |
14 | 81 | { platform: "darwin", discovered: true, applied: true, pending: false }, |
15 | 82 | { platform: "win32", discovered: true, applied: false, pending: true }, |
16 | 83 | { platform: "darwin", discovered: false, applied: true, pending: true }, |
17 | 84 | ])( |
18 | 85 | "applies platform=$platform discovered=$discovered with pending=$pending", |
19 | | - ({ platform, discovered, applied, pending }) => { |
20 | | - const environment: NodeJS.ProcessEnv = { |
21 | | - PATH: "/old/bin", |
22 | | - JAVA_HOME: "/old/java", |
23 | | - UNRELATED: "keep", |
24 | | - T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING: "1", |
25 | | - }; |
26 | | - applyDesktopShellEnvironmentSnapshot(environment, "darwin", { |
27 | | - version: 1, |
28 | | - type: "shellEnvironment", |
29 | | - platform, |
30 | | - discovered, |
31 | | - environment: { PATH: "/fresh/bin", JAVA_HOME: null }, |
32 | | - }); |
33 | | - expect(environment).toEqual({ |
34 | | - PATH: applied ? "/fresh/bin" : "/old/bin", |
35 | | - ...(applied ? {} : { JAVA_HOME: "/old/java" }), |
36 | | - UNRELATED: "keep", |
37 | | - ...(pending ? { T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING: "1" } : {}), |
38 | | - }); |
39 | | - }, |
| 86 | + ({ platform, discovered, applied, pending }) => |
| 87 | + Effect.gen(function* () { |
| 88 | + const environment: NodeJS.ProcessEnv = { |
| 89 | + PATH: "/old/bin", |
| 90 | + JAVA_HOME: "/old/java", |
| 91 | + UNRELATED: "keep", |
| 92 | + T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING: "1", |
| 93 | + }; |
| 94 | + yield* makeApplyDesktopShellEnvironmentSnapshot( |
| 95 | + environment, |
| 96 | + "darwin", |
| 97 | + )({ |
| 98 | + version: 1, |
| 99 | + type: "shellEnvironment", |
| 100 | + platform, |
| 101 | + discovered, |
| 102 | + environment: { PATH: "/fresh/bin", JAVA_HOME: null }, |
| 103 | + }); |
| 104 | + expect(environment).toEqual({ |
| 105 | + PATH: applied ? "/fresh/bin" : "/old/bin", |
| 106 | + ...(applied ? {} : { JAVA_HOME: "/old/java" }), |
| 107 | + UNRELATED: "keep", |
| 108 | + ...(pending ? { T3CODE_DESKTOP_SHELL_DISCOVERY_PENDING: "1" } : {}), |
| 109 | + }); |
| 110 | + }).pipe(Effect.provide(NodeServices.layer)), |
40 | 111 | ); |
41 | 112 |
|
42 | 113 | it.effect("models a zero-byte control write as a stalled descriptor", () => |
|
0 commit comments