|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "@effect/vitest"; |
| 2 | +import { |
| 3 | + existsSync, |
| 4 | + mkdirSync, |
| 5 | + mkdtempSync, |
| 6 | + readFileSync, |
| 7 | + readdirSync, |
| 8 | + rmSync, |
| 9 | + statSync, |
| 10 | + writeFileSync, |
| 11 | +} from "node:fs"; |
| 12 | +import { tmpdir } from "node:os"; |
| 13 | +import { dirname, join } from "node:path"; |
| 14 | +import { Effect, Predicate, Result } from "effect"; |
| 15 | + |
| 16 | +import { ProviderKey } from "@executor-js/sdk"; |
| 17 | +import { makeTestWorkspaceHarness } from "@executor-js/sdk/testing"; |
| 18 | + |
| 19 | +import { fileSecretsPlugin } from "./index"; |
| 20 | + |
| 21 | +const FILE_PROVIDER = ProviderKey.make("file"); |
| 22 | + |
| 23 | +const inspectPlugin = (plugin: ReturnType<typeof fileSecretsPlugin>) => |
| 24 | + Effect.scoped( |
| 25 | + Effect.gen(function* () { |
| 26 | + const workspace = yield* makeTestWorkspaceHarness({ plugins: [plugin] as const }); |
| 27 | + const items = yield* workspace.executor.providers.items(FILE_PROVIDER); |
| 28 | + return { |
| 29 | + filePath: workspace.executor.fileSecrets.filePath, |
| 30 | + itemIds: items.map((item) => String(item.id)), |
| 31 | + }; |
| 32 | + }), |
| 33 | + ); |
| 34 | + |
| 35 | +const writeAuthFile = (filePath: string, contents: string, mode = 0o600): void => { |
| 36 | + mkdirSync(dirname(filePath), { recursive: true }); |
| 37 | + writeFileSync(filePath, contents, { mode }); |
| 38 | +}; |
| 39 | + |
| 40 | +describe("file secrets auth location", () => { |
| 41 | + let workDir: string; |
| 42 | + let dataDir: string; |
| 43 | + let otherDataDir: string; |
| 44 | + let xdgDataHome: string; |
| 45 | + let overrideDir: string; |
| 46 | + let legacyFilePath: string; |
| 47 | + |
| 48 | + beforeEach(() => { |
| 49 | + workDir = mkdtempSync(join(tmpdir(), "executor-file-secrets-location-")); |
| 50 | + dataDir = join(workDir, "data"); |
| 51 | + otherDataDir = join(workDir, "other-data"); |
| 52 | + xdgDataHome = join(workDir, "xdg"); |
| 53 | + overrideDir = join(workDir, "override"); |
| 54 | + legacyFilePath = join(xdgDataHome, "executor", "auth.json"); |
| 55 | + vi.stubEnv("XDG_DATA_HOME", xdgDataHome); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + vi.unstubAllEnvs(); |
| 60 | + rmSync(workDir, { recursive: true, force: true }); |
| 61 | + }); |
| 62 | + |
| 63 | + it.effect("uses auth.json directly under EXECUTOR_DATA_DIR resolved at construction", () => |
| 64 | + Effect.gen(function* () { |
| 65 | + vi.stubEnv("EXECUTOR_DATA_DIR", dataDir); |
| 66 | + const plugin = fileSecretsPlugin(); |
| 67 | + vi.stubEnv("EXECUTOR_DATA_DIR", otherDataDir); |
| 68 | + |
| 69 | + const inspected = yield* inspectPlugin(plugin); |
| 70 | + |
| 71 | + expect(inspected.filePath).toBe(join(dataDir, "auth.json")); |
| 72 | + }), |
| 73 | + ); |
| 74 | + |
| 75 | + it.effect("keeps the XDG location when EXECUTOR_DATA_DIR is unset", () => |
| 76 | + Effect.gen(function* () { |
| 77 | + vi.stubEnv("EXECUTOR_DATA_DIR", ""); |
| 78 | + |
| 79 | + const inspected = yield* inspectPlugin(fileSecretsPlugin()); |
| 80 | + |
| 81 | + expect(inspected.filePath).toBe(legacyFilePath); |
| 82 | + }), |
| 83 | + ); |
| 84 | + |
| 85 | + it.effect("gives an explicit directory precedence without migration", () => |
| 86 | + Effect.gen(function* () { |
| 87 | + vi.stubEnv("EXECUTOR_DATA_DIR", dataDir); |
| 88 | + writeAuthFile(legacyFilePath, '{"legacy":"legacy-secret"}'); |
| 89 | + |
| 90 | + const inspected = yield* inspectPlugin(fileSecretsPlugin({ directory: overrideDir })); |
| 91 | + |
| 92 | + expect(inspected.filePath).toBe(join(overrideDir, "auth.json")); |
| 93 | + expect(inspected.itemIds).toEqual([]); |
| 94 | + expect(existsSync(join(dataDir, "auth.json"))).toBe(false); |
| 95 | + expect(readFileSync(legacyFilePath, "utf8")).toBe('{"legacy":"legacy-secret"}'); |
| 96 | + }), |
| 97 | + ); |
| 98 | + |
| 99 | + it.effect("copies a valid legacy XDG file once and preserves 0600 permissions", () => |
| 100 | + Effect.gen(function* () { |
| 101 | + vi.stubEnv("EXECUTOR_DATA_DIR", dataDir); |
| 102 | + const legacyContents = '{"legacy-token":"legacy-secret"}'; |
| 103 | + writeAuthFile(legacyFilePath, legacyContents, 0o644); |
| 104 | + |
| 105 | + const inspected = yield* inspectPlugin(fileSecretsPlugin()); |
| 106 | + const migratedFilePath = join(dataDir, "auth.json"); |
| 107 | + |
| 108 | + expect(inspected.filePath).toBe(migratedFilePath); |
| 109 | + expect(inspected.itemIds).toEqual(["legacy-token"]); |
| 110 | + expect(readFileSync(migratedFilePath, "utf8")).toContain('"legacy-token": "legacy-secret"'); |
| 111 | + expect(statSync(migratedFilePath).mode & 0o777).toBe(0o600); |
| 112 | + expect(readFileSync(legacyFilePath, "utf8")).toBe(legacyContents); |
| 113 | + }), |
| 114 | + ); |
| 115 | + |
| 116 | + it.effect("uses an existing data-dir file without merging the legacy file", () => |
| 117 | + Effect.gen(function* () { |
| 118 | + vi.stubEnv("EXECUTOR_DATA_DIR", dataDir); |
| 119 | + const activeFilePath = join(dataDir, "auth.json"); |
| 120 | + const activeContents = '{"active-token":"active-secret"}'; |
| 121 | + const legacyContents = '{"legacy-token":"legacy-secret"}'; |
| 122 | + writeAuthFile(activeFilePath, activeContents); |
| 123 | + writeAuthFile(legacyFilePath, legacyContents); |
| 124 | + |
| 125 | + const inspected = yield* inspectPlugin(fileSecretsPlugin()); |
| 126 | + |
| 127 | + expect(inspected.itemIds).toEqual(["active-token"]); |
| 128 | + expect(readFileSync(activeFilePath, "utf8")).toBe(activeContents); |
| 129 | + expect(readFileSync(legacyFilePath, "utf8")).toBe(legacyContents); |
| 130 | + }), |
| 131 | + ); |
| 132 | + |
| 133 | + it.effect("leaves the new store empty when the legacy file is corrupt", () => |
| 134 | + Effect.scoped( |
| 135 | + Effect.gen(function* () { |
| 136 | + vi.stubEnv("EXECUTOR_DATA_DIR", dataDir); |
| 137 | + writeAuthFile(legacyFilePath, "not-json"); |
| 138 | + const workspace = yield* makeTestWorkspaceHarness({ |
| 139 | + plugins: [fileSecretsPlugin()] as const, |
| 140 | + }); |
| 141 | + |
| 142 | + const initial = yield* workspace.executor.providers.items(FILE_PROVIDER); |
| 143 | + expect(initial).toEqual([]); |
| 144 | + expect(existsSync(join(dataDir, "auth.json"))).toBe(false); |
| 145 | + expect(readFileSync(legacyFilePath, "utf8")).toBe("not-json"); |
| 146 | + |
| 147 | + writeAuthFile(legacyFilePath, '{"repaired-token":"repaired-secret"}'); |
| 148 | + const afterRepair = yield* workspace.executor.providers.items(FILE_PROVIDER); |
| 149 | + expect(afterRepair).toEqual([]); |
| 150 | + expect(existsSync(join(dataDir, "auth.json"))).toBe(false); |
| 151 | + }), |
| 152 | + ), |
| 153 | + ); |
| 154 | + |
| 155 | + it.effect("retries migration after a legacy read I/O failure", () => |
| 156 | + Effect.scoped( |
| 157 | + Effect.gen(function* () { |
| 158 | + vi.stubEnv("EXECUTOR_DATA_DIR", dataDir); |
| 159 | + mkdirSync(legacyFilePath, { recursive: true }); |
| 160 | + const workspace = yield* makeTestWorkspaceHarness({ |
| 161 | + plugins: [fileSecretsPlugin()] as const, |
| 162 | + }); |
| 163 | + |
| 164 | + const failed = yield* Effect.result(workspace.executor.providers.items(FILE_PROVIDER)); |
| 165 | + expect(Result.isFailure(failed)).toBe(true); |
| 166 | + if (!Result.isFailure(failed)) return; |
| 167 | + expect(Predicate.isTagged("StorageError")(failed.failure)).toBe(true); |
| 168 | + |
| 169 | + rmSync(legacyFilePath, { recursive: true, force: true }); |
| 170 | + writeAuthFile(legacyFilePath, '{"recovered-token":"recovered-secret"}'); |
| 171 | + |
| 172 | + const recovered = yield* workspace.executor.providers.items(FILE_PROVIDER); |
| 173 | + expect(recovered.map((item) => String(item.id))).toEqual(["recovered-token"]); |
| 174 | + expect(readFileSync(join(dataDir, "auth.json"), "utf8")).toContain( |
| 175 | + '"recovered-token": "recovered-secret"', |
| 176 | + ); |
| 177 | + }), |
| 178 | + ), |
| 179 | + ); |
| 180 | + |
| 181 | + it.effect("shares one migration across concurrent first provider operations", () => |
| 182 | + Effect.scoped( |
| 183 | + Effect.gen(function* () { |
| 184 | + vi.stubEnv("EXECUTOR_DATA_DIR", dataDir); |
| 185 | + const legacyContents = '{"legacy-token":"legacy-secret"}'; |
| 186 | + writeAuthFile(legacyFilePath, legacyContents); |
| 187 | + const workspace = yield* makeTestWorkspaceHarness({ |
| 188 | + plugins: [fileSecretsPlugin()] as const, |
| 189 | + }); |
| 190 | + |
| 191 | + const results = yield* Effect.all( |
| 192 | + [ |
| 193 | + workspace.executor.providers.items(FILE_PROVIDER), |
| 194 | + workspace.executor.providers.items(FILE_PROVIDER), |
| 195 | + ], |
| 196 | + { concurrency: "unbounded" }, |
| 197 | + ); |
| 198 | + |
| 199 | + expect(results.map((items) => items.map((item) => String(item.id)))).toEqual([ |
| 200 | + ["legacy-token"], |
| 201 | + ["legacy-token"], |
| 202 | + ]); |
| 203 | + expect(readdirSync(dataDir)).toEqual(["auth.json"]); |
| 204 | + expect(readFileSync(legacyFilePath, "utf8")).toBe(legacyContents); |
| 205 | + }), |
| 206 | + ), |
| 207 | + ); |
| 208 | +}); |
0 commit comments