Skip to content
Myr

policy.test.ts

This file

Path

apps/cli/test/policy.test.ts

159 lines · 4.7 KiB · TypeScript

Last changed by

Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f

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–102why, one step back
1import { describe, expect, it } from "@effect/vitest";
2import { Brand } from "@repo/brand";3import { Contracts } from "@repo/protocol/contracts";4import { Envelope } from "@repo/protocol/envelope";5import { AskId, KindName } from "@repo/protocol/ids";6import { AskFull } from "@repo/protocol/refusal";7import { explain } from "@repo/protocol/render";8import { unanswered } from "@repo/protocol/testing";9import { Effect, Layer, Option, Schema } from "effect";1011import { detectMode } from "../src/policy.ts";12import { runProgram } from "./remote-server.ts";1314const fixWeek = AskId.make("fix-week");1516const listing = {17 ask: fixWeek,18 blockedBy: [],19 claimed: false,20 kind: KindName.make("fix"),21 territory: "packages/week",22 want: "Weeks start on the locale's first day",23};2425const platform = Layer.mergeAll(26 unanswered,27 Contracts.toLayerHandler("listAsks", () =>28 Effect.succeed({ asks: [listing] })29 ),30 Contracts.toLayerHandler("claim", ({ ask }) =>31 Effect.fail(new AskFull({ ask }))32 )33);3435const agent = { CLAUDECODE: "1" };3637const decodeEnvelope = Schema.decodeEffect(38 Schema.fromJsonString(Envelope(Schema.Unknown))39);4041describe("who the CLI is talking to", () => {42 it("is an agent when a harness's marker is set, even at a terminal", () => {43 const detected = detectMode({ env: agent, isTTY: true });4445 expect([detected.mode, detected.harness]).toStrictEqual([46 "agent",47 Option.some("claude-code"),48 ]);49 });5051 it.each([52 { env: {}, isTTY: false, mode: "automation" },53 { env: { CI: "true" }, isTTY: true, mode: "automation" },54 { env: {}, isTTY: true, mode: "human" },55 { env: { AI_AGENT: "pi" }, isTTY: false, mode: "agent" },56 ] as const)(57 "is $mode with env $env and a terminal: $isTTY",58 ({ env, isTTY, mode }) => {59 const detected = detectMode({ env, isTTY });6061 expect(detected.mode).toBe(mode);62 expect(detected.reason).not.toBe("");63 }64 );65});6667describe("under an agent", () => {68 it.effect("prints asks as a Markdown table and never prompts", () =>69 Effect.gen(function* () {70 const run = yield* runProgram(["asks", "--project", "datekit"], {71 env: agent,72 handlers: platform,73 });7475 expect(run.out).toContain("| ask | kind | territory | want | claimed |");76 expect(run.out.join("\n")).toContain("fix-week");77 expect(run.prompts).toBe(0);78 expect(run.code).toBe(0);79 })80 );8182 it.effect(83 "prints exactly one versioned envelope under --json, and nothing on stderr",84 () =>85 Effect.gen(function* () {86 const run = yield* runProgram(87 ["asks", "--project", "datekit", "--json"],88 { env: agent, handlers: platform }89 );9091 expect(run.out).toHaveLength(1);92 expect(run.err).toStrictEqual([]);93 expect(yield* decodeEnvelope(run.out[0] ?? "")).toStrictEqual({94 command: "asks",95 data: { asks: [listing] },96 ok: true,97 v: 1,98 });99 })100 );101102 it.effect(

Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862

line 103why, one step back
103 "prints a refusal as one refused envelope under --json, and exits with its code",

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 104–112why, one step back
104 () =>
105 Effect.gen(function* () {106 const run = yield* runProgram(107 ["claim", "fix-week", "--project", "datekit", "--json"],108 { env: agent, handlers: platform }109 );110111 expect(run.out).toHaveLength(1);112 expect(run.err).toStrictEqual([]);

Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862

line 113why, one step back
113 expect(run.code).toBe(75);

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 114–135why, one step back
114 expect(yield* decodeEnvelope(run.out[0] ?? "")).toStrictEqual({
115 command: "claim",116 ok: false,117 refusals: [new AskFull({ ask: fixWeek })],118 v: 1,119 });120 })121 );122123 it.effect(124 "prints a refusal's explanation once on stderr without --json, and nothing on stdout",125 () =>126 Effect.gen(function* () {127 const run = yield* runProgram(128 ["claim", "fix-week", "--project", "datekit"],129 { env: agent, handlers: platform }130 );131132 const [reason = ""] = explain([new AskFull({ ask: fixWeek })]);133 const printed = run.err.join("\n");134135 expect(run.out).toStrictEqual([]);

Merge cli/exit-codes into mainoscabrielbefore adoptionc19a862

line 136why, one step back
136 expect(run.code).toBe(75);

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 137–159why, one step back
137 expect(printed.split(reason)).toHaveLength(2);
138 expect(run.err).toStrictEqual([139 `- ${reason}`,140 ` - Next: \`${Brand.cli} asks\``,141 " - Retryable: yes",142 ]);143 })144 );145});146147describe("for a person at a terminal", () => {148 it.effect("prints asks as text, not Markdown", () =>149 Effect.gen(function* () {150 const run = yield* runProgram(["asks", "--project", "datekit"], {151 handlers: platform,152 });153154 expect(run.out.join("\n")).toContain("fix-week");155 expect(run.out.join("\n")).not.toContain("| ask |");156 expect(run.prompts).toBe(0);157 })158 );159});