Skip to content
Myr

platform.test.ts

This file

Path

apps/platform/test/platform.test.ts

230 lines · 6.5 KiB · TypeScript

Last changed by

Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–14why, one step back
1import { describe, expect, it } from "@effect/vitest";
2import { ProjectName } from "@repo/protocol/ids";3import { Effect, Layer, Redacted } from "effect";45import { decodeEnv, InvalidEnv, platform } from "../src/platform.ts";6import {7 emptyFrontPage,8 fakeEnv,9 masterKey,10 operatorToken,11 without,12} from "./fake-env.ts";13import { rpcClient, served, toolCall } from "./harness.ts";14

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 15why, one step back
15const project = ProjectName.make("alice/datekit");

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 16–222why, one step back
16
17const failureOf = (env: Env) => Effect.flip(Layer.build(platform(env)));1819describe("the platform Worker's env", () => {20 it.effect("lets the operator's token call the contracts", () =>21 Effect.gen(function* () {22 const prepared: string[] = [];2324 const { handler } = yield* served(25 platform({ ...fakeEnv(), FRONT_PAGE: emptyFrontPage(prepared) })26 );2728 const operator = yield* rpcClient(handler, operatorToken);2930 expect(yield* operator.listProjects({})).toStrictEqual({31 projects: [],32 });33 expect(prepared).toHaveLength(1);34 })35 );3637 it.effect("turns away any other token", () =>38 Effect.gen(function* () {39 const { fetch } = yield* served(platform(fakeEnv()));4041 const response = yield* fetch(42 toolCall("listAsks", { project }, `${operatorToken}-not`)43 );4445 expect(response.status).toBe(401);46 })47 );4849 it.effect("fails to start without a usable operator token", () =>50 Effect.gen(function* () {51 const failure = yield* failureOf({52 ...fakeEnv(),53 OPERATOR_TOKEN: "short",54 });5556 expect(failure).toStrictEqual(57 new InvalidEnv({ keys: ["OPERATOR_TOKEN"] })58 );59 expect(JSON.stringify(failure)).not.toContain("short");60 })61 );6263 it.effect("fails to start without its Artifacts binding", () =>64 Effect.gen(function* () {65 const failure = yield* failureOf(without(fakeEnv(), "ARTIFACTS"));6667 expect(failure).toStrictEqual(new InvalidEnv({ keys: ["ARTIFACTS"] }));68 })69 );7071 it.effect("fails to start without the private namespace", () =>72 Effect.gen(function* () {73 const failure = yield* failureOf(without(fakeEnv(), "ARTIFACTS_PRIVATE"));7475 expect(failure).toStrictEqual(76 new InvalidEnv({ keys: ["ARTIFACTS_PRIVATE"] })77 );78 })79 );8081 it.effect(82 "fails to start without a master key of 64 hex characters, naming the key only",83 () =>84 Effect.gen(function* () {85 const missing = yield* failureOf(without(fakeEnv(), "MASTER_KEY"));8687 const short = yield* failureOf({88 ...fakeEnv(),89 MASTER_KEY: masterKey.slice(2),90 });9192 const notHex = yield* failureOf({93 ...fakeEnv(),94 MASTER_KEY: `${masterKey.slice(2)}zz`,95 });9697 expect([missing, short, notHex]).toStrictEqual([98 new InvalidEnv({ keys: ["MASTER_KEY"] }),99 new InvalidEnv({ keys: ["MASTER_KEY"] }),100 new InvalidEnv({ keys: ["MASTER_KEY"] }),101 ]);102 expect(JSON.stringify([short, notHex])).not.toContain(103 masterKey.slice(2)104 );105 })106 );107108 it.effect("keeps the master key redacted once decoded", () =>109 Effect.gen(function* () {110 const { MASTER_KEY } = yield* decodeEnv(fakeEnv());111112 expect(Redacted.isRedacted(MASTER_KEY)).toBeTruthy();113 expect(JSON.stringify({ MASTER_KEY })).toBe(114 '{"MASTER_KEY":"<redacted>"}'115 );116 expect(Redacted.value(MASTER_KEY)).toBe(masterKey);117 })118 );119120 it.effect("names every missing binding and var at once", () =>121 Effect.gen(function* () {122 const failure = yield* failureOf(123 without(without(fakeEnv(), "SANDBOX"), "STAGE")124 );125126 expect(failure).toStrictEqual(127 new InvalidEnv({ keys: ["SANDBOX", "STAGE"] })128 );129 })130 );131132 it.effect(133 "fails to start without sign-in's pepper, or with a short one, naming the key only",134 () =>135 Effect.gen(function* () {136 const missing = yield* failureOf(without(fakeEnv(), "PASSWORD_PEPPER"));137138 const short = yield* failureOf({139 ...fakeEnv(),140 PASSWORD_PEPPER: "short-pepper",141 });142143 expect([missing, short]).toStrictEqual([144 new InvalidEnv({ keys: ["PASSWORD_PEPPER"] }),145 new InvalidEnv({ keys: ["PASSWORD_PEPPER"] }),146 ]);147 expect(JSON.stringify(short)).not.toContain("short-pepper");148 })149 );150151 it.effect(152 "fails to start on a session table it cannot read, without printing it",153 () =>154 Effect.gen(function* () {155 const env = {156 ...fakeEnv(),157 SESSION_TOKENS: JSON.stringify({ "short-token": "alice" }),158 };159160 const failure = yield* failureOf(env);161162 expect(failure).toStrictEqual(163 new InvalidEnv({ keys: ["SESSION_TOKENS"] })164 );165 expect(JSON.stringify(failure)).not.toContain("short-token");166 })167 );168169 it.effect("lets a token from the session table call as its person", () =>170 Effect.gen(function* () {171 const token = "alice-token-0123456789abcdef01234567";172173 const env = {174 ...fakeEnv(),175 FRONT_PAGE: emptyFrontPage(),176 SESSION_TOKENS: JSON.stringify({ [token]: "alice" }),177 };178179 const { handler } = yield* served(platform(env));180181 const alice = yield* rpcClient(handler, token);182183 expect(yield* alice.listProjects({})).toStrictEqual({ projects: [] });184 })185 );186187 it.effect("lands with the container Lander unless the env says isogit", () =>188 Effect.gen(function* () {189 const unset = yield* decodeEnv(without(fakeEnv(), "LANDER_ADAPTER"));190191 const isogit = yield* decodeEnv({192 ...fakeEnv(),193 LANDER_ADAPTER: "isogit",194 });195196 expect([unset.LANDER_ADAPTER, isogit.LANDER_ADAPTER]).toStrictEqual([197 "container",198 "isogit",199 ]);200 })201 );202203 it.effect("answers /health with its stage, without a credential", () =>204 Effect.gen(function* () {205 const { fetch } = yield* served(platform(fakeEnv()));206207 const response = yield* fetch(new Request("http://localhost/health"));208209 expect(response.status).toBe(200);210 expect(yield* Effect.promise(() => response.json())).toStrictEqual({211 ok: true,212 stage: "local",213 });214 })215 );216217 it.effect("hands git's paths to the gateway, outside the 401 gate", () =>218 Effect.gen(function* () {219 const { fetch } = yield* served(platform(fakeEnv()));220221 const response = yield* fetch(222 new Request(

Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8

line 223why, one step back
223 "http://localhost/alice/datekit--layer--alice.git/info/refs?service=git-upload-pack"

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 224–230why, one step back
224 )
225 );226227 expect(response.status).toBe(404);228 })229 );230});