Skip to content
File

Blob: test/util/compaction-helpers.ts

typescript71 lines
1import { env } from "cloudflare:test";
2import { packIndexKey, packRefsKey } from "@/worker/keys";
3import { pushStreamingUpdate } from "./streaming-helpers";
4import { runQueueMessage, type QueueRunResult } from "./queue";
5 
6export async function compactOnce(repoId: string): Promise<QueueRunResult> {
7 const doId = env.REPO_DO.idFromName(repoId).toString();
8 return await runQueueMessage({
9 kind: "compaction",
10 doId,
11 repoId,
12 });
13}
14 
15export async function deleteSupersededOnce(
16 repoId: string,
17 packKeys: string[]
18): Promise<QueueRunResult> {
19 const doId = env.REPO_DO.idFromName(repoId).toString();
20 return await runQueueMessage({
21 kind: "compaction-delete",
22 doId,
23 repoId,
24 packKeys,
25 });
26}
27 
28export async function collectPackObjects(
29 packKeys: string[]
30): Promise<Array<{ packKey: string; exists: boolean; idxExists: boolean; refsExists: boolean }>> {
31 const checks: Array<{
32 packKey: string;
33 exists: boolean;
34 idxExists: boolean;
35 refsExists: boolean;
36 }> = [];
37 for (const packKey of packKeys) {
38 checks.push({
39 packKey,
40 exists: (await env.REPO_BUCKET.head(packKey)) !== null,
41 idxExists: (await env.REPO_BUCKET.head(packIndexKey(packKey))) !== null,
42 refsExists: (await env.REPO_BUCKET.head(packRefsKey(packKey))) !== null,
43 });
44 }
45 return checks;
46}
47 
48export async function pushOverflowingStreamingHistory(args: {
49 owner: string;
50 repo: string;
51 repoId: string;
52 startingCommitOid: string;
53 updates: number;
54}): Promise<{ currentCommitOid: string; objectOids: string[] }> {
55 let currentCommitOid = args.startingCommitOid;
56 const objectOids: string[] = [];
57 
58 for (let index = 0; index < args.updates; index++) {
59 const pushed = await pushStreamingUpdate(
60 args.owner,
61 args.repo,
62 currentCommitOid,
63 `streaming update ${index}\n`
64 );
65 currentCommitOid = pushed.commitOid;
66 objectOids.push(pushed.blob.oid, pushed.tree.oid, pushed.commit.oid);
67 }
68 
69 return { currentCommitOid, objectOids };
70}