import process from "node:process"; import { readFileSync } from "node:fs"; import { OpenAI } from "openai"; import { type MultiAttemptExample } from "../../../lib/schemas"; import { MultiAttemptStrategy } from "../../../lib/support/offer-detection"; const multiAttemptExamplesPath = "./data/prompt/multi-attempt/examples.json"; const testCasesPath = "./data/prompt/multi-attempt/test.json"; async function runTestCases( examples: MultiAttemptExample[], outputFilePath: string ) { const tasks = examples.map(async (example) => { const maStrat = createStrategy(example); const result = await maStrat.shouldOfferSupport(); const resultString = `${JSON.stringify(example, null, 2)}\n${JSON.stringify(result, null, 2)}`; appendToFile(resultString, outputFilePath); if (example.offerSupport !== result.offerSupport) { console.log( `Failed tests: ID: ${example.id},\nDescription: ${example.description}\nExpected: ${example.offerSupport}\nReceived: ${result.offerSupport}` ); } expect(result.offerSupport).toBe(example.offerSupport); }); await Promise.allSettled(tasks); } function createStrategy(example: MultiAttemptExample): MultiAttemptStrategy { return new MultiAttemptStrategy( { chatHistory: example.chatHistory ?? [], environmentContext: "", environmentMetadata: {}, }, new OpenAI({ apiKey: process.env.OPENAI_API_KEY }) ); } function readFromFile(path: string): MultiAttemptExample[] { try { const fileContent = readFileSync(path, "utf8"); return JSON.parse(fileContent) as MultiAttemptExample[]; } catch (error) { if (error instanceof Error) { throw new TypeError(`Unable to read file: ${path}, ${error.message}`); } throw new Error(`Unable to read file: ${path}`); } } async function appendToFile(content: string, filePath: string) { const timestamp = new Date().toISOString(); const formattedContent = `${timestamp}:\n${content}\n`; try { const fs = require("node:fs").promises; // Using promises for async file operations await fs.appendFile(filePath, formattedContent); } catch (error) { console.error(`Error appending to file: ${filePath}`, error); } } test("Test self examples", async () => { const examples = readFromFile(multiAttemptExamplesPath); await runTestCases(examples, "./out/multi-attempt/self_examples.txt"); }); test("Test new scenarios", async () => { const examples = readFromFile(testCasesPath); await runTestCases(examples, "./out/multi-attempt/test_results.txt"); });