Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

policy.test.ts

55 lines2.4 KB

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