Files
promptfoo--promptfoo/test/server/findStaticDir.test.ts
T
wehub-resource-sync 0d3cb498a3
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Waiting to run
Test and Publish Multi-arch Docker Image / test (push) Waiting to run
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Blocked by required conditions
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Blocked by required conditions
Validate Renovate Config / Validate Renovate Configuration (push) Waiting to run
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:24:08 +08:00

143 lines
4.7 KiB
TypeScript

import path from 'node:path';
import type { PathLike } from 'node:fs';
import { beforeEach, describe, expect, it, vi } from 'vitest';
// Mock fs before importing the module - spread original to keep other functions working
vi.mock('node:fs', async (importOriginal) => {
const actual = await importOriginal<typeof import('node:fs')>();
return {
...actual,
default: {
...actual,
existsSync: vi.fn(),
},
};
});
vi.mock('../../src/esm', () => ({
getDirectory: vi.fn(),
}));
vi.mock('../../src/logger', () => ({
default: {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
debug: vi.fn(),
},
}));
// Mock other server dependencies to prevent initialization errors
vi.mock('../../src/migrate', () => ({
runDbMigrations: vi.fn().mockResolvedValue(undefined),
}));
vi.mock('../../src/database/signal', () => ({
setupSignalWatcher: vi.fn().mockReturnValue({ close: vi.fn(), on: vi.fn() }),
readSignalFile: vi.fn().mockReturnValue({ type: 'update' }),
}));
vi.mock('../../src/util/server', () => ({
BrowserBehavior: { OPEN: 0, SKIP: 1, ASK: 2 },
BrowserBehaviorNames: { 0: 'OPEN', 1: 'SKIP', 2: 'ASK' },
openBrowser: vi.fn().mockResolvedValue(undefined),
}));
vi.mock('../../src/models/eval', () => ({
default: { latest: vi.fn().mockResolvedValue(null) },
getEvalSummaries: vi.fn().mockResolvedValue([]),
}));
vi.mock('../../src/globalConfig/cloud', () => ({
cloudConfig: { isEnabled: vi.fn().mockReturnValue(false) },
}));
import fs from 'node:fs';
import { getDirectory } from '../../src/esm';
import logger from '../../src/logger';
import { findStaticDir } from '../../src/server/server';
describe('findStaticDir', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('returns standard path when index.html exists there (development)', () => {
// Simulate development: getDirectory returns 'src/', app is at 'src/app/'
vi.mocked(getDirectory).mockReturnValue('/project/src');
vi.mocked(fs.existsSync).mockImplementation((filePath: PathLike) => {
// Standard path check: /project/src/app/index.html
return String(filePath) === path.join('/project/src', 'app', 'index.html');
});
const result = findStaticDir();
expect(result).toBe(path.join('/project/src', 'app'));
expect(logger.debug).not.toHaveBeenCalled();
expect(logger.warn).not.toHaveBeenCalled();
});
it('returns parent path when bundled (standard path missing, parent exists)', () => {
// Simulate bundled: getDirectory returns 'dist/src/server/', app is at 'dist/src/app/'
vi.mocked(getDirectory).mockReturnValue('/project/dist/src/server');
vi.mocked(fs.existsSync).mockImplementation((filePath: PathLike) => {
const pathStr = String(filePath);
// Standard path doesn't exist
if (pathStr === path.join('/project/dist/src/server', 'app', 'index.html')) {
return false;
}
// Parent path exists: /project/dist/src/app/index.html
if (pathStr === path.resolve('/project/dist/src/server', '..', 'app', 'index.html')) {
return true;
}
return false;
});
const result = findStaticDir();
expect(result).toBe(path.resolve('/project/dist/src/server', '..', 'app'));
expect(logger.debug).toHaveBeenCalledWith(
expect.stringContaining('Static directory resolved to parent'),
);
expect(logger.warn).not.toHaveBeenCalled();
});
it('falls back to standard path with warning when neither path exists', () => {
vi.mocked(getDirectory).mockReturnValue('/project/dist/src/server');
vi.mocked(fs.existsSync).mockReturnValue(false);
const result = findStaticDir();
// Should return standard path as fallback
expect(result).toBe(path.join('/project/dist/src/server', 'app'));
expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Static directory not found'));
});
it('checks index.html in both paths before falling back', () => {
vi.mocked(getDirectory).mockReturnValue('/test/dir');
vi.mocked(fs.existsSync).mockReturnValue(false);
findStaticDir();
// Should have checked both paths
expect(fs.existsSync).toHaveBeenCalledWith(path.join('/test/dir', 'app', 'index.html'));
expect(fs.existsSync).toHaveBeenCalledWith(
path.resolve('/test/dir', '..', 'app', 'index.html'),
);
});
it('prefers standard path over parent path when both exist', () => {
vi.mocked(getDirectory).mockReturnValue('/project/src');
// Both paths have index.html
vi.mocked(fs.existsSync).mockReturnValue(true);
const result = findStaticDir();
// Should return standard path (first check wins)
expect(result).toBe(path.join('/project/src', 'app'));
expect(logger.debug).not.toHaveBeenCalled();
});
});