File size: 1,954 Bytes
20f83d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// @vitest-environment node

import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";

const getCachedJson = vi.fn();
vi.mock("../_shared/redis", () => ({
  getCachedJson: (...a: unknown[]) => getCachedJson(...a),
}));

import { getSummarizeArticleCache } from "../worldmonitor/news/v1/get-summarize-article-cache";

const originalFetch = globalThis.fetch;

function makeContext() {
  return {
    request: new Request("https://www.worldmonitor.app/api/news/v1/summarize-article-cache?cache_key=summary:v1:test-key"),
    pathParams: {},
    headers: {},
  };
}

beforeEach(() => {
  getCachedJson.mockReset();
  globalThis.fetch = vi.fn(async () => {
    throw new Error("cache lookup must not call providers");
  }) as typeof fetch;
});

afterEach(() => {
  globalThis.fetch = originalFetch;
});

describe("summarize-article-cache read-only behavior", () => {
  test("returns cached summaries without calling provider fetch", async () => {
    getCachedJson.mockResolvedValue({ summary: "Cached brief", model: "llama", tokens: 123 });

    const result = await getSummarizeArticleCache(makeContext(), {
      cacheKey: "summary:v1:test-key",
    });

    expect(result).toMatchObject({
      summary: "Cached brief",
      model: "llama",
      provider: "cache",
      tokens: 0,
      fallback: false,
      status: "SUMMARIZE_STATUS_CACHED",
    });
    expect(getCachedJson).toHaveBeenCalledWith("summary:v1:test-key");
    expect(globalThis.fetch).not.toHaveBeenCalled();
  });

  test("returns an empty miss without calling provider fetch", async () => {
    getCachedJson.mockResolvedValue(null);

    const result = await getSummarizeArticleCache(makeContext(), {
      cacheKey: "summary:v1:test-key",
    });

    expect(result).toMatchObject({
      summary: "",
      provider: "",
      fallback: true,
      status: "SUMMARIZE_STATUS_UNSPECIFIED",
    });
    expect(globalThis.fetch).not.toHaveBeenCalled();
  });
});