The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import { assert, it } from "@effect/vitest"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as FileSystem from "effect/FileSystem"; |
| 7 | import * as Path from "effect/Path"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | |
| 10 | import * as ManagedPolicy from "../policy/ManagedPolicy.ts"; |
| 11 | import { |
| 12 | makePersistedServerRuntimeState, |
| 13 | PersistedServerRuntimeState, |
| 14 | } from "../serverRuntimeState.ts"; |
| 15 | import { describeRunningServer } from "./policy.ts"; |
| 16 | |
| 17 | const encodeState = Schema.encodeEffect(Schema.fromJsonString(PersistedServerRuntimeState)); |
| 18 | |
| 19 | it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))((it) => |
| 20 | it.effect("tells whether the running server loaded the installed policy", () => |
| 21 | Effect.gen(function* () { |
| 22 | const fs = yield* FileSystem.FileSystem; |
| 23 | const statePath = (yield* Path.Path).join( |
| 24 | yield* fs.makeTempDirectoryScoped({ prefix: "t3-policy-show-" }), |
| 25 | "server-runtime.json", |
| 26 | ); |
| 27 | const loaded = (organization: string) => |
| 28 | ManagedPolicy.loadedPolicy({ |
| 29 | path: "/etc/t3code/policy.json", |
| 30 | document: { schemaVersion: 1, organization }, |
| 31 | }); |
| 32 | const installed = yield* loaded("Example Corp"); |
| 33 | // A live server (this process) that loaded `managedPolicy`; undefined is an older server. |
| 34 | const running = (managedPolicy: typeof installed | undefined, current = installed) => |
| 35 | Effect.gen(function* () { |
| 36 | const state = yield* makePersistedServerRuntimeState({ |
| 37 | config: { host: undefined, devUrl: undefined }, |
| 38 | port: 3773, |
| 39 | ...(managedPolicy === undefined ? {} : { managedPolicy }), |
| 40 | }); |
| 41 | yield* fs.writeFileString(statePath, yield* encodeState(state)); |
| 42 | return yield* describeRunningServer(statePath, current); |
| 43 | }); |
| 44 | |
| 45 | assert.include(yield* describeRunningServer(statePath, installed), "No T3 Code server"); |
| 46 | assert.include(yield* running(installed), "loaded this policy."); |
| 47 | assert.include( |
| 48 | yield* running(yield* loaded("Other Corp")), |
| 49 | "loaded Other Corp's policy from /etc/t3code/policy.json", |
| 50 | ); |
| 51 | assert.include(yield* running(null), "loaded no policy. Restart T3 Code"); |
| 52 | assert.include(yield* running(installed, null), "Restart T3 Code to apply"); |
| 53 | assert.include(yield* running(null, null), "runs without a policy, as installed"); |
| 54 | assert.include(yield* running(undefined), "does not report which policy it loaded"); |
| 55 | }), |
| 56 | ), |
| 57 | ); |