File
Blob: test/util/pack-indexer.helpers.ts
| 1 | import { SubrequestLimiter } from "@/worker/git/operations/limits"; |
| 2 | import { createLogger } from "@/worker/common/logger"; |
| 3 | import type { PackCatalogRow } from "@/worker/git/object-store/types"; |
| 4 | |
| 5 | export const packIndexerLog = createLogger("debug", { service: "PackIndexerTest" }); |
| 6 | |
| 7 | export function makeLimiter() { |
| 8 | return new SubrequestLimiter(6); |
| 9 | } |
| 10 | |
| 11 | export function makeCountSubrequest(counter: { count: number }) { |
| 12 | return (n = 1) => { |
| 13 | counter.count += n; |
| 14 | }; |
| 15 | } |
| 16 | |
| 17 | export function makeTracingLimiter(labels: string[]) { |
| 18 | return { |
| 19 | async run<T>(label: string, fn: () => Promise<T>): Promise<T> { |
| 20 | labels.push(label); |
| 21 | return await fn(); |
| 22 | }, |
| 23 | }; |
| 24 | } |
| 25 | |
| 26 | export function makeActiveCatalogRow(args: { |
| 27 | packKey: string; |
| 28 | packBytes: number; |
| 29 | objectCount: number; |
| 30 | idxBytes: number; |
| 31 | }): PackCatalogRow { |
| 32 | return { |
| 33 | packKey: args.packKey, |
| 34 | kind: "legacy", |
| 35 | state: "active", |
| 36 | tier: 0, |
| 37 | seqLo: 1, |
| 38 | seqHi: 1, |
| 39 | objectCount: args.objectCount, |
| 40 | packBytes: args.packBytes, |
| 41 | idxBytes: args.idxBytes, |
| 42 | createdAt: Date.now(), |
| 43 | supersededBy: null, |
| 44 | }; |
| 45 | } |
| 46 | |
| 47 | export async function rewritePackChecksum( |
| 48 | packBytes: Uint8Array, |
| 49 | mutate: (next: Uint8Array) => void |
| 50 | ): Promise<Uint8Array> { |
| 51 | const next = new Uint8Array(packBytes); |
| 52 | mutate(next); |
| 53 | const digest = new Uint8Array( |
| 54 | await crypto.subtle.digest("SHA-1", next.subarray(0, next.length - 20)) |
| 55 | ); |
| 56 | next.set(digest, next.length - 20); |
| 57 | return next; |
| 58 | } |
| 59 | |
| 60 | export function setSingleBytePackHeaderSize( |
| 61 | bytes: Uint8Array, |
| 62 | offset: number, |
| 63 | typeCode: number, |
| 64 | size: number |
| 65 | ): void { |
| 66 | if (size < 0 || size > 0x0f) { |
| 67 | throw new Error(`test helper requires a single-byte pack header size, got ${size}`); |
| 68 | } |
| 69 | bytes[offset] = (typeCode << 4) | size; |
| 70 | } |