The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics nodeBuiltinImport:off |
| 2 | import * as NodeFS from "node:fs"; |
| 3 | import * as NodeOS from "node:os"; |
| 4 | import * as NodePath from "node:path"; |
| 5 | |
| 6 | import type { GitCommandError } from "@t3tools/contracts"; |
| 7 | |
| 8 | import { assert, it } from "@effect/vitest"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import * as Option from "effect/Option"; |
| 12 | import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; |
| 13 | import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; |
| 14 | |
| 15 | import * as AzureDevOpsCli from "./AzureDevOpsCli.ts"; |
| 16 | import * as AzureDevOpsSourceControlProvider from "./AzureDevOpsSourceControlProvider.ts"; |
| 17 | |
| 18 | const withRepository = <A, E>( |
| 19 | test: (input: { |
| 20 | readonly git: (...args: string[]) => Effect.Effect<string, GitCommandError>; |
| 21 | readonly write: (path: string, contents: string) => Effect.Effect<void>; |
| 22 | readonly commit: Effect.Effect<void, GitCommandError>; |
| 23 | readonly readTemplate: (treeish?: string) => Effect.Effect<Option.Option<string>>; |
| 24 | }) => Effect.Effect<A, E>, |
| 25 | ) => |
| 26 | Effect.gen(function* () { |
| 27 | const host = yield* SourceControlHost.SourceControlHost; |
| 28 | const cwd = yield* Effect.acquireRelease( |
| 29 | Effect.sync(() => NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-azure-template-"))), |
| 30 | (directory) => Effect.sync(() => NodeFS.rmSync(directory, { recursive: true, force: true })), |
| 31 | ); |
| 32 | const git = (...args: string[]) => |
| 33 | host.git |
| 34 | .execute({ operation: "AzureDevOpsTemplate.test", cwd, args }) |
| 35 | .pipe(Effect.map((result) => result.stdout.trim())); |
| 36 | const write = (path: string, contents: string) => |
| 37 | Effect.sync(() => { |
| 38 | NodeFS.mkdirSync(NodePath.dirname(NodePath.join(cwd, path)), { recursive: true }); |
| 39 | NodeFS.writeFileSync(NodePath.join(cwd, path), contents); |
| 40 | }); |
| 41 | const commit = Effect.gen(function* () { |
| 42 | yield* git("add", "-A"); |
| 43 | yield* git("commit", "--allow-empty", "-m", "Add pull request templates"); |
| 44 | }); |
| 45 | yield* git("init", "--initial-branch=main"); |
| 46 | yield* git("config", "user.email", "test@example.com"); |
| 47 | yield* git("config", "user.name", "Test User"); |
| 48 | yield* git("config", "core.autocrlf", "false"); |
| 49 | yield* git("remote", "add", "origin", cwd); |
| 50 | const provider = yield* AzureDevOpsSourceControlProvider.make.pipe( |
| 51 | Effect.provide(Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({})), |
| 52 | ); |
| 53 | return yield* test({ |
| 54 | git, |
| 55 | write, |
| 56 | commit, |
| 57 | readTemplate: (treeish = "main") => provider.readChangeRequestTemplate!({ cwd, treeish }), |
| 58 | }); |
| 59 | }).pipe( |
| 60 | Effect.scoped, |
| 61 | Effect.provide( |
| 62 | TestSourceControlHost.layerWithRealGit({ |
| 63 | git: { resolvePrimaryRemoteName: () => Effect.succeed("origin") }, |
| 64 | }), |
| 65 | ), |
| 66 | ); |
| 67 | |
| 68 | const publishMain = (git: (...args: string[]) => Effect.Effect<string, GitCommandError>) => |
| 69 | Effect.gen(function* () { |
| 70 | yield* git("update-ref", "refs/remotes/origin/main", "main"); |
| 71 | yield* git("symbolic-ref", "refs/remotes/origin/HEAD", "refs/remotes/origin/main"); |
| 72 | }); |
| 73 | |
| 74 | it.effect("reads the default-branch template whatever branch the pull request targets", () => |
| 75 | withRepository(({ git, write, commit, readTemplate }) => |
| 76 | Effect.gen(function* () { |
| 77 | const templatePath = ".azuredevops/pull_request_template.md"; |
| 78 | yield* write(templatePath, "Default branch template"); |
| 79 | yield* commit; |
| 80 | yield* publishMain(git); |
| 81 | yield* git("checkout", "-b", "release"); |
| 82 | yield* write(templatePath, "Release branch template"); |
| 83 | yield* commit; |
| 84 | yield* write(templatePath, "Uncommitted template"); |
| 85 | |
| 86 | assert.deepStrictEqual( |
| 87 | yield* readTemplate("release"), |
| 88 | Option.some("Default branch template"), |
| 89 | ); |
| 90 | }), |
| 91 | ), |
| 92 | ); |
| 93 | |
| 94 | // Microsoft searches these locations in order, ignoring case; extension must not override folder. |
| 95 | it.effect.each([ |
| 96 | [".AzureDevOps/PULL_REQUEST_TEMPLATE.txt", ".vsts/pull_request_template.md"], |
| 97 | [".VSTS/pull_request_template.TXT", "docs/pull_request_template.md"], |
| 98 | ["Docs/Pull_Request_Template.txt", "pull_request_template.md"], |
| 99 | ] as const)("prefers %s over %s", ([preferred, fallback]) => |
| 100 | withRepository(({ git, write, commit, readTemplate }) => |
| 101 | Effect.gen(function* () { |
| 102 | yield* write(preferred, "Preferred template"); |
| 103 | yield* write(fallback, "Fallback template"); |
| 104 | yield* commit; |
| 105 | yield* publishMain(git); |
| 106 | |
| 107 | assert.deepStrictEqual(yield* readTemplate(), Option.some("Preferred template")); |
| 108 | }), |
| 109 | ), |
| 110 | ); |
| 111 | |
| 112 | it.effect("ignores missing, blank, symlinked, and nested templates", () => |
| 113 | withRepository(({ git, write, commit, readTemplate }) => |
| 114 | Effect.gen(function* () { |
| 115 | yield* commit; |
| 116 | yield* publishMain(git); |
| 117 | assert.deepStrictEqual(yield* readTemplate(), Option.none()); |
| 118 | |
| 119 | yield* write(".azuredevops/pull_request_template.md", " \n\t "); |
| 120 | yield* commit; |
| 121 | yield* publishMain(git); |
| 122 | assert.deepStrictEqual(yield* readTemplate(), Option.none()); |
| 123 | |
| 124 | yield* git("rm", "-q", ".azuredevops/pull_request_template.md"); |
| 125 | yield* write("template.md", "Not a default template"); |
| 126 | yield* write("docs/nested/pull_request_template.md", "Nested template"); |
| 127 | yield* commit; |
| 128 | // An index mode commits a symlink even on hosts without symlink support. |
| 129 | const blob = yield* git("rev-parse", "HEAD:template.md"); |
| 130 | yield* git( |
| 131 | "update-index", |
| 132 | "--add", |
| 133 | "--cacheinfo", |
| 134 | `120000,${blob},.azuredevops/pull_request_template.md`, |
| 135 | ); |
| 136 | yield* git("commit", "-m", "Add a symlinked template"); |
| 137 | yield* publishMain(git); |
| 138 | assert.deepStrictEqual(yield* readTemplate(), Option.none()); |
| 139 | }), |
| 140 | ), |
| 141 | ); |
| 142 | |
| 143 | it.effect("bounds template content and marks truncation", () => |
| 144 | withRepository(({ git, write, commit, readTemplate }) => |
| 145 | Effect.gen(function* () { |
| 146 | const prefix = "x".repeat(8_000); |
| 147 | yield* write("pull_request_template.md", prefix + "EXCLUDED_SUFFIX"); |
| 148 | yield* commit; |
| 149 | yield* publishMain(git); |
| 150 | |
| 151 | assert.deepStrictEqual(yield* readTemplate(), Option.some(prefix + "\n\n[truncated]")); |
| 152 | }), |
| 153 | ), |
| 154 | ); |
| 155 | |
| 156 | it.effect("returns no template when the local remote default branch is unknown", () => |
| 157 | withRepository(({ write, commit, readTemplate }) => |
| 158 | Effect.gen(function* () { |
| 159 | yield* write("pull_request_template.md", "Target branch template"); |
| 160 | yield* commit; |
| 161 | |
| 162 | assert.deepStrictEqual(yield* readTemplate(), Option.none()); |
| 163 | }), |
| 164 | ), |
| 165 | ); |