|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { bestClaim, type WikidataClaim } from "./wikidata"; |
| 3 | + |
| 4 | +function makeClaim(time: string, rank?: "preferred" | "normal" | "deprecated"): WikidataClaim { |
| 5 | + return { |
| 6 | + mainsnak: { |
| 7 | + datavalue: { |
| 8 | + value: { time }, |
| 9 | + type: "time", |
| 10 | + }, |
| 11 | + }, |
| 12 | + rank, |
| 13 | + }; |
| 14 | +} |
| 15 | + |
| 16 | +describe("bestClaim", () => { |
| 17 | + it("returns the preferred claim over normal ones", () => { |
| 18 | + const claims = [ |
| 19 | + makeClaim("+2026-02-15T00:00:00Z", "normal"), |
| 20 | + makeClaim("+2026-04-01T00:00:00Z", "preferred"), |
| 21 | + ]; |
| 22 | + const result = bestClaim(claims); |
| 23 | + expect((result!.mainsnak.datavalue!.value as { time: string }).time).toBe("+2026-04-01T00:00:00Z"); |
| 24 | + }); |
| 25 | + |
| 26 | + it("skips deprecated claims", () => { |
| 27 | + const claims = [ |
| 28 | + makeClaim("+2026-02-15T00:00:00Z", "deprecated"), |
| 29 | + makeClaim("+2026-04-01T00:00:00Z", "normal"), |
| 30 | + ]; |
| 31 | + const result = bestClaim(claims); |
| 32 | + expect((result!.mainsnak.datavalue!.value as { time: string }).time).toBe("+2026-04-01T00:00:00Z"); |
| 33 | + }); |
| 34 | + |
| 35 | + it("returns null when all claims are deprecated", () => { |
| 36 | + const claims = [ |
| 37 | + makeClaim("+2026-02-15T00:00:00Z", "deprecated"), |
| 38 | + makeClaim("+2025-11-01T00:00:00Z", "deprecated"), |
| 39 | + ]; |
| 40 | + expect(bestClaim(claims)).toBeNull(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("returns the first normal claim when no preferred exists", () => { |
| 44 | + const claims = [ |
| 45 | + makeClaim("+2020-01-01T00:00:00Z", "normal"), |
| 46 | + makeClaim("+2021-06-15T00:00:00Z", "normal"), |
| 47 | + ]; |
| 48 | + const result = bestClaim(claims); |
| 49 | + expect((result!.mainsnak.datavalue!.value as { time: string }).time).toBe("+2020-01-01T00:00:00Z"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("handles claims without an explicit rank (treats as non-deprecated)", () => { |
| 53 | + const claims = [ |
| 54 | + makeClaim("+1969-07-20T00:00:00Z"), |
| 55 | + ]; |
| 56 | + const result = bestClaim(claims); |
| 57 | + expect((result!.mainsnak.datavalue!.value as { time: string }).time).toBe("+1969-07-20T00:00:00Z"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("prefers preferred over claims with no rank", () => { |
| 61 | + const claims = [ |
| 62 | + makeClaim("+2020-01-01T00:00:00Z"), |
| 63 | + makeClaim("+2026-04-01T00:00:00Z", "preferred"), |
| 64 | + ]; |
| 65 | + const result = bestClaim(claims); |
| 66 | + expect((result!.mainsnak.datavalue!.value as { time: string }).time).toBe("+2026-04-01T00:00:00Z"); |
| 67 | + }); |
| 68 | +}); |
0 commit comments