ionian/packages/server/tests/error_handler.test.ts
monoid d28c255d21 feat: add app configuration management with Kysely integration
- Implemented `getAppConfig` and `upsertAppConfig` functions in `config.ts` for managing application settings in the database.
- Updated `mod.ts` to export the new configuration functions.
- Refactored `ComicConfig.ts` to load and update comic watch paths using the new configuration functions.
- Modified `comic_watcher.ts` to accept paths as parameters for creating watchers.
- Created a new settings router in `settings.ts` for managing application settings via HTTP requests.
- Integrated the settings router into the main server in `server.ts`.
- Updated the settings management to use the new database-backed configuration.
- Removed legacy configuration management code from `configRW.ts`.
- Added integration tests for the settings router and error handling.
- Updated `vitest` configuration for testing.
- Cleaned up unused type definitions in `pnpm-lock.yaml`.
2025-09-30 23:15:20 +09:00

59 lines
1.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { ClientRequestError, error_handler } from "../src/route/error_handler.ts";
import { DocumentBodySchema } from "dbtype";
const createSet = () => ({ status: undefined as number | string | undefined });
describe("error_handler", () => {
it("formats ClientRequestError with provided status", () => {
const set = createSet();
const result = error_handler({
code: "UNKNOWN",
error: new ClientRequestError(400, "invalid payload"),
set,
});
expect(set.status).toBe(400);
expect(result).toEqual({
code: 400,
message: "BadRequest",
detail: "invalid payload",
});
});
it("coerces ZodError into a 400 response", () => {
const parseResult = DocumentBodySchema.safeParse({});
const set = createSet();
if (parseResult.success) {
throw new Error("Expected validation error");
}
const result = error_handler({
code: "VALIDATION",
error: parseResult.error,
set,
});
expect(set.status).toBe(400);
expect(result.code).toBe(400);
expect(result.message).toBe("BadRequest");
expect(result.detail).toContain("Required");
});
it("defaults to 500 for unexpected errors", () => {
const set = createSet();
const result = error_handler({
code: "INTERNAL_SERVER_ERROR",
error: new Error("boom"),
set,
});
expect(set.status).toBe(500);
expect(result).toEqual({
code: 500,
message: "Internal Server Error",
detail: "boom",
});
});
});