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

build-cli-archive.test.ts

220 lines8.4 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as ConfigProvider from "effect/ConfigProvider";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Option from "effect/Option";
7import * as Path from "effect/Path";
8import * as Sink from "effect/Sink";
9import * as Stream from "effect/Stream";
10import { ChildProcess, ChildProcessSpawner } from "effect/process";
11​
12import { awaitCliNotarization, signMacArchiveContents } from "./build-cli-archive.ts";
13​
14const signedEnv = {
15 T3CODE_CLI_MAC_SIGN_IDENTITY: "Developer ID Application: Test (TEAM)",
16 APPLE_API_KEY: "/tmp/AuthKey_TEST.p8",
17 APPLE_API_KEY_ID: "TEST",
18 APPLE_API_ISSUER: "issuer",
19};
20​
21type NotaryReply = { readonly exitCode: number; readonly stdout?: string };
22​
23/** Stands in for `xcrun notarytool`, codesign and ditto; records each invocation. */
24function stubSpawner(notary: (subcommand: string) => NotaryReply) {
25 const calls: Array<ReadonlyArray<string>> = [];
26 const spawner = ChildProcessSpawner.make((command) => {
27 const argv = ChildProcess.isStandardCommand(command)
28 ? [command.command, ...command.args.map(String)]
29 : [];
30 calls.push(argv);
31 const reply = argv[1] === "notarytool" ? notary(argv[2] ?? "") : { exitCode: 0 };
32 return Effect.succeed(
33 ChildProcessSpawner.makeHandle({
34 pid: ChildProcessSpawner.ProcessId(1),
35 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(reply.exitCode)),
36 exitStatus: Effect.succeed({
37 _tag: "Exited",
38 code: ChildProcessSpawner.ExitCode(reply.exitCode),
39 }),
40 isRunning: Effect.succeed(false),
41 kill: () => Effect.void,
42 unref: Effect.succeed(Effect.void),
43 stdin: Sink.drain,
44 stdout: reply.stdout ? Stream.make(new TextEncoder().encode(reply.stdout)) : Stream.empty,
45 stderr: Stream.empty,
46 all: Stream.empty,
47 getInputFd: () => Sink.drain,
48 getOutputFd: () => Stream.empty,
49 }),
50 );
51 });
52 return { calls, spawner };
54​
55const withStubs = <A, E, R>(
56 effect: Effect.Effect<A, E, R>,
57 stub: ReturnType<typeof stubSpawner>,
58 env: Record<string, string> = signedEnv,
59) =>
60 effect.pipe(
61 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, stub.spawner),
62 Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env }))),
63 );
64​
65const makeContent = Effect.fn("test.makeContent")(function* () {
66 const fs = yield* FileSystem.FileSystem;
67 const path = yield* Path.Path;
68 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-notary-" });
69 const contentDir = path.join(root, "content");
70 yield* fs.makeDirectory(contentDir);
71 const executablePath = path.join(contentDir, "t3");
72 yield* fs.writeFileString(executablePath, "t3");
73 return { root, contentDir, executablePath, submissionFile: path.join(root, "submission") };
74});
75​
76const notarySubcommands = (calls: ReadonlyArray<ReadonlyArray<string>>) =>
77 calls.filter((argv) => argv[1] === "notarytool").map((argv) => argv.slice(2));
78​
79it.layer(NodeServices.layer)("build-cli-archive notarization", (it) => {
80 it.effect("records a deferred submission without waiting, then gates on Accepted", () =>
81 Effect.gen(function* () {
82 const fs = yield* FileSystem.FileSystem;
83 // Apple documents id and path for an unwaited submit; a pending status must not fail it.
84 for (const submitted of [
85 '{"id":"new-id","message":"Successfully uploaded file","path":"t3.zip"}',
86 '{"id":"new-id","status":"In Progress"}',
87 ]) {
88 const content = yield* makeContent();
89 yield* fs.writeFileString(content.submissionFile, "stale-id");
90 const stub = stubSpawner((subcommand) =>
91 subcommand === "submit"
92 ? { exitCode: 0, stdout: submitted }
93 : { exitCode: 0, stdout: '{"id":"new-id","status":"Accepted"}' },
94 );
95​
96 yield* withStubs(
97 signMacArchiveContents({
98 repoRoot: content.root,
99 contentDir: content.contentDir,
100 executablePath: content.executablePath,
101 notarySubmissionFile: Option.some(content.submissionFile),
102 }),
103 stub,
104 );
105 assert.equal(yield* fs.readFileString(content.submissionFile), "new-id");
106 const [submit] = notarySubcommands(stub.calls);
107 assert.equal(submit?.[0], "submit");
108 assert.isFalse(submit?.includes("--wait"));
109​
110 yield* withStubs(awaitCliNotarization(content.submissionFile), stub);
111 assert.deepEqual(notarySubcommands(stub.calls)[1]?.slice(0, 2), ["wait", "new-id"]);
112 }
113 }).pipe(Effect.scoped),
114 );
115​
116 it.effect("fails the gate unless Apple accepted the submission", () =>
117 Effect.gen(function* () {
118 const fs = yield* FileSystem.FileSystem;
119 const cases: ReadonlyArray<{
120 readonly name: string;
121 readonly wait: NotaryReply;
122 readonly submitted?: boolean;
123 readonly env?: Record<string, string>;
124 readonly error: string | undefined;
125 readonly fetchesLog?: boolean;
126 }> = [
127 {
128 name: "invalid",
129 wait: { exitCode: 0, stdout: '{"id":"sub","status":"Invalid"}' },
130 error: "CliArchiveNotarizationRejectedError",
131 fetchesLog: true,
132 },
133 {
134 name: "rejected with a non-zero exit",
135 wait: { exitCode: 1, stdout: '{"id":"sub","status":"Rejected"}' },
136 error: "CliArchiveNotarizationRejectedError",
137 fetchesLog: true,
138 },
139 {
140 name: "still in progress",
141 wait: { exitCode: 0, stdout: '{"id":"sub","status":"In Progress"}' },
142 error: "CliArchiveNotarizationRejectedError",
143 },
144 {
145 name: "still in progress with a non-zero exit",
146 wait: { exitCode: 1, stdout: '{"id":"sub","status":"In Progress"}' },
147 error: "CliArchiveCommandFailedError",
148 },
149 {
150 name: "notarytool failed",
151 wait: { exitCode: 69 },
152 error: "CliArchiveCommandFailedError",
153 },
154 {
155 name: "no status",
156 wait: { exitCode: 0, stdout: '{"id":"sub"}' },
157 error: "CliArchiveNotarizationRejectedError",
158 },
159 {
160 name: "accepted with a non-zero exit",
161 wait: { exitCode: 1, stdout: '{"id":"sub","status":"Accepted"}' },
162 error: "CliArchiveCommandFailedError",
163 },
164 {
165 name: "submission missing",
166 wait: { exitCode: 0, stdout: '{"id":"sub","status":"Accepted"}' },
167 submitted: false,
168 error: "CliArchiveInputMissingError",
169 },
170 {
171 name: "submission without credentials",
172 wait: { exitCode: 0, stdout: '{"id":"sub","status":"Accepted"}' },
173 env: { T3CODE_CLI_MAC_SIGN_IDENTITY: signedEnv.T3CODE_CLI_MAC_SIGN_IDENTITY },
174 error: "CliArchiveInputMissingError",
175 },
176 {
177 name: "notarization not configured",
178 wait: { exitCode: 0 },
179 submitted: false,
180 env: {},
181 error: undefined,
182 },
183 ];
184 for (const testCase of cases) {
185 const content = yield* makeContent();
186 if (testCase.submitted !== false) yield* fs.writeFileString(content.submissionFile, "sub");
187 const stub = stubSpawner(() => testCase.wait);
188 const exit = yield* withStubs(
189 awaitCliNotarization(content.submissionFile),
190 stub,
191 testCase.env,
192 ).pipe(Effect.flip, Effect.option);
193 assert.equal(Option.getOrUndefined(exit)?._tag, testCase.error, testCase.name);
194 assert.equal(
195 notarySubcommands(stub.calls).some((argv) => argv[0] === "log"),
196 testCase.fetchesLog ?? false,
197 testCase.name,
198 );
199 }
200 }).pipe(Effect.scoped),
201 );
202​
203 it.effect("fails a standalone build whose waited submission is not Accepted", () =>
204 Effect.gen(function* () {
205 const content = yield* makeContent();
206 const stub = stubSpawner(() => ({ exitCode: 0, stdout: '{"id":"sub","status":"Invalid"}' }));
207 const error = yield* withStubs(
208 signMacArchiveContents({
209 repoRoot: content.root,
210 contentDir: content.contentDir,
211 executablePath: content.executablePath,
212 notarySubmissionFile: Option.none(),
213 }),
214 stub,
215 ).pipe(Effect.flip);
216 assert.equal(error._tag, "CliArchiveNotarizationRejectedError");
217 assert.isTrue(notarySubcommands(stub.calls)[0]?.includes("--wait"));
218 }).pipe(Effect.scoped),
219 );
220});