|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { FeedItem } from "../../domain.ts"; |
| 3 | +import { |
| 4 | + buildRankingPrompt, |
| 5 | + itemKey, |
| 6 | + mergeRankedKeysOrder, |
| 7 | + mergeRankedOrder, |
| 8 | + parseRankedIndices, |
| 9 | +} from "../rank.ts"; |
| 10 | +import { FakeLlmRanker } from "./fakeLlmRanker.ts"; |
| 11 | + |
| 12 | +function item(overrides: Partial<FeedItem>): FeedItem { |
| 13 | + return { |
| 14 | + source: "hackernews", |
| 15 | + externalId: "1", |
| 16 | + title: "title", |
| 17 | + url: "https://example.com", |
| 18 | + sourceRank: 1, |
| 19 | + metadata: {}, |
| 20 | + ...overrides, |
| 21 | + }; |
| 22 | +} |
| 23 | + |
| 24 | +describe("buildRankingPrompt", () => { |
| 25 | + it("numbers items by index and includes title, summary, and interests", () => { |
| 26 | + const items = [ |
| 27 | + item({ title: "Rust async runtime", summary: "a new executor" }), |
| 28 | + item({ title: "No summary item" }), |
| 29 | + ]; |
| 30 | + const prompt = buildRankingPrompt(items, "rust, distributed systems"); |
| 31 | + expect(prompt).toContain("0: Rust async runtime — a new executor"); |
| 32 | + expect(prompt).toContain("1: No summary item"); |
| 33 | + expect(prompt).toContain("Reader interests: rust, distributed systems"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("truncates oversized interests text", () => { |
| 37 | + const prompt = buildRankingPrompt([], "x".repeat(1000)); |
| 38 | + const line = prompt.split("\n").find((l) => l.startsWith("Reader interests:"))!; |
| 39 | + expect(line.length).toBeLessThan(320); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe("parseRankedIndices", () => { |
| 44 | + it("parses a well-formed JSON array", () => { |
| 45 | + expect(parseRankedIndices("[2, 0, 1]", 3)).toEqual([2, 0, 1]); |
| 46 | + }); |
| 47 | + |
| 48 | + it("tolerates surrounding prose/markdown around the array", () => { |
| 49 | + expect(parseRankedIndices("Sure! ```json\n[1, 0]\n```", 2)).toEqual([ |
| 50 | + 1, 0, |
| 51 | + ]); |
| 52 | + }); |
| 53 | + |
| 54 | + it("drops out-of-range and duplicate indices", () => { |
| 55 | + expect(parseRankedIndices("[1, 1, 5, -1, 0]", 2)).toEqual([1, 0]); |
| 56 | + }); |
| 57 | + |
| 58 | + it("returns [] for garbage output instead of throwing", () => { |
| 59 | + expect(parseRankedIndices("not even close to json", 3)).toEqual([]); |
| 60 | + expect(parseRankedIndices("", 3)).toEqual([]); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe("mergeRankedOrder", () => { |
| 65 | + const items = [item({ externalId: "a" }), item({ externalId: "b" }), item({ externalId: "c" })]; |
| 66 | + |
| 67 | + it("places ranked items first in model order, then appends the rest", () => { |
| 68 | + const merged = mergeRankedOrder(items, [2, 0]); |
| 69 | + expect(merged.map((i) => i.externalId)).toEqual(["c", "a", "b"]); |
| 70 | + }); |
| 71 | + |
| 72 | + it("passes through original order when ranking is empty", () => { |
| 73 | + expect(mergeRankedOrder(items, []).map((i) => i.externalId)).toEqual([ |
| 74 | + "a", |
| 75 | + "b", |
| 76 | + "c", |
| 77 | + ]); |
| 78 | + }); |
| 79 | + |
| 80 | + it("ignores indices it doesn't recognize without throwing", () => { |
| 81 | + const merged = mergeRankedOrder(items, [99, 1]); |
| 82 | + expect(merged.map((i) => i.externalId)).toEqual(["b", "a", "c"]); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe("mergeRankedKeysOrder", () => { |
| 87 | + const items = [ |
| 88 | + item({ source: "hackernews", externalId: "a" }), |
| 89 | + item({ source: "github", externalId: "b" }), |
| 90 | + item({ source: "hackernews", externalId: "c" }), |
| 91 | + ]; |
| 92 | + |
| 93 | + it("places ranked items first by cached key order, then appends the rest", () => { |
| 94 | + const merged = mergeRankedKeysOrder(items, [ |
| 95 | + itemKey(items[2]!), |
| 96 | + itemKey(items[0]!), |
| 97 | + ]); |
| 98 | + expect(merged.map((i) => i.externalId)).toEqual(["c", "a", "b"]); |
| 99 | + }); |
| 100 | + |
| 101 | + it("tolerates a cached key for an item that no longer exists", () => { |
| 102 | + const merged = mergeRankedKeysOrder(items, [ |
| 103 | + "hackernews missing-id", |
| 104 | + itemKey(items[1]!), |
| 105 | + ]); |
| 106 | + expect(merged.map((i) => i.externalId)).toEqual(["b", "a", "c"]); |
| 107 | + }); |
| 108 | + |
| 109 | + it("tolerates duplicate cached keys", () => { |
| 110 | + const key = itemKey(items[1]!); |
| 111 | + const merged = mergeRankedKeysOrder(items, [key, key]); |
| 112 | + expect(merged.map((i) => i.externalId)).toEqual(["b", "a", "c"]); |
| 113 | + }); |
| 114 | + |
| 115 | + it("passes through original order when there is no cached ranking", () => { |
| 116 | + expect(mergeRankedKeysOrder(items, []).map((i) => i.externalId)).toEqual([ |
| 117 | + "a", |
| 118 | + "b", |
| 119 | + "c", |
| 120 | + ]); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +describe("FakeLlmRanker + degrade pattern", () => { |
| 125 | + const items = [item({ externalId: "a" }), item({ externalId: "b" })]; |
| 126 | + |
| 127 | + it("a successful rank reorders items", async () => { |
| 128 | + const ranker = new FakeLlmRanker(() => [1, 0]); |
| 129 | + const ranked = await ranker.rank(items, "anything"); |
| 130 | + expect(mergeRankedOrder(items, ranked).map((i) => i.externalId)).toEqual([ |
| 131 | + "b", |
| 132 | + "a", |
| 133 | + ]); |
| 134 | + }); |
| 135 | + |
| 136 | + it("a thrown error is the caller's signal to degrade to chronological order", async () => { |
| 137 | + const ranker = new FakeLlmRanker(() => { |
| 138 | + throw new Error("model unavailable"); |
| 139 | + }); |
| 140 | + await expect(ranker.rank(items, "anything")).rejects.toThrow( |
| 141 | + "model unavailable", |
| 142 | + ); |
| 143 | + }); |
| 144 | +}); |
0 commit comments