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

This commit is contained in:
2025-12-06 15:48:37 -08:00
parent ceba599722
commit 8357664d01
2 changed files with 32 additions and 28 deletions

View File

@@ -3,19 +3,27 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
import type { MasterGroceryItem } from '../types';
// 1. Hoist the mocks so they can be referenced inside the mock factories
const { mockGenerateContent, mockReadFile, mockToBuffer, mockExtract, mockSharp } = vi.hoisted(() => {
const { mockGenerateContent, mockReadFile, mockToBuffer, mockExtract, mockSharp, fsFactory } = vi.hoisted(() => {
const generateContent = vi.fn();
const readFile = vi.fn();
const toBuffer = vi.fn();
const extract = vi.fn(() => ({ toBuffer }));
const sharp = vi.fn(() => ({ extract }));
// Define fsFactory inside hoisted scope to avoid ReferenceError during vi.mock execution
const fsFactory = () => ({
default: { readFile },
readFile,
promises: { readFile }
});
return {
mockGenerateContent: generateContent,
mockReadFile: readFile,
mockToBuffer: toBuffer,
mockExtract: extract,
mockSharp: sharp
mockSharp: sharp,
fsFactory
};
});
@@ -24,7 +32,8 @@ 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. Current Strategy: Use vi.fn() directly inside factory for GoogleGenAI and a unified factory for FS to ensure consistency.
// 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.
// 2. Mock the @google/genai SDK
vi.mock('@google/genai', () => {
@@ -37,13 +46,7 @@ vi.mock('@google/genai', () => {
};
});
// 3. Robust mock for file system operations
const fsFactory = () => ({
default: { readFile: mockReadFile },
readFile: mockReadFile,
promises: { readFile: mockReadFile }
});
// 3. Robust mock for file system operations using the hoisted factory
vi.mock('fs', fsFactory);
vi.mock('node:fs', fsFactory);
vi.mock('fs/promises', fsFactory);

View File

@@ -1,3 +1,4 @@
// src/services/notificationService.test.ts
import { describe, it, expect, vi, beforeEach, beforeAll, type Mock } from 'vitest';
// --- FIX LEDGER ---
@@ -6,7 +7,8 @@ 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. Current Strategy: Remove mock factory. Import real module and use vi.spyOn on the default export in the test body.
// 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.
// Remove the broken mock factory
vi.unmock('../lib/toast');
@@ -43,21 +45,19 @@ describe('Notification Service', () => {
describe('notifySuccess', () => {
it('should call the injected toaster.success with correct options', async () => {
// 1. Import the dependency first
const toastModule = await import('../lib/toast');
const toast = toastModule.default;
// 2. Setup the spy BEFORE importing the service under test
const successSpy = vi.spyOn(toast, 'success').mockImplementation(() => 'id');
const mockToaster = {
success: vi.fn(),
error: vi.fn(),
};
// 3. Import the service (which will use the spied toast object)
const { notifySuccess } = await import('./notificationService');
const message = 'Operation was successful!';
notifySuccess(message);
// Pass the mock toaster explicitly
notifySuccess(message, mockToaster);
expect(successSpy).toHaveBeenCalledTimes(1);
expect(successSpy).toHaveBeenCalledWith(
expect(mockToaster.success).toHaveBeenCalledTimes(1);
expect(mockToaster.success).toHaveBeenCalledWith(
message,
expect.objectContaining({
style: expect.any(Object),
@@ -72,18 +72,19 @@ describe('Notification Service', () => {
describe('notifyError', () => {
it('should call the injected toaster.error with correct options', async () => {
const toastModule = await import('../lib/toast');
const toast = toastModule.default;
const errorSpy = vi.spyOn(toast, 'error').mockImplementation(() => 'id');
const mockToaster = {
success: vi.fn(),
error: vi.fn(),
};
const { notifyError } = await import('./notificationService');
const message = 'Something went wrong!';
notifyError(message);
// Pass the mock toaster explicitly
notifyError(message, mockToaster);
expect(errorSpy).toHaveBeenCalledTimes(1);
expect(errorSpy).toHaveBeenCalledWith(
expect(mockToaster.error).toHaveBeenCalledTimes(1);
expect(mockToaster.error).toHaveBeenCalledWith(
message,
expect.objectContaining({
style: expect.any(Object),