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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180import { describe, expect, it } from "@effect/vitest";import { Brand } from "@repo/brand";import { Contracts } from "@repo/protocol/contracts";import { AskId, Handle, KindName } from "@repo/protocol/ids";import { unanswered } from "@repo/protocol/testing";import { ConfigProvider, Effect, Layer, Option, Redacted } from "effect";
import { Credentials } from "../src/credentials.ts";import type { Profile } from "../src/credentials.ts";import { RemoteClient } from "../src/remote.ts";import { failingPlatform, httpPlatform, knownToken, runProgram,} from "./remote-server.ts";import type { Handlers } from "./remote-server.ts";
const handlers: Handlers = Layer.mergeAll( unanswered, Contracts.toLayerHandler("listAsks", () => Effect.succeed({ asks: [ { ask: AskId.make("fix-week"), blockedBy: [], claimed: false, kind: KindName.make("fix"), territory: "packages/week", want: "Weeks start on the locale's first day", }, ], }) ), Contracts.toLayerHandler("status", () => Effect.succeed({ answers: [], claims: [], handle: Handle.make("tester") }) ));
const holding = (profile: Option.Option<Profile>) => Layer.succeed(Credentials)({ current: Effect.succeed(profile), forHost: () => Effect.succeed(profile), put: () => Effect.void, remove: () => Effect.succeed(false), });
const profileOn = (url: string, token: string): Profile => ({ token: Redacted.make(token), url,});
const remote = ( credentials: Layer.Layer<Credentials>, env: Readonly<Record<string, string>> = {}) => RemoteClient.pipe( Layer.provide([ credentials, ConfigProvider.layer(ConfigProvider.fromEnv({ env: { ...env } })), ]) );
const urlVariable = `${Brand.slug.toUpperCase()}_URL`;
const signIn = `next: ${Brand.cli} login`;
describe("the remote client", () => { it.live("sends the stored profile's token as the HTTP bearer", () => Effect.gen(function* () { const platform = yield* httpPlatform(handlers);
const run = yield* runProgram(["status", "--json"], { client: remote( holding(Option.some(profileOn(platform.origin, knownToken))) ), handlers, });
expect(run.code).toBe(0); expect(yield* platform.seen).toStrictEqual([`Bearer ${knownToken}`]); }).pipe(Effect.scoped) );
it.live("status says whom the platform takes the caller for", () => Effect.gen(function* () { const platform = yield* httpPlatform(handlers);
const run = yield* runProgram(["status"], { client: remote( holding(Option.some(profileOn(platform.origin, knownToken))) ), handlers, });
expect(run.code).toBe(0); expect(run.out[0]).toBe("Signed in as tester."); }).pipe(Effect.scoped) );
it.live( "without a profile sends no credential: a public contract answers, a signed-in one says to log in", () => Effect.gen(function* () { const platform = yield* httpPlatform(handlers);
const client = remote(holding(Option.none()), { [urlVariable]: platform.origin, });
const asks = yield* runProgram(["asks", "--project", "datekit"], { client, handlers, });
const status = yield* runProgram(["status"], { client, handlers });
expect([asks.code, status.code]).toStrictEqual([0, 77]); expect(asks.out.join("\n")).toContain("fix-week"); expect(status.err.join("\n")).toContain(signIn); expect(status.out).toStrictEqual([]); expect(yield* platform.seen).toStrictEqual([undefined, undefined]); }).pipe(Effect.scoped) );
it.live( "turns a 401 into the same words, not a stack trace, and exits 77", () => Effect.gen(function* () { const platform = yield* httpPlatform(handlers);
const run = yield* runProgram(["status"], { client: remote( holding(Option.some(profileOn(platform.origin, "a-stale-token"))) ), handlers, });
expect(run.code).toBe(77); expect(run.err).toStrictEqual([ "Unauthenticated: the platform does not know who you are", ` ${signIn}`, " retryable: no", ]); }).pipe(Effect.scoped) );
it.live( "says in one line what failed when the platform answers with an error, and exits 69", () => Effect.gen(function* () { const origin = yield* failingPlatform(503);
const run = yield* runProgram(["asks", "--project", "datekit"], { client: remote(holding(Option.none()), { [urlVariable]: origin }), handlers, });
expect(run.code).toBe(69); expect(run.out).toStrictEqual([]); expect(run.err).toHaveLength(1); expect(run.err[0]).toMatch(/^could not reach the platform: .*\b503\b/u); expect(run.err[0]).toContain(`${origin}/rpc`); }).pipe(Effect.scoped) );
it.live("with no profile and no url, is a usage error naming login", () => Effect.gen(function* () { const run = yield* runProgram(["asks", "--project", "datekit"], { client: remote(holding(Option.none())), handlers, });
expect(run.code).toBe(2); expect(run.err).toHaveLength(1); expect(run.err[0]).toContain(`${Brand.cli} login`); }) );});