|
| 1 | +import { describe, it, expect, beforeEach } from "vitest"; |
| 2 | +import { resolveBucket } from "./bucket.js"; |
| 3 | +import type { Env } from "./types.js"; |
| 4 | + |
| 5 | +// --------------------------------------------------------------------------- |
| 6 | +// Mock R2 buckets (just need distinct identity) |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | + |
| 9 | +function mockBucket(name: string): R2Bucket { |
| 10 | + return { _name: name } as unknown as R2Bucket; |
| 11 | +} |
| 12 | + |
| 13 | +// resolveBucket uses a module-level cache — we need a fresh import per test group. |
| 14 | +// Since we can't easily reset module state, we test behavior patterns instead. |
| 15 | + |
| 16 | +describe("resolveBucket", () => { |
| 17 | + describe("single bucket (no shards)", () => { |
| 18 | + it("returns DATA_BUCKET when no shard buckets configured", () => { |
| 19 | + // Reset module cache by providing a fresh env |
| 20 | + // Note: module-level cache means subsequent calls reuse first env's buckets. |
| 21 | + // This test must run first or in isolation. |
| 22 | + const primary = mockBucket("primary"); |
| 23 | + const env = { |
| 24 | + DATA_BUCKET: primary, |
| 25 | + MASTER_DO: {} as DurableObjectNamespace, |
| 26 | + QUERY_DO: {} as DurableObjectNamespace, |
| 27 | + FRAGMENT_DO: {} as DurableObjectNamespace, |
| 28 | + } as Env; |
| 29 | + |
| 30 | + const result = resolveBucket(env, "orders/fragment-1.lance"); |
| 31 | + // With single bucket, always returns primary |
| 32 | + expect(result).toBeDefined(); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe("FNV-1a hash properties", () => { |
| 37 | + it("same key always routes to same bucket (deterministic)", () => { |
| 38 | + const env = { |
| 39 | + DATA_BUCKET: mockBucket("b0"), |
| 40 | + DATA_BUCKET_1: mockBucket("b1"), |
| 41 | + DATA_BUCKET_2: mockBucket("b2"), |
| 42 | + DATA_BUCKET_3: mockBucket("b3"), |
| 43 | + MASTER_DO: {} as DurableObjectNamespace, |
| 44 | + QUERY_DO: {} as DurableObjectNamespace, |
| 45 | + FRAGMENT_DO: {} as DurableObjectNamespace, |
| 46 | + } as Env; |
| 47 | + |
| 48 | + const r1 = resolveBucket(env, "orders/fragment-1.lance"); |
| 49 | + const r2 = resolveBucket(env, "orders/fragment-1.lance"); |
| 50 | + const r3 = resolveBucket(env, "orders/fragment-99.lance"); |
| 51 | + // Same table prefix → same bucket |
| 52 | + expect(r1).toBe(r2); |
| 53 | + expect(r1).toBe(r3); // "orders" prefix is the same |
| 54 | + }); |
| 55 | + |
| 56 | + it("routes by prefix (table name), not full key", () => { |
| 57 | + const env = { |
| 58 | + DATA_BUCKET: mockBucket("b0"), |
| 59 | + DATA_BUCKET_1: mockBucket("b1"), |
| 60 | + DATA_BUCKET_2: mockBucket("b2"), |
| 61 | + DATA_BUCKET_3: mockBucket("b3"), |
| 62 | + MASTER_DO: {} as DurableObjectNamespace, |
| 63 | + QUERY_DO: {} as DurableObjectNamespace, |
| 64 | + FRAGMENT_DO: {} as DurableObjectNamespace, |
| 65 | + } as Env; |
| 66 | + |
| 67 | + // Different fragments of same table → same bucket |
| 68 | + const r1 = resolveBucket(env, "users/fragment-1.lance"); |
| 69 | + const r2 = resolveBucket(env, "users/fragment-999.lance"); |
| 70 | + expect(r1).toBe(r2); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe("FNV-1a hash distribution", () => { |
| 75 | + it("distributes different table names across buckets", () => { |
| 76 | + // We can't test actual distribution without resetting module cache, |
| 77 | + // but we can verify the FNV-1a algorithm independently |
| 78 | + const tables = ["orders", "users", "events", "products", "sessions", |
| 79 | + "clicks", "logs", "metrics", "payments", "invoices", |
| 80 | + "customers", "inventory", "shipments", "reviews", "ratings"]; |
| 81 | + |
| 82 | + // Simulate FNV-1a hash |
| 83 | + const bucketCount = 4; |
| 84 | + const distribution = new Map<number, string[]>(); |
| 85 | + for (const table of tables) { |
| 86 | + let h = 0x811c9dc5; |
| 87 | + for (let i = 0; i < table.length; i++) { |
| 88 | + h ^= table.charCodeAt(i); |
| 89 | + h = Math.imul(h, 0x01000193); |
| 90 | + } |
| 91 | + const idx = (h >>> 0) % bucketCount; |
| 92 | + if (!distribution.has(idx)) distribution.set(idx, []); |
| 93 | + distribution.get(idx)!.push(table); |
| 94 | + } |
| 95 | + |
| 96 | + // With 15 tables across 4 buckets, expect each bucket to have at least 1 |
| 97 | + expect(distribution.size).toBeGreaterThanOrEqual(2); |
| 98 | + // No single bucket should have all tables |
| 99 | + for (const [, tables] of distribution) { |
| 100 | + expect(tables.length).toBeLessThan(15); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + it("FNV-1a is deterministic for known inputs", () => { |
| 105 | + // Verify hash of "orders" is consistent |
| 106 | + const table = "orders"; |
| 107 | + let h = 0x811c9dc5; |
| 108 | + for (let i = 0; i < table.length; i++) { |
| 109 | + h ^= table.charCodeAt(i); |
| 110 | + h = Math.imul(h, 0x01000193); |
| 111 | + } |
| 112 | + const hash1 = h >>> 0; |
| 113 | + |
| 114 | + // Same computation again |
| 115 | + h = 0x811c9dc5; |
| 116 | + for (let i = 0; i < table.length; i++) { |
| 117 | + h ^= table.charCodeAt(i); |
| 118 | + h = Math.imul(h, 0x01000193); |
| 119 | + } |
| 120 | + const hash2 = h >>> 0; |
| 121 | + |
| 122 | + expect(hash1).toBe(hash2); |
| 123 | + expect(hash1).toBeGreaterThan(0); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments