File
Blob: test/util/pack-first.ts
| 1 | import type { CacheContext } from "@/worker/cache"; |
| 2 | |
| 3 | import { buildPack } from "./git-pack"; |
| 4 | import { buildTreePayload, seedPackedRepoState } from "./packed-repo"; |
| 5 | import { encodeGitObject } from "@/worker/git/core"; |
| 6 | import { createExecutionContext, env } from "cloudflare:test"; |
| 7 | |
| 8 | export function createTestCacheContext(url: string, subreqBudget?: number): CacheContext { |
| 9 | return { |
| 10 | req: new Request(url), |
| 11 | ctx: createExecutionContext(), |
| 12 | memo: typeof subreqBudget === "number" ? { subreqBudget } : {}, |
| 13 | }; |
| 14 | } |
| 15 | |
| 16 | export async function seedPackFirstRepo(repoId: string) { |
| 17 | const id = env.REPO_DO.idFromName(repoId); |
| 18 | const getStub = () => env.REPO_DO.get(id); |
| 19 | const author = "You <you@example.com> 0 +0000"; |
| 20 | const baseBlobPayload = new TextEncoder().encode("version one\n"); |
| 21 | const nextBlobPayload = new TextEncoder().encode("version two\n"); |
| 22 | |
| 23 | const baseBlob = await encodeGitObject("blob", baseBlobPayload); |
| 24 | const nextBlob = await encodeGitObject("blob", nextBlobPayload); |
| 25 | |
| 26 | const baseTreePayload = buildTreePayload([ |
| 27 | { mode: "100644", name: "README.md", oid: baseBlob.oid }, |
| 28 | ]); |
| 29 | const nextTreePayload = buildTreePayload([ |
| 30 | { mode: "100644", name: "README.md", oid: nextBlob.oid }, |
| 31 | ]); |
| 32 | const baseTree = await encodeGitObject("tree", baseTreePayload); |
| 33 | const nextTree = await encodeGitObject("tree", nextTreePayload); |
| 34 | |
| 35 | const baseCommitPayload = new TextEncoder().encode( |
| 36 | `tree ${baseTree.oid}\n` + `author ${author}\n` + `committer ${author}\n\n` + `first commit\n` |
| 37 | ); |
| 38 | const baseCommit = await encodeGitObject("commit", baseCommitPayload); |
| 39 | |
| 40 | const nextCommitPayload = new TextEncoder().encode( |
| 41 | `tree ${nextTree.oid}\n` + |
| 42 | `parent ${baseCommit.oid}\n` + |
| 43 | `author ${author}\n` + |
| 44 | `committer ${author}\n\n` + |
| 45 | `second commit\n` |
| 46 | ); |
| 47 | const nextCommit = await encodeGitObject("commit", nextCommitPayload); |
| 48 | |
| 49 | const packBytes = await buildPack([ |
| 50 | { type: "blob", payload: baseBlobPayload }, |
| 51 | { type: "tree", payload: baseTreePayload }, |
| 52 | { type: "commit", payload: baseCommitPayload }, |
| 53 | { type: "blob", payload: nextBlobPayload }, |
| 54 | { type: "tree", payload: nextTreePayload }, |
| 55 | { type: "commit", payload: nextCommitPayload }, |
| 56 | ]); |
| 57 | |
| 58 | const seeded = await seedPackedRepoState({ |
| 59 | env, |
| 60 | repoId, |
| 61 | getStub, |
| 62 | packs: [{ name: "pack-read-path.pack", packBytes }], |
| 63 | refs: [{ name: "refs/heads/main", oid: nextCommit.oid }], |
| 64 | head: { target: "refs/heads/main", oid: nextCommit.oid }, |
| 65 | looseObjects: [baseBlob, nextBlob, baseTree, nextTree, baseCommit, nextCommit], |
| 66 | mirrorLooseToR2: true, |
| 67 | }); |
| 68 | |
| 69 | return { |
| 70 | ...seeded, |
| 71 | baseBlob, |
| 72 | nextBlob, |
| 73 | baseTree, |
| 74 | nextTree, |
| 75 | baseCommit, |
| 76 | nextCommit, |
| 77 | objectOids: [ |
| 78 | baseBlob.oid, |
| 79 | nextBlob.oid, |
| 80 | baseTree.oid, |
| 81 | nextTree.oid, |
| 82 | baseCommit.oid, |
| 83 | nextCommit.oid, |
| 84 | ], |
| 85 | }; |
| 86 | } |