Files
bambuddy/frontend/src/__tests__/setup.ts
T
maziggy 104b9d4a25 Fix external spool ams_mapping2 slot_id (Issue #213)
The ams_mapping2 format was incorrectly using the tray_id (254/255) as
the slot_id for external spools. The printer expects slot_id to be the
actual slot index (0 for main nozzle, 1 for deputy nozzle), not the
tray_id value.

Before: {"ams_id": 255, "slot_id": 254}  <- invalid slot index
After:  {"ams_id": 255, "slot_id": 0}    <- correct slot index

This caused prints using external spool to fail immediately with error
code 07FF_8007.

Closes #213
2026-02-03 09:29:13 -05:00

103 lines
2.6 KiB
TypeScript

/**
* Test setup file for Vitest.
* Configures testing environment, mocks, and MSW server.
*/
import '@testing-library/jest-dom';
import { afterAll, afterEach, beforeAll, vi } from 'vitest';
import { cleanup } from '@testing-library/react';
import { server } from './mocks/server';
// Initialize i18n for tests (suppresses react-i18next warnings)
import '../i18n';
// Setup MSW server
beforeAll(() =>
server.listen({
// Bypass unhandled requests silently (don't warn, just let them through)
// Handlers use wildcard (*) prefix to match any origin
onUnhandledRequest: 'bypass',
})
);
afterEach(() => {
cleanup();
server.resetHandlers();
});
afterAll(() => server.close());
// Mock window.matchMedia for responsive components
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
// Mock ResizeObserver
class ResizeObserverMock {
observe = vi.fn();
unobserve = vi.fn();
disconnect = vi.fn();
}
vi.stubGlobal('ResizeObserver', ResizeObserverMock);
// Mock IntersectionObserver
class IntersectionObserverMock {
observe = vi.fn();
unobserve = vi.fn();
disconnect = vi.fn();
root = null;
rootMargin = '';
thresholds = [];
}
vi.stubGlobal('IntersectionObserver', IntersectionObserverMock);
// Mock WebSocket
class MockWebSocket {
static readonly CONNECTING = 0;
static readonly OPEN = 1;
static readonly CLOSING = 2;
static readonly CLOSED = 3;
readyState = MockWebSocket.OPEN;
onopen: ((event: Event) => void) | null = null;
onclose: ((event: CloseEvent) => void) | null = null;
onmessage: ((event: MessageEvent) => void) | null = null;
onerror: ((event: Event) => void) | null = null;
url: string;
constructor(url: string) {
this.url = url;
setTimeout(() => this.onopen?.(new Event('open')), 0);
}
send = vi.fn();
close = vi.fn();
}
vi.stubGlobal('WebSocket', MockWebSocket);
// Mock scrollTo
window.scrollTo = vi.fn();
// Mock localStorage
const localStorageMock = {
getItem: vi.fn(),
setItem: vi.fn(),
removeItem: vi.fn(),
clear: vi.fn(),
};
Object.defineProperty(window, 'localStorage', { value: localStorageMock });
// Suppress console output during tests (reduces noise)
// Remove these lines if you need to debug test output
vi.spyOn(console, 'log').mockImplementation(() => {});
vi.spyOn(console, 'warn').mockImplementation(() => {});
vi.spyOn(console, 'error').mockImplementation(() => {});