|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { convertTransfer } from "@hyperframes/engine"; |
| 3 | +import { createHdrImageTransferCache } from "./hdrImageTransferCache.ts"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Build a deterministic rgb48le buffer for `pixelCount` pixels. |
| 7 | + * Each pixel is 3 channels × 2 bytes = 6 bytes. Values vary per pixel/channel |
| 8 | + * so the LUT-based `convertTransfer` produces bytes that differ from the |
| 9 | + * source. |
| 10 | + */ |
| 11 | +function makeSourceBuffer(pixelCount: number, seed = 0): Buffer { |
| 12 | + const buf = Buffer.alloc(pixelCount * 6); |
| 13 | + for (let i = 0; i < pixelCount; i++) { |
| 14 | + const off = i * 6; |
| 15 | + // Spread values across the 16-bit range so HLG↔PQ LUT lookups land on |
| 16 | + // mid-curve entries that are guaranteed to differ from the input. |
| 17 | + buf.writeUInt16LE((seed + i * 257) & 0xff_ff, off); |
| 18 | + buf.writeUInt16LE((seed + i * 521 + 1) & 0xff_ff, off + 2); |
| 19 | + buf.writeUInt16LE((seed + i * 1031 + 2) & 0xff_ff, off + 4); |
| 20 | + } |
| 21 | + return buf; |
| 22 | +} |
| 23 | + |
| 24 | +function expectedConverted(source: Buffer, from: "hlg" | "pq", to: "hlg" | "pq"): Buffer { |
| 25 | + const copy = Buffer.from(source); |
| 26 | + convertTransfer(copy, from, to); |
| 27 | + return copy; |
| 28 | +} |
| 29 | + |
| 30 | +describe("hdrImageTransferCache", () => { |
| 31 | + test("returns source buffer unchanged when sourceTransfer === targetTransfer", () => { |
| 32 | + const cache = createHdrImageTransferCache(); |
| 33 | + const source = makeSourceBuffer(4); |
| 34 | + |
| 35 | + const result = cache.getConverted("img1", "pq", "pq", source); |
| 36 | + |
| 37 | + expect(result).toBe(source); |
| 38 | + expect(cache.size()).toBe(0); |
| 39 | + }); |
| 40 | + |
| 41 | + test("first miss converts and caches", () => { |
| 42 | + const cache = createHdrImageTransferCache(); |
| 43 | + const source = makeSourceBuffer(4); |
| 44 | + const expected = expectedConverted(source, "hlg", "pq"); |
| 45 | + |
| 46 | + const result = cache.getConverted("img1", "hlg", "pq", source); |
| 47 | + |
| 48 | + expect(result).not.toBe(source); |
| 49 | + expect(Buffer.compare(result, expected)).toBe(0); |
| 50 | + expect(cache.size()).toBe(1); |
| 51 | + }); |
| 52 | + |
| 53 | + test("second hit returns cached buffer reference", () => { |
| 54 | + const cache = createHdrImageTransferCache(); |
| 55 | + const source = makeSourceBuffer(4); |
| 56 | + |
| 57 | + const first = cache.getConverted("img1", "hlg", "pq", source); |
| 58 | + const second = cache.getConverted("img1", "hlg", "pq", source); |
| 59 | + |
| 60 | + expect(second).toBe(first); |
| 61 | + expect(cache.size()).toBe(1); |
| 62 | + }); |
| 63 | + |
| 64 | + test("does not re-run convertTransfer on cache hit", () => { |
| 65 | + const cache = createHdrImageTransferCache(); |
| 66 | + const source = makeSourceBuffer(4); |
| 67 | + |
| 68 | + const first = cache.getConverted("img1", "hlg", "pq", source); |
| 69 | + const snapshot = Buffer.from(first); |
| 70 | + // If a hit ran convertTransfer again on the cached buffer (PQ→PQ would |
| 71 | + // be a no-op, but PQ→HLG would mutate), the bytes would change. |
| 72 | + cache.getConverted("img1", "hlg", "pq", source); |
| 73 | + |
| 74 | + expect(Buffer.compare(first, snapshot)).toBe(0); |
| 75 | + }); |
| 76 | + |
| 77 | + test("different target transfers for same imageId are cached independently", () => { |
| 78 | + const cache = createHdrImageTransferCache(); |
| 79 | + const source = makeSourceBuffer(4); |
| 80 | + |
| 81 | + const toPq = cache.getConverted("img1", "hlg", "pq", source); |
| 82 | + const toHlg = cache.getConverted("img1", "pq", "hlg", source); |
| 83 | + |
| 84 | + expect(toPq).not.toBe(toHlg); |
| 85 | + expect(Buffer.compare(toPq, expectedConverted(source, "hlg", "pq"))).toBe(0); |
| 86 | + expect(Buffer.compare(toHlg, expectedConverted(source, "pq", "hlg"))).toBe(0); |
| 87 | + expect(cache.size()).toBe(2); |
| 88 | + }); |
| 89 | + |
| 90 | + test("different imageIds are cached independently", () => { |
| 91 | + const cache = createHdrImageTransferCache(); |
| 92 | + const a = makeSourceBuffer(4, 100); |
| 93 | + const b = makeSourceBuffer(4, 200); |
| 94 | + |
| 95 | + const convA = cache.getConverted("a", "hlg", "pq", a); |
| 96 | + const convB = cache.getConverted("b", "hlg", "pq", b); |
| 97 | + |
| 98 | + expect(convA).not.toBe(convB); |
| 99 | + expect(Buffer.compare(convA, expectedConverted(a, "hlg", "pq"))).toBe(0); |
| 100 | + expect(Buffer.compare(convB, expectedConverted(b, "hlg", "pq"))).toBe(0); |
| 101 | + expect(cache.size()).toBe(2); |
| 102 | + }); |
| 103 | + |
| 104 | + test("LRU evicts oldest entry when maxEntries exceeded", () => { |
| 105 | + const cache = createHdrImageTransferCache({ maxEntries: 2 }); |
| 106 | + const a = makeSourceBuffer(2, 1); |
| 107 | + const b = makeSourceBuffer(2, 2); |
| 108 | + const c = makeSourceBuffer(2, 3); |
| 109 | + |
| 110 | + const convA1 = cache.getConverted("a", "hlg", "pq", a); |
| 111 | + cache.getConverted("b", "hlg", "pq", b); |
| 112 | + cache.getConverted("c", "hlg", "pq", c); |
| 113 | + |
| 114 | + expect(cache.size()).toBe(2); |
| 115 | + |
| 116 | + const convA2 = cache.getConverted("a", "hlg", "pq", a); |
| 117 | + expect(convA2).not.toBe(convA1); |
| 118 | + expect(Buffer.compare(convA2, expectedConverted(a, "hlg", "pq"))).toBe(0); |
| 119 | + }); |
| 120 | + |
| 121 | + test("access promotes entry to most-recently-used", () => { |
| 122 | + const cache = createHdrImageTransferCache({ maxEntries: 2 }); |
| 123 | + const a = makeSourceBuffer(2, 1); |
| 124 | + const b = makeSourceBuffer(2, 2); |
| 125 | + const c = makeSourceBuffer(2, 3); |
| 126 | + |
| 127 | + const convA1 = cache.getConverted("a", "hlg", "pq", a); |
| 128 | + cache.getConverted("b", "hlg", "pq", b); |
| 129 | + |
| 130 | + const convA2 = cache.getConverted("a", "hlg", "pq", a); |
| 131 | + expect(convA2).toBe(convA1); |
| 132 | + |
| 133 | + cache.getConverted("c", "hlg", "pq", c); |
| 134 | + |
| 135 | + const convA3 = cache.getConverted("a", "hlg", "pq", a); |
| 136 | + expect(convA3).toBe(convA1); |
| 137 | + |
| 138 | + const convB2 = cache.getConverted("b", "hlg", "pq", b); |
| 139 | + expect(Buffer.compare(convB2, expectedConverted(b, "hlg", "pq"))).toBe(0); |
| 140 | + expect(cache.size()).toBe(2); |
| 141 | + }); |
| 142 | + |
| 143 | + test("maxEntries: 0 disables caching but still returns correct converted bytes", () => { |
| 144 | + const cache = createHdrImageTransferCache({ maxEntries: 0 }); |
| 145 | + const source = makeSourceBuffer(4); |
| 146 | + const expected = expectedConverted(source, "hlg", "pq"); |
| 147 | + |
| 148 | + const first = cache.getConverted("img1", "hlg", "pq", source); |
| 149 | + const second = cache.getConverted("img1", "hlg", "pq", source); |
| 150 | + |
| 151 | + expect(first).not.toBe(second); |
| 152 | + expect(Buffer.compare(first, expected)).toBe(0); |
| 153 | + expect(Buffer.compare(second, expected)).toBe(0); |
| 154 | + expect(cache.size()).toBe(0); |
| 155 | + }); |
| 156 | + |
| 157 | + test("cached buffer is independent from the source buffer", () => { |
| 158 | + const cache = createHdrImageTransferCache(); |
| 159 | + const source = makeSourceBuffer(4); |
| 160 | + const sourceSnapshot = Buffer.from(source); |
| 161 | + |
| 162 | + const cached = cache.getConverted("img1", "hlg", "pq", source); |
| 163 | + source.fill(0); |
| 164 | + |
| 165 | + expect(cache.getConverted("img1", "hlg", "pq", source)).toBe(cached); |
| 166 | + expect(Buffer.compare(cached, expectedConverted(sourceSnapshot, "hlg", "pq"))).toBe(0); |
| 167 | + }); |
| 168 | + |
| 169 | + // Source-buffer-immutability guarantee (PR #384 review feedback): the cache |
| 170 | + // MUST NOT mutate the source buffer the caller hands in, on any path. |
| 171 | + // `convertTransfer` mutates in place, so the implementation has to clone |
| 172 | + // before converting — these tests pin the invariant against future |
| 173 | + // refactors that might forget the `Buffer.from(source)` defense. |
| 174 | + |
| 175 | + test("does not mutate the source buffer on a convert+cache miss", () => { |
| 176 | + const cache = createHdrImageTransferCache(); |
| 177 | + const source = makeSourceBuffer(4); |
| 178 | + const sourceSnapshot = Buffer.from(source); |
| 179 | + |
| 180 | + cache.getConverted("img1", "hlg", "pq", source); |
| 181 | + |
| 182 | + expect(Buffer.compare(source, sourceSnapshot)).toBe(0); |
| 183 | + }); |
| 184 | + |
| 185 | + test("does not mutate the source buffer on a convert+cache miss with maxEntries=0 passthrough", () => { |
| 186 | + const cache = createHdrImageTransferCache({ maxEntries: 0 }); |
| 187 | + const source = makeSourceBuffer(4); |
| 188 | + const sourceSnapshot = Buffer.from(source); |
| 189 | + |
| 190 | + const result = cache.getConverted("img1", "hlg", "pq", source); |
| 191 | + |
| 192 | + expect(Buffer.compare(source, sourceSnapshot)).toBe(0); |
| 193 | + expect(result).not.toBe(source); |
| 194 | + expect(Buffer.compare(result, expectedConverted(sourceSnapshot, "hlg", "pq"))).toBe(0); |
| 195 | + expect(cache.size()).toBe(0); |
| 196 | + }); |
| 197 | + |
| 198 | + test("does not mutate the source buffer on a cache hit", () => { |
| 199 | + const cache = createHdrImageTransferCache(); |
| 200 | + const source = makeSourceBuffer(4); |
| 201 | + const sourceSnapshot = Buffer.from(source); |
| 202 | + |
| 203 | + cache.getConverted("img1", "hlg", "pq", source); |
| 204 | + cache.getConverted("img1", "hlg", "pq", source); |
| 205 | + |
| 206 | + expect(Buffer.compare(source, sourceSnapshot)).toBe(0); |
| 207 | + }); |
| 208 | + |
| 209 | + test("rejects invalid maxEntries", () => { |
| 210 | + expect(() => createHdrImageTransferCache({ maxEntries: -1 })).toThrow(); |
| 211 | + expect(() => createHdrImageTransferCache({ maxEntries: 1.5 })).toThrow(); |
| 212 | + expect(() => createHdrImageTransferCache({ maxEntries: Number.NaN })).toThrow(); |
| 213 | + }); |
| 214 | + |
| 215 | + test("default maxEntries is large enough for typical compositions", () => { |
| 216 | + const cache = createHdrImageTransferCache(); |
| 217 | + const source = makeSourceBuffer(2); |
| 218 | + |
| 219 | + for (let i = 0; i < 16; i++) { |
| 220 | + cache.getConverted(`img${i}`, "hlg", "pq", source); |
| 221 | + } |
| 222 | + expect(cache.size()).toBe(16); |
| 223 | + |
| 224 | + // The first inserted entry should still be present (no eviction yet). |
| 225 | + const first = cache.getConverted("img0", "hlg", "pq", source); |
| 226 | + expect(Buffer.compare(first, expectedConverted(source, "hlg", "pq"))).toBe(0); |
| 227 | + expect(cache.size()).toBe(16); |
| 228 | + }); |
| 229 | +}); |
0 commit comments