remote.test.ts
This file
Path
apps/cli/test/remote.test.ts
179 lines · 5.3 KiB · TypeScript
Last changed by
Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
History of this fileTake 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–117why, one step back1import { describe, expect, it } from "@effect/vitest";2import { Brand } from "@repo/brand";3import { Contracts } from "@repo/protocol/contracts";4import { AskId, Handle, KindName } from "@repo/protocol/ids";5import { unanswered } from "@repo/protocol/testing";6import { ConfigProvider, Effect, Layer, Option, Redacted } from "effect";78import { Credentials } from "../src/credentials.ts";9import type { Profile } from "../src/credentials.ts";10import { RemoteClient } from "../src/remote.ts";11import {12 failingPlatform,13 httpPlatform,14 knownToken,15 runProgram,16} from "./remote-server.ts";17import type { Handlers } from "./remote-server.ts";1819const handlers: Handlers = Layer.mergeAll(20 unanswered,21 Contracts.toLayerHandler("listAsks", () =>22 Effect.succeed({23 asks: [24 {25 ask: AskId.make("fix-week"),26 blockedBy: [],27 claimed: false,28 kind: KindName.make("fix"),29 territory: "packages/week",30 want: "Weeks start on the locale's first day",31 },32 ],33 })34 ),35 Contracts.toLayerHandler("status", () =>36 Effect.succeed({ answers: [], claims: [], handle: Handle.make("tester") })37 )38);3940const holding = (profile: Option.Option<Profile>) =>41 Layer.succeed(Credentials)({42 current: Effect.succeed(profile),43 forHost: () => Effect.succeed(profile),44 put: () => Effect.void,45 remove: () => Effect.succeed(false),46 });4748const profileOn = (url: string, token: string): Profile => ({49 token: Redacted.make(token),50 url,51});5253const remote = (54 credentials: Layer.Layer<Credentials>,55 env: Readonly<Record<string, string>> = {}56) =>57 RemoteClient.pipe(58 Layer.provide([59 credentials,60 ConfigProvider.layer(ConfigProvider.fromEnv({ env: { ...env } })),61 ])62 );6364const urlVariable = `${Brand.slug.toUpperCase()}_URL`;6566const signIn = `next: ${Brand.cli} login`;6768describe("the remote client", () => {69 it.live("sends the stored profile's token as the HTTP bearer", () =>70 Effect.gen(function* () {71 const platform = yield* httpPlatform(handlers);7273 const run = yield* runProgram(["status", "--json"], {74 client: remote(75 holding(Option.some(profileOn(platform.origin, knownToken)))76 ),77 handlers,78 });7980 expect(run.code).toBe(0);81 expect(yield* platform.seen).toStrictEqual([`Bearer ${knownToken}`]);82 }).pipe(Effect.scoped)83 );8485 it.live("status says whom the platform takes the caller for", () =>86 Effect.gen(function* () {87 const platform = yield* httpPlatform(handlers);8889 const run = yield* runProgram(["status"], {90 client: remote(91 holding(Option.some(profileOn(platform.origin, knownToken)))92 ),93 handlers,94 });9596 expect(run.code).toBe(0);97 expect(run.out[0]).toBe("Signed in as tester.");98 }).pipe(Effect.scoped)99 );100101 it.live(102 "without a profile sends no credential: a public contract answers, a signed-in one says to log in",103 () =>104 Effect.gen(function* () {105 const platform = yield* httpPlatform(handlers);106107 const client = remote(holding(Option.none()), {108 [urlVariable]: platform.origin,109 });110111 const asks = yield* runProgram(["asks", "--project", "datekit"], {112 client,113 handlers,114 });115116 const status = yield* runProgram(["status"], { client, handlers });117Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
line 118why, one step back118 expect([asks.code, status.code]).toStrictEqual([0, 77]);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 119–125why, one step back119 expect(asks.out.join("\n")).toContain("fix-week");120 expect(status.err.join("\n")).toContain(signIn);121 expect(status.out).toStrictEqual([]);122 expect(yield* platform.seen).toStrictEqual([undefined, undefined]);123 }).pipe(Effect.scoped)124 );125Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
lines 126–130why, one step back126 it.live(127 "turns a 401 into the same words, not a stack trace, and exits 77",128 () =>129 Effect.gen(function* () {130 const platform = yield* httpPlatform(handlers);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
line 131why, one step back131Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
lines 132–137why, one step back132 const run = yield* runProgram(["status"], {133 client: remote(134 holding(Option.some(profileOn(platform.origin, "a-stale-token")))135 ),136 handlers,137 });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
line 138why, one step back138Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
lines 139–145why, one step back139 expect(run.code).toBe(77);140 expect(run.err).toStrictEqual([141 "Unauthenticated: the platform does not know who you are",142 ` ${signIn}`,143 " retryable: no",144 ]);145 }).pipe(Effect.scoped)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 146–148why, one step back146 );147148 it.live(Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
line 149why, one step back149 "says in one line what failed when the platform answers with an error, and exits 69",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 150–158why, one step back150 () =>151 Effect.gen(function* () {152 const origin = yield* failingPlatform(503);153154 const run = yield* runProgram(["asks", "--project", "datekit"], {155 client: remote(holding(Option.none()), { [urlVariable]: origin }),156 handlers,157 });158Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862
line 159why, one step back159 expect(run.code).toBe(69);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 160–179why, one step back160 expect(run.out).toStrictEqual([]);161 expect(run.err).toHaveLength(1);162 expect(run.err[0]).toMatch(/^could not reach the platform: .*\b503\b/u);163 expect(run.err[0]).toContain(`${origin}/rpc`);164 }).pipe(Effect.scoped)165 );166167 it.live("with no profile and no url, is a usage error naming login", () =>168 Effect.gen(function* () {169 const run = yield* runProgram(["asks", "--project", "datekit"], {170 client: remote(holding(Option.none())),171 handlers,172 });173174 expect(run.code).toBe(2);175 expect(run.err).toHaveLength(1);176 expect(run.err[0]).toContain(`${Brand.cli} login`);177 })178 );179});