Files
fusion/packages/dashboard/vitest.setup.ts
gsxdsm c671beae96 feat(KB-503): add multi-project CLI support with project subcommands
- Add project subcommands: list, add, remove, show, set-default, detect
- Add --project flag support for all task commands
- Create project-context.ts utilities for project resolution
- Add defaultProjectId to GlobalSettings type
- Implement project auto-detection from cwd
- Update CLI argument parsing for global --project flag
- Add multi-project CLI documentation to AGENTS.md
2026-04-01 07:02:15 -07:00

91 lines
2.4 KiB
TypeScript

import "@testing-library/jest-dom";
import { vi } from "vitest";
// Mock localStorage
const localStorageMock: Record<string, string> = {};
if (typeof window !== "undefined") {
Object.defineProperty(window, "localStorage", {
value: {
getItem: (key: string) => localStorageMock[key] || null,
setItem: (key: string, value: string) => {
localStorageMock[key] = value;
},
removeItem: (key: string) => {
delete localStorageMock[key];
},
clear: () => {
Object.keys(localStorageMock).forEach((key) => delete localStorageMock[key]);
},
},
writable: true,
});
// Mock matchMedia
Object.defineProperty(window, "matchMedia", {
writable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches: query === "(prefers-color-scheme: dark)" ? true : false,
media: query,
onchange: null,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
}
// Global MockEventSource for tests
class MockEventSource {
static instances: MockEventSource[] = [];
static CONNECTING = 0;
static OPEN = 1;
static CLOSED = 2;
url: string;
listeners: Record<string, ((e: any) => void)[]> = {};
readyState = 0;
close = vi.fn(() => {
this.readyState = MockEventSource.CLOSED;
});
constructor(url: string) {
this.url = url;
this.readyState = MockEventSource.OPEN;
MockEventSource.instances.push(this);
}
addEventListener(event: string, fn: (e: any) => void) {
if (!this.listeners[event]) this.listeners[event] = [];
this.listeners[event].push(fn);
}
removeEventListener(event: string, fn: (e: any) => void) {
this.listeners[event] = (this.listeners[event] || []).filter((listener) => listener !== fn);
}
// Helper to simulate a server event
_emit(event: string, data?: unknown) {
for (const fn of this.listeners[event] || []) {
fn(data === undefined ? ({ } as { data: string }) : { data: JSON.stringify(data) });
}
}
}
// Set up before each test
beforeEach(() => {
MockEventSource.instances = [];
(globalThis as any).EventSource = MockEventSource;
});
// Clean up after each test
afterEach(() => {
// Close all lingering EventSource instances
for (const instance of MockEventSource.instances) {
instance.close();
}
MockEventSource.instances = [];
delete (globalThis as any).EventSource;
});
export { MockEventSource };