File size: 2,949 Bytes
6c3af4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { beforeEach, describe, expect, it, vi } from "vitest";
import { generateFollowUpQuestion } from "./followUpQuestions";

vi.mock("./pubSub", () => ({
  getSuppressNextFollowUp: vi.fn(() => false),
}));

vi.mock("./textGeneration", () => ({
  generateChatResponse: vi.fn(),
}));

import { getSuppressNextFollowUp } from "./pubSub";
import { generateChatResponse } from "./textGeneration";

const mockedGenerateChatResponse = vi.mocked(generateChatResponse);
const mockedGetSuppressNextFollowUp = vi.mocked(getSuppressNextFollowUp);

beforeEach(() => {
  vi.clearAllMocks();
  mockedGetSuppressNextFollowUp.mockReturnValue(false);
});

describe("generateFollowUpQuestion", () => {
  it("keeps an English question with leading numbering", async () => {
    mockedGenerateChatResponse.mockResolvedValue(
      "1. What about the alternatives?",
    );

    const question = await generateFollowUpQuestion({
      topic: "cats",
      currentContent: "Cats are independent.",
    });

    expect(question).toBe("What about the alternatives?");
  });

  it("keeps a Chinese question ending in a fullwidth question mark", async () => {
    mockedGenerateChatResponse.mockResolvedValue("你觉得这个方案怎么样?");

    const question = await generateFollowUpQuestion({
      topic: "方案",
      currentContent: "方案可行。",
    });

    expect(question).toBe("你觉得这个方案怎么样?");
  });

  it("strips a leading bullet from a Japanese question without touching the text", async () => {
    mockedGenerateChatResponse.mockResolvedValue(
      "- この案についてどう思いますか?",
    );

    const question = await generateFollowUpQuestion({
      topic: "案",
      currentContent: "案は実行可能です。",
    });

    expect(question).toBe("この案についてどう思いますか?");
  });

  it("keeps an Arabic question ending in an Arabic question mark", async () => {
    mockedGenerateChatResponse.mockResolvedValue("ما رأيك في البدائل؟");

    const question = await generateFollowUpQuestion({
      topic: "البدائل",
      currentContent: "البدائل متاحة.",
    });

    expect(question).toBe("ما رأيك في البدائل؟");
  });

  it("returns an empty string when no line ends with a question mark", async () => {
    mockedGenerateChatResponse.mockResolvedValue("No questions here.");

    const question = await generateFollowUpQuestion({
      topic: "cats",
      currentContent: "Cats are independent.",
    });

    expect(question).toBe("");
  });

  it("returns an empty string when follow-up questions are suppressed", async () => {
    mockedGetSuppressNextFollowUp.mockReturnValue(true);

    const question = await generateFollowUpQuestion({
      topic: "cats",
      currentContent: "Cats are independent.",
    });

    expect(mockedGenerateChatResponse).not.toHaveBeenCalled();
    expect(question).toBe("");
  });
});