Open History to see the last commit
1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as HostProcess from "@t3tools/shared/HostProcess";
4import * as NetService from "@t3tools/shared/Net";
5import { assert, it } from "@effect/vitest";
6import * as Effect from "effect/Effect";
7import * as Layer from "effect/Layer";
8import * as FileSystem from "effect/FileSystem";
9import * as Path from "effect/Path";
10import * as Schema from "effect/Schema";
11import { Command } from "effect/cli";
12import * as TestConsole from "effect/testing/TestConsole";
13
14import { cli } from "../binCli.ts";
15import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
16import {
17 makePersistedServerRuntimeState,
18 PersistedServerRuntimeState,
19} from "../serverRuntimeState.ts";
20import { describeRunningServer } from "./policy.ts";
21
22const encodeState = Schema.encodeEffect(Schema.fromJsonString(PersistedServerRuntimeState));
23
24it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))((it) => {
25 it.effect("tells whether the running server loaded the installed policy", () =>
26 Effect.gen(function* () {
27 const fs = yield* FileSystem.FileSystem;
28 const statePath = (yield* Path.Path).join(
29 yield* fs.makeTempDirectoryScoped({ prefix: "t3-policy-show-" }),
30 "server-runtime.json",
31 );
32 const loaded = (organization: string) =>
33 ManagedPolicy.loadedPolicy({
34 path: "/etc/t3code/policy.json",
35 document: { schemaVersion: 1, organization },
36 });
37 const installed = yield* loaded("Example Corp");
38 // A live server (this process) that loaded `managedPolicy`; undefined is an older server.
39 const running = (managedPolicy: typeof installed | undefined, current = installed) =>
40 Effect.gen(function* () {
41 const state = yield* makePersistedServerRuntimeState({
42 config: { host: undefined, devUrl: undefined },
43 port: 3773,
44 ...(managedPolicy === undefined ? {} : { managedPolicy }),
45 });
46 yield* fs.writeFileString(statePath, yield* encodeState(state));
47 return yield* describeRunningServer(statePath, current);
48 });
49
50 assert.include(yield* describeRunningServer(statePath, installed), "No T3 Code server");
51 assert.include(yield* running(installed), "loaded this policy.");
52 assert.include(
53 yield* running(yield* loaded("Other Corp")),
54 "loaded Other Corp's policy from /etc/t3code/policy.json",
55 );
56 assert.include(yield* running(null), "loaded no policy. Restart T3 Code");
57 assert.include(yield* running(installed, null), "Restart T3 Code to apply");
58 assert.include(yield* running(null, null), "runs without a policy, as installed");
59 assert.include(yield* running(undefined), "does not report which policy it loaded");
60 }),
61 );
62
63 it.effect("templates expands ~/ in the output directory and --policy file", () =>
64 Effect.gen(function* () {
65 const fs = yield* FileSystem.FileSystem;
66 const path = yield* Path.Path;
67 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-policy-templates-" });
68 const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-policy-templates-cwd-" });
69 yield* Effect.acquireRelease(
70 Effect.sync(() => {
71 const previous = process.cwd();
72 process.chdir(cwd);
73 return previous;
74 }),
75 (previous) => Effect.sync(() => process.chdir(previous)),
76 );
77 yield* fs.writeFileString(
78 path.join(home, "policy.json"),
79 JSON.stringify({ schemaVersion: 1, organization: "Tilde Corp" }),
80 );
81
82 yield* Command.runWith(cli, { version: "0.0.0" })([
83 "policy",
84 "templates",
85 "~/out",
86 "--policy=~/policy.json",
87 ]).pipe(
88 Effect.provideService(HostProcess.HomeDirectory, home),
89 Effect.provide(NetService.layer),
90 );
91
92 assert.include(
93 yield* fs.readFileString(path.join(home, "out", "macos", "t3code.mobileconfig")),
94 "Tilde Corp",
95 );
96 }),
97 );
98
99 it.effect("check reads a ~/ policy file from the home directory", () =>
100 Effect.gen(function* () {
101 const fs = yield* FileSystem.FileSystem;
102 const path = yield* Path.Path;
103 const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-policy-check-" });
104 const file = path.join(home, "policy.json");
105 yield* fs.writeFileString(
106 file,
107 JSON.stringify({ schemaVersion: 1, organization: "Tilde Corp" }),
108 );
109
110 yield* Command.runWith(cli, { version: "0.0.0" })(["policy", "check", "~/policy.json"]).pipe(
111 Effect.provideService(HostProcess.HomeDirectory, home),
112 Effect.provide(NetService.layer),
113 );
114
115 assert.include((yield* TestConsole.logLines).join("\n"), `Managed by Tilde Corp (${file}`);
116 }).pipe(Effect.provide(Layer.fresh(TestConsole.layer))),
117 );
118});