surfaces.test.ts
This file
Path
apps/platform/test/surfaces.test.ts
169 lines · 4.9 KiB · TypeScript
Last changed by
Merge build/comments-platform into mainoscabrielbefore adoptionddf09e8
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–32why, one step back1import { describe, expect, it } from "@effect/vitest";2import { Contracts, surfaced } from "@repo/protocol/contracts";3import { Outcome } from "@repo/protocol/domain/outcome";4import {5 AskId,6 CommitSha,7 Handle,8 ProjectName,9 RequestId,10} from "@repo/protocol/ids";11import { CurrentPrincipal, Principal } from "@repo/protocol/principal";12import { NotClaimed } from "@repo/protocol/refusal";13import { Effect, Layer, Ref } from "effect";1415import { MemorySessions } from "../src/sessions/memory.ts";16import {17 noProjects,18 origin,19 rpcClient,20 served,21 toolCall,22 toolList,23 toolNames,24 toolResult,25} from "./harness.ts";2627const ask = AskId.make("fix-parser");2829const base = CommitSha.make("1111111111111111111111111111111111111111");3031const head = CommitSha.make("3333333333333333333333333333333333333333");32Merge build/1400-w14-foundation into mainoscabrielbefore adoption47dd2f8
line 33why, one step back33const 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 34–169why, one step back3435const requestId = RequestId.make("answer-0001");3637const args = { ask, base, head, project, requestId };3839const receipt = {40 ...args,41 outcome: Outcome.cases.Checking.make({ stage: "merge" }),42};4344const sessions = MemorySessions.layer({45 "alice-token": Handle.make("alice"),46 "bob-token": Handle.make("bob"),47});4849const claimHeldBy = (holder: string) =>50 Layer.mergeAll(51 noProjects,52 Contracts.toLayerHandler("submitAnswer", (payload) =>53 Effect.gen(function* () {54 const holds = Principal.match(yield* CurrentPrincipal, {55 Anonymous: () => false,56 Person: ({ handle }) => handle === holder,57 });5859 if (!holds) {60 return yield* NotClaimed.of(payload.ask);61 }6263 return receipt;64 })65 )66 );6768describe("the platform's MCP surface", () => {69 it.effect("calls a tool as the principal its bearer token names", () =>70 Effect.gen(function* () {71 const { fetch } = yield* served(72 Layer.mergeAll(claimHeldBy("alice"), sessions)73 );7475 const asAlice = yield* toolResult(76 yield* fetch(toolCall("submitAnswer", args, "alice-token"))77 );7879 const asBob = yield* toolResult(80 yield* fetch(toolCall("submitAnswer", args, "bob-token"))81 );8283 expect(asAlice).toStrictEqual({ isError: false, json: receipt });84 expect(asBob.isError).toBeTruthy();85 expect(asBob.json).toHaveProperty("_tag", "NotClaimed");86 })87 );8889 it.effect("lists the MCP-surfaced contracts only: no watch, no setEnv", () =>90 Effect.gen(function* () {91 const { fetch } = yield* served(Layer.mergeAll(noProjects, sessions));9293 const names = yield* toolNames(yield* fetch(toolList("alice-token")));9495 expect(names).toStrictEqual([...surfaced("mcp").requests.keys()]);96 expect(names).toContain("submitAnswer");97 expect(names).not.toContain("watch");98 expect(names).not.toContain("setEnv");99 })100 );101102 it.effect(103 "turns away a call with no credential, or one it does not know",104 () =>105 Effect.gen(function* () {106 const calls = yield* Ref.make(0);107108 const counted = Effect.andThen(109 Ref.update(calls, (count) => count + 1),110 Effect.die("a turned-away call reached a contract")111 );112113 const { fetch } = yield* served(114 Layer.mergeAll(115 noProjects,116 Contracts.toLayerHandler("listAsks", () => counted),117 Contracts.toLayerHandler("submitAnswer", () => counted),118 sessions119 )120 );121122 const responses = [123 yield* fetch(toolCall("submitAnswer", args)),124 yield* fetch(toolCall("submitAnswer", args, "mallory-token")),125 yield* fetch(toolCall("listAsks", { project })),126 yield* fetch(toolCall("listAsks", { project }, "mallory-token")),127 ];128129 const metadata = `resource_metadata="${origin}/.well-known/oauth-protected-resource"`;130131 expect(132 responses.map((response) => ({133 challenge: response.headers.get("www-authenticate"),134 status: response.status,135 }))136 ).toStrictEqual([137 { challenge: `Bearer ${metadata}`, status: 401 },138 {139 challenge: `Bearer error="invalid_token", ${metadata}`,140 status: 401,141 },142 { challenge: `Bearer ${metadata}`, status: 401 },143 {144 challenge: `Bearer error="invalid_token", ${metadata}`,145 status: 401,146 },147 ]);148 expect(yield* Ref.get(calls)).toBe(0);149 })150 );151});152153describe("the platform's RPC surface", () => {154 it.effect("calls a contract as the principal its bearer token names", () =>155 Effect.gen(function* () {156 const { handler } = yield* served(157 Layer.mergeAll(claimHeldBy("alice"), sessions)158 );159160 const alice = yield* rpcClient(handler, "alice-token");161 const bob = yield* rpcClient(handler, "bob-token");162163 expect(yield* alice.submitAnswer(args)).toStrictEqual(receipt);164 expect(yield* Effect.flip(bob.submitAnswer(args))).toStrictEqual(165 NotClaimed.of(ask)166 );167 })168 );169});