wishforge-ide/apps/web/src/lib/readiness.test.ts
eust-w 6fc7eaf00c ✨ Improve local WishForge reliability and LAN access
Detect actual service and Runner readiness, restore LAN access after network changes, protect production web builds with rollback, honor cancellation during executor startup, and show truthful cached-data and credential states.
2026-10-25 03:29:51 +08:00

65 lines
3.0 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import type { ProviderInfo } from './types';
import { credentialSourceLabel, initialWishProvider, resourceState, wishProviderReady } from './readiness';
const codex: ProviderInfo = {
id: 'codex',
name: 'Codex',
configured: false,
enabled: false,
credentialSource: 'none',
capabilities: {},
};
const claude: ProviderInfo = {
id: 'claude-code',
name: 'Claude Code',
configured: true,
enabled: true,
credentialSource: 'native_account',
capabilities: {},
};
describe('control-plane connection display', () => {
it('never treats cached data as proof of connectivity after failure', () => {
expect(resourceState(true, true, false)).toBe('stale');
expect(resourceState(true, true, true)).toBe('stale');
expect(resourceState(false, true, false)).toBe('unavailable');
});
it('distinguishes initial loading, background validation and confirmed data', () => {
expect(resourceState(false, false, true)).toBe('loading');
expect(resourceState(true, false, true)).toBe('refreshing');
expect(resourceState(true, false, false)).toBe('ready');
});
});
describe('new-wish executor selection and submission boundary', () => {
it('selects the available executor when the ordinary preferred Codex is unconfigured', () => {
expect(initialWishProvider([codex, claude], false)).toBe('claude-code');
expect(initialWishProvider([claude, { ...codex, configured: true, enabled: true }], false)).toBe('codex');
});
it('prefers available native Claude for the POC but does not impose operator policy from a UI hint', () => {
expect(initialWishProvider([codex, claude], true)).toBe('claude-code');
expect(initialWishProvider([{ ...codex, configured: true, enabled: true }], true)).toBe('codex');
});
it('blocks unknown, disabled or contradictory provider state', () => {
expect(wishProviderReady(undefined, undefined, undefined)).toBe(false);
expect(wishProviderReady(codex, undefined, undefined)).toBe(false);
expect(wishProviderReady({ ...claude, configured: false }, undefined, undefined)).toBe(false);
});
it('blocks submission after a provider or project read fails even with cached ready metadata', () => {
expect(wishProviderReady(claude, new Error('offline'), undefined)).toBe(false);
expect(wishProviderReady(claude, undefined, new Error('forbidden'))).toBe(false);
expect(wishProviderReady(claude, undefined, undefined)).toBe(true);
});
});
describe('credential source disclosure', () => {
it('does not turn mixed or missing metadata into a native/tenant authentication claim', () => {
expect(credentialSourceLabel('native_account')).toContain('本机 CLI');
expect(credentialSourceLabel('native_account')).toContain('启动时验证');
expect(credentialSourceLabel('mixed')).toContain('认证来源待确认');
expect(credentialSourceLabel(undefined)).toContain('尚未确认');
expect(credentialSourceLabel('tenant_credential')).toContain('租户 API Key');
});
});