|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import { rateHallucination } from "./hallucination_autorater"; |
| 16 | +import { VertexModel } from "../../src/models/vertex_model"; |
| 17 | +import { StatementWithComments } from "./autorating_utils"; |
| 18 | +import fs from "fs"; |
| 19 | +import * as path from "path"; |
| 20 | + |
| 21 | +jest.mock("../../src/models/vertex_model"); // Mock the VertexModel |
| 22 | + |
| 23 | +describe("rateHallucination", () => { |
| 24 | + let mockModel: jest.Mocked<VertexModel>; |
| 25 | + const mockOutputDir = "test_output"; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + mockModel = new VertexModel("", "", "") as jest.Mocked<VertexModel>; // Create a mocked instance |
| 29 | + // Ensure output directory exists and is empty |
| 30 | + if (!fs.existsSync(mockOutputDir)) { |
| 31 | + fs.mkdirSync(mockOutputDir); |
| 32 | + } else { |
| 33 | + fs.rmSync(mockOutputDir, { recursive: true, force: true }); // Clean up after previous tests |
| 34 | + fs.mkdirSync(mockOutputDir); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + afterEach(() => { |
| 39 | + fs.rmSync(mockOutputDir, { recursive: true, force: true }); // Clean up after each test |
| 40 | + }); |
| 41 | + |
| 42 | + it("should correctly process summaries and generate report", async () => { |
| 43 | + const summaries: StatementWithComments[] = [ |
| 44 | + { statement: "Statement 1", comments: "Comment 1" }, |
| 45 | + { statement: "Statement 2", comments: "Comment 2" }, |
| 46 | + ]; |
| 47 | + const mockResponseData = { |
| 48 | + analysis: "Test analysis", |
| 49 | + answer: "YES", |
| 50 | + explanation: "Test explanation", |
| 51 | + }; |
| 52 | + mockModel.generateData.mockResolvedValue(mockResponseData); // Mock generateData to resolve with mock data |
| 53 | + |
| 54 | + await rateHallucination(mockModel, summaries, mockOutputDir); |
| 55 | + |
| 56 | + // Check if the files were created |
| 57 | + const csvPath = path.join(mockOutputDir, "hallucination_autoratings.csv"); |
| 58 | + const reportPath = path.join(mockOutputDir, "hallucination_report.txt"); |
| 59 | + expect(fs.existsSync(csvPath)).toBe(true); |
| 60 | + expect(fs.existsSync(reportPath)).toBe(true); |
| 61 | + |
| 62 | + // Check some of the CSV content and aggregated results |
| 63 | + const csvContent = fs.readFileSync(csvPath, "utf8"); |
| 64 | + expect(csvContent).toContain("Statement 1"); |
| 65 | + expect(csvContent).toContain("YES"); // Hallucination result for Statement 1 |
| 66 | + |
| 67 | + // Check report content |
| 68 | + const reportContent = fs.readFileSync(reportPath, "utf8"); |
| 69 | + expect(reportContent).toContain("Summary Evaluation Report"); |
| 70 | + expect(reportContent).toContain("Total statements: 2"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should handle LLM errors gracefully", async () => { |
| 74 | + const summaries: StatementWithComments[] = [ |
| 75 | + { statement: "Statement 1", comments: "Comment 1" }, |
| 76 | + ]; |
| 77 | + mockModel.generateData.mockRejectedValue(new Error("LLM Error")); // Mock an LLM error |
| 78 | + const consoleErrorSpy = jest.spyOn(console, "error"); |
| 79 | + |
| 80 | + await rateHallucination(mockModel, summaries, mockOutputDir); |
| 81 | + |
| 82 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 83 | + "Error during LLM call or parsing:", |
| 84 | + expect.any(Error) |
| 85 | + ); |
| 86 | + |
| 87 | + // Check for NULL values in CSV due to the error |
| 88 | + const csvPath = path.join(mockOutputDir, "hallucination_autoratings.csv"); |
| 89 | + expect(fs.existsSync(csvPath)).toBe(true); |
| 90 | + const csvContent = fs.readFileSync(csvPath, "utf8"); |
| 91 | + expect(csvContent).toContain("NULL"); |
| 92 | + |
| 93 | + consoleErrorSpy.mockRestore(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should handle invalid responses from LLM", async () => { |
| 97 | + const summaries: StatementWithComments[] = [ |
| 98 | + { statement: "Statement 1", comments: "Comment 1" }, |
| 99 | + ]; |
| 100 | + mockModel.generateData.mockResolvedValue(null); // Mock invalid response |
| 101 | + const consoleWarnSpy = jest.spyOn(console, "warn"); |
| 102 | + |
| 103 | + await rateHallucination(mockModel, summaries, mockOutputDir); |
| 104 | + |
| 105 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 106 | + "Skipping statement due to LLM error or invalid response." |
| 107 | + ); |
| 108 | + consoleWarnSpy.mockRestore(); |
| 109 | + }); |
| 110 | +}); |
0 commit comments