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