test(core): strengthen app and auth assertions

This commit is contained in:
root 2026-03-14 14:25:18 +00:00
parent 9a746dfffa
commit 8ad3e1be34
2 changed files with 27 additions and 9 deletions

View File

@ -32,8 +32,8 @@ describe('createServiceApp', () => {
expect(body.service).toBe('my-service');
expect(body.version).toBe('1.2.3');
expect(body.description).toBe('A test service');
expect(body.timestamp).toBeTruthy();
expect(body.requestId).toBeTruthy();
expect(body.timestamp).toMatch(/^\d{4}-\d{2}-\d{2}T/);
expect(body.requestId).toBe(res.headers['x-request-id']);
await app.close();
});
@ -72,6 +72,8 @@ describe('createServiceApp', () => {
expect(res.headers['x-request-id']).toMatch(
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
);
const body = JSON.parse(res.payload);
expect(body.requestId).toBe(res.headers['x-request-id']);
await app.close();
});
@ -92,7 +94,7 @@ describe('createServiceApp', () => {
expect(res.statusCode).toBe(404);
const body = JSON.parse(res.payload);
expect(body.error).toBe('User not found');
expect(body.requestId).toBeTruthy();
expect(body.requestId).toBe(res.headers['x-request-id']);
await app.close();
});
@ -114,7 +116,7 @@ describe('createServiceApp', () => {
const body = JSON.parse(res.payload);
expect(body.error).toBe('Validation failed');
expect(body.details).toEqual({ field: 'email' });
expect(body.requestId).toBeTruthy();
expect(body.requestId).toBe(res.headers['x-request-id']);
await app.close();
});
@ -134,7 +136,7 @@ describe('createServiceApp', () => {
expect(res.statusCode).toBe(500);
const body = JSON.parse(res.payload);
expect(body.error).toBe('Internal server error');
expect(body.requestId).toBeTruthy();
expect(body.requestId).toBe(res.headers['x-request-id']);
await app.close();
});
@ -166,6 +168,8 @@ describe('createServiceApp', () => {
expect(JSON.parse(before.payload)).toMatchObject({
status: 'not_ready',
service: 'ready-test',
version: '1.0.0',
requestId: before.headers['x-request-id'],
});
app.setReadyState(true);
@ -175,6 +179,8 @@ describe('createServiceApp', () => {
expect(JSON.parse(after.payload)).toMatchObject({
status: 'ready',
service: 'ready-test',
version: '1.0.0',
requestId: after.headers['x-request-id'],
});
await app.close();

View File

@ -67,8 +67,7 @@ describe('createAuthProvider', () => {
<div data-testid="child">Hello</div>
</AuthProvider>
);
expect(screen.getByTestId('child')).toBeDefined();
expect(screen.getByText('Hello')).toBeDefined();
expect(screen.getByTestId('child').textContent).toBe('Hello');
});
it('starts unauthenticated with no stored user', () => {
@ -159,6 +158,13 @@ describe('createAuthProvider', () => {
expect(result).toBe(true);
expect(screen.getByTestId('auth').textContent).toBe('true');
expect(screen.getByTestId('email').textContent).toBe('test@example.com');
expect(mockFetch).toHaveBeenCalledWith(
'/api/auth/login',
expect.objectContaining({
method: 'POST',
body: JSON.stringify({ email: 'test@example.com', password: 'pass123' }),
})
);
expect(localStorageMock.setItem).toHaveBeenCalledWith(
'test_auth_user',
expect.stringContaining('test@example.com')
@ -179,9 +185,14 @@ describe('createAuthProvider', () => {
let loginFn: (email: string, password: string) => Promise<boolean>;
function LoginComponent() {
const { login, isAuthenticated } = useAuth();
const { login, isAuthenticated, error } = useAuth();
loginFn = login;
return <span data-testid="auth">{String(isAuthenticated)}</span>;
return (
<div>
<span data-testid="auth">{String(isAuthenticated)}</span>
<span data-testid="error">{error ?? 'none'}</span>
</div>
);
}
render(
@ -197,6 +208,7 @@ describe('createAuthProvider', () => {
expect(result).toBe(false);
expect(screen.getByTestId('auth').textContent).toBe('false');
expect(screen.getByTestId('error').textContent).toBe('Unauthorized');
});
it('logout clears user and storage', async () => {