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

driver.test.ts

155 lines5.6 KB

1// @effect-diagnostics nodeBuiltinImport:off -- the fake `op` reads the template the driver wrote.
2import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
3import * as NodeFileSystem from "@effect/platform-node/NodeFileSystem";
4import { assert, describe, it } from "@effect/vitest";
5import * as Deferred from "effect/Deferred";
6import * as Effect from "effect/Effect";
7import * as Fiber from "effect/Fiber";
8import * as Layer from "effect/Layer";
9import * as Sink from "effect/Sink";
10import * as Stream from "effect/Stream";
11import * as TestClock from "effect/testing/TestClock";
12import { ChildProcessSpawner } from "effect/process";
13import * as NodeFS from "node:fs";
14​
15import { OnePasswordAccount } from "../shared/definition.ts";
16import { driver } from "./driver.ts";
17​
18const encoder = new TextEncoder();
19const account = OnePasswordAccount.make("my.1password.com");
20​
21type OpResult = {
22 readonly stdout?: string;
23 readonly stderr?: string;
24 readonly code?: number;
25 readonly hang?: boolean;
26};
27​
28function fakeOp(answer: (args: ReadonlyArray<string>) => OpResult) {
29 const invocations: Array<ReadonlyArray<string>> = [];
30 const spawned = Deferred.makeUnsafe<void>();
31 const layer = Layer.mergeAll(
32 Layer.succeed(
33 ChildProcessSpawner.ChildProcessSpawner,
34 ChildProcessSpawner.make((command) =>
35 Effect.sync(() => {
36 const { args } = command as unknown as { readonly args: ReadonlyArray<string> };
37 invocations.push(args);
38 Deferred.doneUnsafe(spawned, Effect.void);
39 const result = answer(args);
40 return ChildProcessSpawner.makeHandle({
41 pid: ChildProcessSpawner.ProcessId(1),
42 exitCode: result.hang
43 ? Effect.never
44 : Effect.succeed(ChildProcessSpawner.ExitCode(result.code ?? 0)),
45 isRunning: Effect.succeed(false),
46 kill: () => Effect.void,
47 unref: Effect.succeed(Effect.void),
48 stdin: Sink.drain,
49 stdout: Stream.make(encoder.encode(result.stdout ?? "")),
50 stderr: Stream.make(encoder.encode(result.stderr ?? "")),
51 all: Stream.empty,
52 getInputFd: () => Sink.drain,
53 getOutputFd: () => Stream.empty,
54 });
55 }),
56 ),
57 ),
58 NodeFileSystem.layer,
59 NodeCrypto.layer,
60 );
61 return { layer, invocations, spawned };
63​
64// Answers `op inject` by substituting each `{{ reference }}` in the template file it was handed.
65const injectFrom =
66 (secrets: Readonly<Record<string, string>>) =>
67 (args: ReadonlyArray<string>): OpResult => {
68 const template = NodeFS.readFileSync(args[args.indexOf("-i") + 1]!, "utf8");
69 return { stdout: template.replace(/\{\{ (\S+) \}\}/g, (_, reference) => secrets[reference]!) };
70 };
71​
72describe("1Password driver", () => {
73 it.effect("reads one reference from the account", () => {
74 const op = fakeOp(() => ({ stdout: "sk-live" }));
75 return Effect.gen(function* () {
76 assert.deepStrictEqual(yield* driver.read({ account }, ["op://Private/OpenAI/credential"]), [
77 "sk-live",
78 ]);
79 assert.deepStrictEqual(op.invocations, [
80 [
81 "--cache=false",
82 "read",
83 "--account=my.1password.com",
84 "--no-newline",
85 "op://Private/OpenAI/credential",
86 ],
87 ]);
88 }).pipe(Effect.provide(op.layer));
89 });
90​
91 it.effect("reads a failed reference as undefined", () => {
92 const op = fakeOp(() => ({ code: 1, stderr: "[ERROR] account is locked" }));
93 return Effect.gen(function* () {
94 assert.deepStrictEqual(yield* driver.read({ account }, ["op://Private/OpenAI/credential"]), [
95 undefined,
96 ]);
97 }).pipe(Effect.provide(op.layer));
98 });
99​
100 it.effect("reads every reference in a single call", () => {
101 const op = fakeOp(
102 injectFrom({
103 "op://Private/OpenAI/credential": "sk-a",
104 "op://Private/Anthropic/key": " sk-b\n",
105 }),
106 );
107 return Effect.gen(function* () {
108 const values = yield* driver.read({ account }, [
109 "op://Private/OpenAI/credential",
110 "op://Private/Anthropic/key",
111 ]);
112​
113 assert.deepStrictEqual(values, ["sk-a", " sk-b\n"]);
114 assert.strictEqual(op.invocations.length, 1);
115 assert.deepStrictEqual(op.invocations[0]?.slice(0, 3), [
116 "--cache=false",
117 "inject",
118 "--account=my.1password.com",
119 ]);
120 }).pipe(Effect.provide(op.layer));
121 });
122​
123 it.effect("falls back to one read at a time when the batch fails", () => {
124 const op = fakeOp((args) =>
125 args.includes("inject")
126 ? { code: 1, stderr: '[ERROR] could not resolve "op://Private/Gone/key"' }
127 : args.at(-1) === "op://Private/Gone/key"
128 ? { code: 1, stderr: "[ERROR] item not found" }
129 : { stdout: "sk-a" },
130 );
131 return Effect.gen(function* () {
132 const values = yield* driver.read({ account }, [
133 "op://Private/OpenAI/credential",
134 "op://Private/Gone/key",
135 ]);
136​
137 assert.deepStrictEqual(values, ["sk-a", undefined]);
138 assert.strictEqual(op.invocations.length, 3);
139 }).pipe(Effect.provide(op.layer));
140 });
141​
142 it.effect("does not read one at a time after the batch times out", () => {
143 const op = fakeOp(() => ({ hang: true }));
144 return Effect.gen(function* () {
145 const reading = yield* driver
146 .read({ account }, ["op://Private/OpenAI/credential", "op://Private/Anthropic/key"])
147 .pipe(Effect.forkChild);
148 yield* Deferred.await(op.spawned);
149 yield* TestClock.adjust("1 minute");
150​
151 assert.deepStrictEqual(yield* Fiber.join(reading), [undefined, undefined]);
152 assert.strictEqual(op.invocations.length, 1);
153 }).pipe(Effect.provide(op.layer));
154 });
155});