File
Blob: test/readpath-tag-peel.worker.test.ts
| 1 | import { it, expect } from "vitest"; |
| 2 | import { env } from "cloudflare:workers"; |
| 3 | import { asTypedStorage, type RepoStateSchema } from "@/worker/do/repo/repoState"; |
| 4 | import { uniqueRepoId, runDOWithRetry } from "./util/test-helpers"; |
| 5 | import { setupRepoForTests } from "./util/repoSeed"; |
| 6 | import { readPath } from "@/worker/git/operations/read"; |
| 7 | import { encodeGitObject } from "@/worker/git/core/objects"; |
| 8 | import { seedPackFirstRepo } from "./util/pack-first"; |
| 9 | import { registerTestPack } from "./util/packed-repo"; |
| 10 | |
| 11 | it("readPath resolves tag to its target commit tree (tag peel)", async () => { |
| 12 | const owner = "o"; |
| 13 | const repo = uniqueRepoId("r-readpath-tag"); |
| 14 | await setupRepoForTests(env, owner, repo); |
| 15 | const repoId = `${owner}/${repo}`; |
| 16 | const seeded = await seedPackFirstRepo(repoId); |
| 17 | const id = env.REPO_DO.idFromName(repoId); |
| 18 | const getStub = () => env.REPO_DO.get(id); |
| 19 | |
| 20 | // Create an annotated tag object pointing to the seeded commit |
| 21 | const commitOid = seeded.nextCommit.oid; |
| 22 | const tagPayload = new TextEncoder().encode( |
| 23 | `object ${commitOid}\n` + |
| 24 | `type commit\n` + |
| 25 | `tag v1\n` + |
| 26 | `tagger Test <test@example.com> 0 +0000\n` + |
| 27 | `\n` + |
| 28 | `message\n` |
| 29 | ); |
| 30 | const { oid: tagOid } = await encodeGitObject("tag", tagPayload); |
| 31 | |
| 32 | // Pack the tag and register it |
| 33 | await registerTestPack({ |
| 34 | env, |
| 35 | repoId, |
| 36 | getStub, |
| 37 | packName: "pack-tag.pack", |
| 38 | objects: [{ type: "tag", payload: tagPayload }], |
| 39 | }); |
| 40 | |
| 41 | // Add the tag ref |
| 42 | await runDOWithRetry(getStub, async (_instance, state) => { |
| 43 | const store = asTypedStorage<RepoStateSchema>(state.storage); |
| 44 | const refs = ((await store.get("refs")) || []) as { name: string; oid: string }[]; |
| 45 | refs.push({ name: "refs/tags/v1", oid: tagOid }); |
| 46 | await store.put("refs", refs); |
| 47 | }); |
| 48 | |
| 49 | // Call readPath with the tag ref; expect a tree result (root tree of the tagged commit) |
| 50 | const result = await readPath(env as unknown as Env, repoId, "refs/tags/v1"); |
| 51 | expect(result.type).toBe("tree"); |
| 52 | if (result.type === "tree") { |
| 53 | expect(Array.isArray(result.entries)).toBe(true); |
| 54 | } |
| 55 | }); |