|
| 1 | +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 5 | +import { |
| 6 | + type CommandRunner, |
| 7 | + DEFAULT_TRANSITOUS_REPO_URL, |
| 8 | + generateTransitousApiKeys, |
| 9 | +} from "../src/lib/transitous-api-keys"; |
| 10 | + |
| 11 | +let tmp: string; |
| 12 | + |
| 13 | +function writeTransitousCatalog(catalogDir: string): void { |
| 14 | + mkdirSync(join(catalogDir, "feeds"), { recursive: true }); |
| 15 | + mkdirSync(join(catalogDir, "transitland-atlas", "feeds", "nested"), { recursive: true }); |
| 16 | + |
| 17 | + writeFileSync( |
| 18 | + join(catalogDir, "feeds", "us-ca.json"), |
| 19 | + JSON.stringify({ |
| 20 | + sources: [ |
| 21 | + { name: "NeedsKey", "transitland-atlas-id": "atlas-auth-1" }, |
| 22 | + { name: "NoAuthNeeded", "transitland-atlas-id": "atlas-open" }, |
| 23 | + { name: "AlreadyInCatalog", "transitland-atlas-id": "atlas-auth-1", "api-key": "inline" }, |
| 24 | + { name: "CustomUrl", "transitland-atlas-id": "atlas-auth-1", "url-override": "https://x" }, |
| 25 | + ], |
| 26 | + }), |
| 27 | + "utf-8", |
| 28 | + ); |
| 29 | + writeFileSync( |
| 30 | + join(catalogDir, "feeds", "ca-qc.json"), |
| 31 | + JSON.stringify({ |
| 32 | + sources: [{ name: "QuebecAgency", "transitland-atlas-id": "atlas-auth-2", skip: true }], |
| 33 | + }), |
| 34 | + "utf-8", |
| 35 | + ); |
| 36 | + writeFileSync( |
| 37 | + join(catalogDir, "transitland-atlas", "feeds", "nested", "agencies.dmfr.json"), |
| 38 | + JSON.stringify({ |
| 39 | + feeds: [ |
| 40 | + { id: "atlas-auth-1", authorization: { type: "api-key" } }, |
| 41 | + { id: "atlas-auth-2", authorization: { type: "token" } }, |
| 42 | + { id: "atlas-open" }, |
| 43 | + ], |
| 44 | + }), |
| 45 | + "utf-8", |
| 46 | + ); |
| 47 | +} |
| 48 | + |
| 49 | +beforeEach(() => { |
| 50 | + tmp = mkdtempSync(join(tmpdir(), "openmapx-transitous-api-keys-")); |
| 51 | + writeFileSync(join(tmp, "pnpm-workspace.yaml"), "packages: []\n"); |
| 52 | + mkdirSync(join(tmp, "services"), { recursive: true }); |
| 53 | + mkdirSync(join(tmp, "infra", "docker", "services", "transitous"), { recursive: true }); |
| 54 | +}); |
| 55 | + |
| 56 | +afterEach(() => { |
| 57 | + rmSync(tmp, { recursive: true, force: true }); |
| 58 | +}); |
| 59 | + |
| 60 | +describe("generateTransitousApiKeys", () => { |
| 61 | + it("clones the Transitous catalog and preserves existing filled keys", async () => { |
| 62 | + const outputPath = join(tmp, "infra", "docker", "services", "transitous", "api-keys.json"); |
| 63 | + writeFileSync( |
| 64 | + outputPath, |
| 65 | + JSON.stringify( |
| 66 | + { |
| 67 | + "us-ca/NeedsKey": "preserved-secret", |
| 68 | + "legacy/no-longer-needed": "stale-value", |
| 69 | + }, |
| 70 | + null, |
| 71 | + 2, |
| 72 | + ), |
| 73 | + "utf-8", |
| 74 | + ); |
| 75 | + |
| 76 | + const calls: Array<{ command: string; args: string[]; cwd?: string }> = []; |
| 77 | + const runner: CommandRunner = async (command, args, opts) => { |
| 78 | + calls.push({ command, args, cwd: opts.cwd }); |
| 79 | + if (command === "git" && args[0] === "clone") { |
| 80 | + const targetDir = args.at(-1); |
| 81 | + if (typeof targetDir === "string") { |
| 82 | + writeTransitousCatalog(targetDir); |
| 83 | + } |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + const result = await generateTransitousApiKeys({ rootDir: tmp, runner }); |
| 88 | + |
| 89 | + expect(result.requiredCount).toBe(2); |
| 90 | + expect(result.preservedCount).toBe(1); |
| 91 | + expect(result.droppedCount).toBe(1); |
| 92 | + expect(result.outputPath).toBe(outputPath); |
| 93 | + expect(JSON.parse(readFileSync(outputPath, "utf-8")) as Record<string, string>).toStrictEqual({ |
| 94 | + "ca-qc/QuebecAgency": "", |
| 95 | + "us-ca/NeedsKey": "preserved-secret", |
| 96 | + }); |
| 97 | + expect(calls).toEqual([ |
| 98 | + { |
| 99 | + command: "git", |
| 100 | + args: [ |
| 101 | + "clone", |
| 102 | + "--depth", |
| 103 | + "1", |
| 104 | + "--recurse-submodules", |
| 105 | + "--shallow-submodules", |
| 106 | + DEFAULT_TRANSITOUS_REPO_URL, |
| 107 | + join(tmp, "infra", "docker", "data", ".transitous-catalog"), |
| 108 | + ], |
| 109 | + cwd: join(tmp, "infra", "docker", "data"), |
| 110 | + }, |
| 111 | + ]); |
| 112 | + }); |
| 113 | + |
| 114 | + it("uses an existing cached catalog and continues when git pull fails", async () => { |
| 115 | + const catalogDir = join(tmp, "infra", "docker", "data", ".transitous-catalog"); |
| 116 | + mkdirSync(join(catalogDir, ".git"), { recursive: true }); |
| 117 | + writeTransitousCatalog(catalogDir); |
| 118 | + |
| 119 | + const calls: Array<{ command: string; args: string[]; cwd?: string }> = []; |
| 120 | + const runner: CommandRunner = async (command, args, opts) => { |
| 121 | + calls.push({ command, args, cwd: opts.cwd }); |
| 122 | + if (command === "git" && args[2] === "pull") { |
| 123 | + throw new Error("network unavailable"); |
| 124 | + } |
| 125 | + }; |
| 126 | + |
| 127 | + const result = await generateTransitousApiKeys({ rootDir: tmp, runner }); |
| 128 | + |
| 129 | + expect(result.requiredCount).toBe(2); |
| 130 | + expect(result.preservedCount).toBe(0); |
| 131 | + expect(result.droppedCount).toBe(0); |
| 132 | + const outputPath = join(tmp, "infra", "docker", "services", "transitous", "api-keys.json"); |
| 133 | + expect(JSON.parse(readFileSync(outputPath, "utf-8")) as Record<string, string>).toStrictEqual({ |
| 134 | + "ca-qc/QuebecAgency": "", |
| 135 | + "us-ca/NeedsKey": "", |
| 136 | + }); |
| 137 | + expect(calls).toEqual([ |
| 138 | + { |
| 139 | + command: "git", |
| 140 | + args: ["-C", catalogDir, "reset", "--hard", "HEAD"], |
| 141 | + cwd: catalogDir, |
| 142 | + }, |
| 143 | + { |
| 144 | + command: "git", |
| 145 | + args: ["-C", catalogDir, "pull", "--ff-only"], |
| 146 | + cwd: join(tmp, "infra", "docker", "data"), |
| 147 | + }, |
| 148 | + { |
| 149 | + command: "git", |
| 150 | + args: ["-C", catalogDir, "submodule", "update", "--init", "--checkout", "--depth", "1"], |
| 151 | + cwd: join(tmp, "infra", "docker", "data"), |
| 152 | + }, |
| 153 | + ]); |
| 154 | + }); |
| 155 | +}); |
0 commit comments