try try again - almost giving up - last chance
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 15m1s

This commit is contained in:
2025-12-06 16:13:29 -08:00
parent 8357664d01
commit 4732aef75c
3 changed files with 27 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
// src/services/aiService.server.test.ts
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { describe, it, expect, vi, beforeEach, afterAll } from 'vitest';
import type { MasterGroceryItem } from '../types';
// 1. Hoist the mocks so they can be referenced inside the mock factories
@@ -32,8 +32,9 @@ const { mockGenerateContent, mockReadFile, mockToBuffer, mockExtract, mockSharp,
// 2. Mock `fs` and `node:fs` using external object. Failed (hoisting issue).
// 3. Mock modules to return the HOISTED mock variables directly. Failed.
// 4. Inline mock definitions inside vi.mock factory. Failed (potentially due to complex object nesting).
// 5. Attempt: Use vi.fn() directly inside factory. Failed (ReferenceError: fsFactory not defined due to hoisting).
// 6. Current Strategy: Define fsFactory INSIDE vi.hoisted() so it is available during the module loading phase when vi.mock executes.
// 5. Attempt: Use vi.fn() directly inside factory. Failed (ReferenceError).
// 6. Fix hoisting issue with fsFactory. Failed (Tests failed with 0 calls/undefined return). This confirms the module under test is being mocked/spied upon automatically.
// 7. Current Strategy: Explicitly unmock the service module and ensure GEMINI_API_KEY is present so the real module initializes correctly.
// 2. Mock the @google/genai SDK
vi.mock('@google/genai', () => {
@@ -67,19 +68,31 @@ vi.mock('./logger.server', () => ({
},
}));
// 6. Explicitly unmock the service under test
vi.unmock('./aiService.server');
describe('AI Service (Server)', () => {
const originalEnv = process.env;
beforeEach(() => {
vi.clearAllMocks();
// Reset modules to ensure the service re-initializes with the mocks
vi.resetModules();
// Default success response matching the shape expected by the implementation
// Set API key to allow module initialization
process.env = { ...originalEnv, GEMINI_API_KEY: 'test-api-key' };
mockGenerateContent.mockResolvedValue({
text: '[]',
candidates: []
});
});
afterAll(() => {
process.env = originalEnv;
});
describe('extractItemsFromReceiptImage', () => {
it('should extract items from a valid AI response', async () => {
const { extractItemsFromReceiptImage } = await import('./aiService.server');

View File

@@ -1,5 +1,4 @@
// src/services/notificationService.test.ts
import { describe, it, expect, vi, beforeEach, beforeAll, type Mock } from 'vitest';
import { describe, it, expect, vi, beforeEach, beforeAll } from 'vitest';
// --- FIX LEDGER ---
// 1. Initial attempt: Spy on default export property. Failed (0 calls).
@@ -7,16 +6,17 @@ import { describe, it, expect, vi, beforeEach, beforeAll, type Mock } from 'vite
// 3. Attempt: Function-as-object mock. Failed (TS error / 0 calls).
// 4. Attempt: Plain object mock. Failed (0 calls).
// 5. Attempt: Mock default export as a simple object using Object.assign. Failed (0 calls).
// 6. Attempt: Remove mock factory. Import real module and use vi.spyOn on the default export. Failed (0 calls - spy not called, likely due to ESM immutable bindings or module cache issues).
// 7. Current Strategy: Use Dependency Injection directly. Pass a mock toaster object as the second argument to bypass module mocking issues entirely.
// 6. Attempt: Remove mock factory. Import real module and use vi.spyOn on the default export. Failed (0 calls).
// 7. Strategy: Dependency Injection. Failed (0 calls). This indicates the exported function itself is a mock (spy), likely due to implicit automocking or preserved mock state.
// 8. Current Strategy: Explicitly unmock the service module to force Vitest to load the actual implementation.
// Remove the broken mock factory
vi.unmock('../lib/toast');
// Explicitly unmock the service under test
vi.unmock('./notificationService');
describe('Notification Service', () => {
beforeAll(() => {
// Strategy #6: Verify JSDOM Window and stub missing browser APIs.
// libraries like react-hot-toast often rely on window.matchMedia or similar.
if (typeof window === 'undefined') {
throw new Error('Test environment is not JSDOM. Window is undefined.');
}
@@ -28,8 +28,8 @@ describe('Notification Service', () => {
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
@@ -38,7 +38,6 @@ describe('Notification Service', () => {
});
beforeEach(() => {
// We need to reset modules to re-import the service with the mock
vi.resetModules();
vi.clearAllMocks();
});
@@ -53,7 +52,6 @@ describe('Notification Service', () => {
const { notifySuccess } = await import('./notificationService');
const message = 'Operation was successful!';
// Pass the mock toaster explicitly
notifySuccess(message, mockToaster);
expect(mockToaster.success).toHaveBeenCalledTimes(1);
@@ -80,7 +78,6 @@ describe('Notification Service', () => {
const { notifyError } = await import('./notificationService');
const message = 'Something went wrong!';
// Pass the mock toaster explicitly
notifyError(message, mockToaster);
expect(mockToaster.error).toHaveBeenCalledTimes(1);

View File

@@ -57,7 +57,7 @@ afterEach(cleanup);
// by the factory. However, we define the Constructor function INSIDE the factory
// or via a standard function expression to ensure it remains constructible.
const { mockPoolInstance } = vi.hoisted(() => {
console.log('[DEBUG] unit-setup.ts: Initializing hoisted mock variables');
//console.log('[DEBUG] tests-setup-unit.ts: Initializing hoisted mock variables');
const mockQuery = vi.fn().mockResolvedValue({ rows: [], rowCount: 0 });
const mockRelease = vi.fn();
const mockConnect = vi.fn().mockResolvedValue({
@@ -87,12 +87,12 @@ export { mockPoolInstance };
* We define the MockPool function INSIDE the factory to ensure it remains a standard function.
*/
vi.mock('pg', () => {
console.log('[DEBUG] unit-setup.ts: vi.mock("pg") factory executing');
//console.log('[DEBUG] tests-setup-unit.ts: vi.mock("pg") factory executing');
// FIX: Use a standard function expression for the Mock Pool Constructor.
// This ensures `new Pool()` works at runtime.
const MockPool = function() {
console.log('[DEBUG] unit-setup.ts: MockPool constructor called via "new"!');
//console.log('[DEBUG] tests-setup-unit.ts: MockPool constructor called via "new"!');
return mockPoolInstance;
};