Showing the changes to apps/server/src/sourceControl/SourceControlDiscovery.test.ts (1 of 13 files). Show all
Unified · Split
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | import { assert, it } from "@effect/vitest"; |
| 2 | 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | −import { Effect, Layer } from "effect"; | |
| 3 | +import { Effect, Layer, Option } from "effect"; | |
| 4 | 4 | import { ChildProcessSpawner } from "effect/unstable/process"; |
| 5 | 5 | import { VcsProcessSpawnError } from "@t3tools/contracts"; |
| 6 | 6 | |
| @@ -8,10 +8,16 @@ import { ServerConfig } from "../config.ts"; · 1 unmodified line | ||
| 8 | 8 | import * as VcsProcess from "../vcs/VcsProcess.ts"; |
| 9 | 9 | import { SourceControlDiscovery, layer } from "./SourceControlDiscovery.ts"; |
| 10 | 10 | |
| 11 | −const processOutput = (stdout: string): VcsProcess.VcsProcessOutput => ({ | |
| 12 | − exitCode: ChildProcessSpawner.ExitCode(0), | |
| 11 | +const processOutput = ( | |
| 12 | + stdout: string, | |
| 13 | + options?: { | |
| 14 | + readonly stderr?: string; | |
| 15 | + readonly exitCode?: ChildProcessSpawner.ExitCode; | |
| 16 | + }, | |
| 17 | +): VcsProcess.VcsProcessOutput => ({ | |
| 18 | + exitCode: options?.exitCode ?? ChildProcessSpawner.ExitCode(0), | |
| 13 | 19 | stdout, |
| 14 | − stderr: "", | |
| 20 | + stderr: options?.stderr ?? "", | |
| 15 | 21 | stdoutTruncated: false, |
| 16 | 22 | stderrTruncated: false, |
| 17 | 23 | }); |
| @@ -27,9 +33,20 @@ it.effect("reports implemented tools separately from locally available CLIs", () · 9 unmodified lines | ||
| 27 | 33 | if (input.command === "git") { |
| 28 | 34 | return Effect.succeed(processOutput("git version 2.51.0\n")); |
| 29 | 35 | } |
| 30 | − if (input.command === "gh") { | |
| 36 | + if (input.command === "gh" && input.args[0] === "--version") { | |
| 31 | 37 | return Effect.succeed(processOutput("gh version 2.83.0\n")); |
| 32 | 38 | } |
| 39 | + if (input.command === "gh" && input.args.join(" ") === "auth status") { | |
| 40 | + return Effect.succeed( | |
| 41 | + processOutput(`github.com | |
| 42 | +Logged in to github.com account juliusmarminge (keyring) | |
| 43 | +- Active account: true | |
| 44 | +- Git operations protocol: ssh | |
| 45 | +- Token: gho_************************************ | |
| 46 | +- Token scopes: 'admin:public_key', 'gist', 'read:org', 'repo' | |
| 47 | +`), | |
| 48 | + ); | |
| 49 | + } | |
| 33 | 50 | return Effect.fail( |
| 34 | 51 | new VcsProcessSpawnError({ |
| 35 | 52 | operation: input.operation, |
| @@ -65,12 +82,133 @@ it.effect("reports implemented tools separately from locally available CLIs", () · 29 unmodified lines | ||
| 65 | 82 | kind: item.kind, |
| 66 | 83 | implemented: item.implemented, |
| 67 | 84 | status: item.status, |
| 85 | + auth: item.auth.status, | |
| 86 | + account: item.auth.account, | |
| 87 | + })), | |
| 88 | + [ | |
| 89 | + { | |
| 90 | + kind: "github", | |
| 91 | + implemented: true, | |
| 92 | + status: "available", | |
| 93 | + auth: "authenticated", | |
| 94 | + account: Option.some("juliusmarminge"), | |
| 95 | + }, | |
| 96 | + { | |
| 97 | + kind: "gitlab", | |
| 98 | + implemented: false, | |
| 99 | + status: "missing", | |
| 100 | + auth: "unknown", | |
| 101 | + account: Option.none(), | |
| 102 | + }, | |
| 103 | + { | |
| 104 | + kind: "azure-devops", | |
| 105 | + implemented: false, | |
| 106 | + status: "missing", | |
| 107 | + auth: "unknown", | |
| 108 | + account: Option.none(), | |
| 109 | + }, | |
| 110 | + { | |
| 111 | + kind: "bitbucket", | |
| 112 | + implemented: false, | |
| 113 | + status: "missing", | |
| 114 | + auth: "unknown", | |
| 115 | + account: Option.none(), | |
| 116 | + }, | |
| 117 | + ], | |
| 118 | + ); | |
| 119 | + }).pipe(Effect.provide(testLayer)); | |
| 120 | +}); | |
| 121 | + | |
| 122 | +it.effect("probes provider authentication without exposing token details", () => { | |
| 123 | + const testLayer = layer.pipe( | |
| 124 | + Layer.provide( | |
| 125 | + ServerConfig.layerTest(process.cwd(), { prefix: "t3-source-control-auth-discovery-" }), | |
| 126 | + ), | |
| 127 | + Layer.provide( | |
| 128 | + Layer.mock(VcsProcess.VcsProcess)({ | |
| 129 | + run: (input) => { | |
| 130 | + if (input.args[0] === "--version") { | |
| 131 | + return Effect.succeed(processOutput(`${input.command} version test\n`)); | |
| 132 | + } | |
| 133 | + if (input.command === "gh" && input.args.join(" ") === "auth status") { | |
| 134 | + return Effect.succeed( | |
| 135 | + processOutput(`github.com | |
| 136 | +Logged in to github.com account octocat (keyring) | |
| 137 | +- Token: gho_************************************ | |
| 138 | +- Token scopes: 'repo' | |
| 139 | +`), | |
| 140 | + ); | |
| 141 | + } | |
| 142 | + if (input.command === "glab" && input.args.join(" ") === "auth status") { | |
| 143 | + return Effect.succeed( | |
| 144 | + processOutput(`gitlab.com | |
| 145 | +Logged in to gitlab.com as gitlab-user | |
| 146 | +`), | |
| 147 | + ); | |
| 148 | + } | |
| 149 | + if ( | |
| 150 | + input.command === "az" && | |
| 151 | + input.args.join(" ") === "account show --query user.name -o tsv" | |
| 152 | + ) { | |
| 153 | + return Effect.succeed(processOutput("azure-user@example.com\n")); | |
| 154 | + } | |
| 155 | + if (input.command === "bb" && input.args.join(" ") === "auth status") { | |
| 156 | + return Effect.succeed( | |
| 157 | + processOutput(`bitbucket.org | |
| 158 | +Logged in as bitbucket-user | |
| 159 | +`), | |
| 160 | + ); | |
| 161 | + } | |
| 162 | + return Effect.fail( | |
| 163 | + new VcsProcessSpawnError({ | |
| 164 | + operation: input.operation, | |
| 165 | + command: input.command, | |
| 166 | + cwd: input.cwd, | |
| 167 | + cause: new Error(`${input.command} not found`), | |
| 168 | + }), | |
| 169 | + ); | |
| 170 | + }, | |
| 171 | + }), | |
| 172 | + ), | |
| 173 | + Layer.provideMerge(NodeServices.layer), | |
| 174 | + ); | |
| 175 | + | |
| 176 | + return Effect.gen(function* () { | |
| 177 | + const discovery = yield* SourceControlDiscovery; | |
| 178 | + const result = yield* discovery.discover; | |
| 179 | + | |
| 180 | + assert.deepStrictEqual( | |
| 181 | + result.sourceControlProviders.map((item) => ({ | |
| 182 | + kind: item.kind, | |
| 183 | + auth: item.auth.status, | |
| 184 | + account: item.auth.account, | |
| 185 | + detail: item.auth.detail, | |
| 68 | 186 | })), |
| 69 | 187 | [ |
| 70 | − { kind: "github", implemented: true, status: "available" }, | |
| 71 | − { kind: "gitlab", implemented: false, status: "missing" }, | |
| 72 | − { kind: "azure-devops", implemented: false, status: "missing" }, | |
| 73 | − { kind: "bitbucket", implemented: false, status: "missing" }, | |
| 188 | + { | |
| 189 | + kind: "github", | |
| 190 | + auth: "authenticated", | |
| 191 | + account: Option.some("octocat"), | |
| 192 | + detail: Option.none(), | |
| 193 | + }, | |
| 194 | + { | |
| 195 | + kind: "gitlab", | |
| 196 | + auth: "authenticated", | |
| 197 | + account: Option.some("gitlab-user"), | |
| 198 | + detail: Option.none(), | |
| 199 | + }, | |
| 200 | + { | |
| 201 | + kind: "azure-devops", | |
| 202 | + auth: "authenticated", | |
| 203 | + account: Option.some("azure-user@example.com"), | |
| 204 | + detail: Option.none(), | |
| 205 | + }, | |
| 206 | + { | |
| 207 | + kind: "bitbucket", | |
| 208 | + auth: "authenticated", | |
| 209 | + account: Option.some("bitbucket-user"), | |
| 210 | + detail: Option.none(), | |
| 211 | + }, | |
| 74 | 212 | ], |
| 75 | 213 | ); |
| 76 | 214 | }).pipe(Effect.provide(testLayer)); |