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

policy.test.ts

57 lines2.6 KB

1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as FileSystem from "effect/FileSystem";
7import * as Path from "effect/Path";
8import * as Schema from "effect/Schema";
9​
10import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
11import {
12 makePersistedServerRuntimeState,
13 PersistedServerRuntimeState,
14} from "../serverRuntimeState.ts";
15import { describeRunningServer } from "./policy.ts";
16​
17const encodeState = Schema.encodeEffect(Schema.fromJsonString(PersistedServerRuntimeState));
18​
19it.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);