|
| 1 | +/* jscpd:ignore-start */ |
| 2 | +import { NodeContext } from "@effect/platform-node" |
| 3 | +import type { PlatformError } from "@effect/platform/Error" |
| 4 | +import * as FileSystem from "@effect/platform/FileSystem" |
| 5 | +import * as Path from "@effect/platform/Path" |
| 6 | +import { describe, expect, it } from "@effect/vitest" |
| 7 | +import { Effect } from "effect" |
| 8 | +import type * as Scope from "effect/Scope" |
| 9 | + |
| 10 | +import { countAuthCredentialAccounts, countCodexCredentialAccounts } from "../../src/docker-git/menu-auth-helpers.js" |
| 11 | + |
| 12 | +const withTempDir = <A, E, R>( |
| 13 | + use: (tempDir: string) => Effect.Effect<A, E, R> |
| 14 | +): Effect.Effect<A, E | PlatformError, FileSystem.FileSystem | Exclude<R, Scope.Scope>> => |
| 15 | + Effect.scoped( |
| 16 | + Effect.gen(function*(_) { |
| 17 | + const fs = yield* _(FileSystem.FileSystem) |
| 18 | + const tempDir = yield* _( |
| 19 | + fs.makeTempDirectoryScoped({ |
| 20 | + prefix: "docker-git-auth-helpers-" |
| 21 | + }) |
| 22 | + ) |
| 23 | + return yield* _(use(tempDir)) |
| 24 | + }) |
| 25 | + ) |
| 26 | + |
| 27 | +const hasMarkerCredentials = ( |
| 28 | + fs: FileSystem.FileSystem, |
| 29 | + accountPath: string |
| 30 | +): Effect.Effect<boolean, PlatformError> => |
| 31 | + fs.readFileString(`${accountPath}/.token`).pipe( |
| 32 | + Effect.map((content) => content.trim().length > 0) |
| 33 | + ) |
| 34 | + |
| 35 | +describe("menu auth helpers", () => { |
| 36 | + it.effect("counts root credentials and skips broken directory entries", () => |
| 37 | + withTempDir((root) => |
| 38 | + Effect.gen(function*(_) { |
| 39 | + const fs = yield* _(FileSystem.FileSystem) |
| 40 | + const path = yield* _(Path.Path) |
| 41 | + yield* _(fs.makeDirectory(path.join(root, "work"), { recursive: true })) |
| 42 | + yield* _(fs.writeFileString(path.join(root, ".token"), "root-token\n")) |
| 43 | + yield* _(fs.writeFileString(path.join(root, "work", ".token"), "work-token\n")) |
| 44 | + yield* _(fs.symlink(path.join(root, "missing-account"), path.join(root, "broken"))) |
| 45 | + |
| 46 | + const count = yield* _(countAuthCredentialAccounts(fs, path, root, hasMarkerCredentials)) |
| 47 | + |
| 48 | + expect(count).toBe(2) |
| 49 | + }) |
| 50 | + ).pipe(Effect.provide(NodeContext.layer))) |
| 51 | + |
| 52 | + it.effect("counts root and labeled Codex credentials", () => |
| 53 | + withTempDir((root) => |
| 54 | + Effect.gen(function*(_) { |
| 55 | + const fs = yield* _(FileSystem.FileSystem) |
| 56 | + const path = yield* _(Path.Path) |
| 57 | + yield* _(fs.makeDirectory(path.join(root, "work"), { recursive: true })) |
| 58 | + yield* _(fs.writeFileString(path.join(root, "auth.json"), "{}\n")) |
| 59 | + yield* _(fs.writeFileString(path.join(root, "work", "auth.json"), "{}\n")) |
| 60 | + yield* _(fs.symlink(path.join(root, "missing-account"), path.join(root, "broken"))) |
| 61 | + |
| 62 | + const count = yield* _(countCodexCredentialAccounts(fs, path, root)) |
| 63 | + |
| 64 | + expect(count).toBe(2) |
| 65 | + }) |
| 66 | + ).pipe(Effect.provide(NodeContext.layer))) |
| 67 | +}) |
| 68 | +/* jscpd:ignore-end */ |
0 commit comments