|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + classifyReferenceBackground, |
| 4 | + createReferenceBackgroundRecorder, |
| 5 | + referenceBackgroundResult, |
| 6 | + type BackgroundLayer, |
| 7 | + type ReferenceBackgroundResult, |
| 8 | +} from "./referenceBackground.js"; |
| 9 | + |
| 10 | +const layer = (overrides: Partial<BackgroundLayer> = {}): BackgroundLayer => ({ |
| 11 | + rgba: [10, 46, 138, 255], |
| 12 | + image: "none", |
| 13 | + opacity: "1", |
| 14 | + filter: "none", |
| 15 | + blend: "normal", |
| 16 | + varying: false, |
| 17 | + ...overrides, |
| 18 | +}); |
| 19 | + |
| 20 | +describe("reference background", () => { |
| 21 | + it("returns opaque browser-resolved RGB, not a named/default color", () => { |
| 22 | + expect(classifyReferenceBackground({ layers: [layer()], explicitNone: false })).toEqual({ |
| 23 | + kind: "solid", |
| 24 | + rgb: [10, 46, 138], |
| 25 | + }); |
| 26 | + }); |
| 27 | + it.each(["linear-gradient(red, blue)", 'url("film.png")'])( |
| 28 | + "never coerces %s to its background color", |
| 29 | + (image) => { |
| 30 | + expect( |
| 31 | + classifyReferenceBackground({ layers: [layer({ image })], explicitNone: false }), |
| 32 | + ).toEqual({ kind: "unsupported_non_solid" }); |
| 33 | + }, |
| 34 | + ); |
| 35 | + it("requires positive absence proof, not merely transparent computed color", () => { |
| 36 | + const layers = [layer({ rgba: [0, 0, 0, 0] })]; |
| 37 | + expect(classifyReferenceBackground({ layers, explicitNone: false })).toEqual({ |
| 38 | + kind: "unknown", |
| 39 | + }); |
| 40 | + expect(classifyReferenceBackground({ layers, explicitNone: true })).toEqual({ kind: "none" }); |
| 41 | + }); |
| 42 | + it("does not use a hidden ancestor image behind an opaque root", () => { |
| 43 | + expect( |
| 44 | + classifyReferenceBackground({ |
| 45 | + layers: [layer(), layer({ image: "url(image.png)" })], |
| 46 | + explicitNone: false, |
| 47 | + }), |
| 48 | + ).toEqual({ kind: "solid", rgb: [10, 46, 138] }); |
| 49 | + }); |
| 50 | + it.each([ |
| 51 | + null, |
| 52 | + { layers: [], explicitNone: false }, |
| 53 | + { layers: [layer({ rgba: [NaN, 0, 0, 255] })], explicitNone: false }, |
| 54 | + { layers: [layer({ rgba: [0, 0, 0, 128] }), layer()], explicitNone: false }, |
| 55 | + { layers: [layer({ varying: true })], explicitNone: false }, |
| 56 | + ])("leaves unavailable, malformed, translucent or varying paint unknown", (observation) => { |
| 57 | + expect(classifyReferenceBackground(observation)).toEqual({ kind: "unknown" }); |
| 58 | + }); |
| 59 | + it.each([ |
| 60 | + undefined, |
| 61 | + {}, |
| 62 | + { kind: "solid", rgb: [1, 2, 256] }, |
| 63 | + { kind: "solid", rgb: [1, 2.5, 3] }, |
| 64 | + { kind: "solid", rgb: [1, 2] }, |
| 65 | + ])("normalizes malformed receipts to unknown", (value) => { |
| 66 | + expect(referenceBackgroundResult(value)).toEqual({ reference_background: { kind: "unknown" } }); |
| 67 | + }); |
| 68 | + it("reserves exactly one initial read across concurrent and later sessions", async () => { |
| 69 | + const records: ReferenceBackgroundResult[] = []; |
| 70 | + const record = createReferenceBackgroundRecorder((value) => { |
| 71 | + records.push(value); |
| 72 | + }); |
| 73 | + const first = vi.fn(async () => ({ |
| 74 | + kind: "solid" as const, |
| 75 | + rgb: [10, 46, 138] as [number, number, number], |
| 76 | + })); |
| 77 | + const later = vi.fn(async () => ({ kind: "unsupported_non_solid" as const })); |
| 78 | + await Promise.all([record(first), record(later)]); |
| 79 | + await record(later); |
| 80 | + expect(first).toHaveBeenCalledTimes(1); |
| 81 | + expect(later).not.toHaveBeenCalled(); |
| 82 | + expect(records).toEqual([{ reference_background: { kind: "solid", rgb: [10, 46, 138] } }]); |
| 83 | + }); |
| 84 | + it("does not replace an indeterminate canonical read with a chunk-local fallback", async () => { |
| 85 | + const records: ReferenceBackgroundResult[] = []; |
| 86 | + const record = createReferenceBackgroundRecorder((value) => { |
| 87 | + records.push(value); |
| 88 | + }); |
| 89 | + await record(async () => { |
| 90 | + throw new Error("page closed"); |
| 91 | + }); |
| 92 | + const fallback = vi.fn(async () => ({ kind: "none" as const })); |
| 93 | + await record(fallback); |
| 94 | + expect(fallback).not.toHaveBeenCalled(); |
| 95 | + expect(records).toEqual([{ reference_background: { kind: "unknown" } }]); |
| 96 | + }); |
| 97 | +}); |
0 commit comments