mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-01-31 06:42:03 +00:00
- Removed the old spec regeneration routes and replaced them with a new structure under the app-spec directory for better modularity. - Introduced unit tests for common functionalities in app-spec, covering state management and error handling. - Added documentation on route organization patterns to improve maintainability and clarity for future development.
104 lines
3.4 KiB
TypeScript
104 lines
3.4 KiB
TypeScript
import { describe, it, expect, beforeEach } from "vitest";
|
|
import {
|
|
setRunningState,
|
|
getErrorMessage,
|
|
isRunning,
|
|
currentAbortController,
|
|
} from "@/routes/app-spec/common.js";
|
|
|
|
describe("app-spec/common.ts", () => {
|
|
beforeEach(() => {
|
|
// Reset state before each test
|
|
setRunningState(false, null);
|
|
});
|
|
|
|
describe("setRunningState", () => {
|
|
it("should set isRunning to true when running is true", () => {
|
|
setRunningState(true);
|
|
expect(isRunning).toBe(true);
|
|
});
|
|
|
|
it("should set isRunning to false when running is false", () => {
|
|
setRunningState(true);
|
|
setRunningState(false);
|
|
expect(isRunning).toBe(false);
|
|
});
|
|
|
|
it("should set currentAbortController when provided", () => {
|
|
const controller = new AbortController();
|
|
setRunningState(true, controller);
|
|
expect(currentAbortController).toBe(controller);
|
|
});
|
|
|
|
it("should set currentAbortController to null when not provided", () => {
|
|
const controller = new AbortController();
|
|
setRunningState(true, controller);
|
|
setRunningState(false);
|
|
expect(currentAbortController).toBe(null);
|
|
});
|
|
|
|
it("should set currentAbortController to null when explicitly passed null", () => {
|
|
const controller = new AbortController();
|
|
setRunningState(true, controller);
|
|
setRunningState(true, null);
|
|
expect(currentAbortController).toBe(null);
|
|
});
|
|
|
|
it("should update state multiple times correctly", () => {
|
|
const controller1 = new AbortController();
|
|
const controller2 = new AbortController();
|
|
|
|
setRunningState(true, controller1);
|
|
expect(isRunning).toBe(true);
|
|
expect(currentAbortController).toBe(controller1);
|
|
|
|
setRunningState(true, controller2);
|
|
expect(isRunning).toBe(true);
|
|
expect(currentAbortController).toBe(controller2);
|
|
|
|
setRunningState(false, null);
|
|
expect(isRunning).toBe(false);
|
|
expect(currentAbortController).toBe(null);
|
|
});
|
|
});
|
|
|
|
describe("getErrorMessage", () => {
|
|
it("should return message from Error instance", () => {
|
|
const error = new Error("Test error message");
|
|
expect(getErrorMessage(error)).toBe("Test error message");
|
|
});
|
|
|
|
it("should return 'Unknown error' for non-Error objects", () => {
|
|
expect(getErrorMessage("string error")).toBe("Unknown error");
|
|
expect(getErrorMessage(123)).toBe("Unknown error");
|
|
expect(getErrorMessage(null)).toBe("Unknown error");
|
|
expect(getErrorMessage(undefined)).toBe("Unknown error");
|
|
expect(getErrorMessage({})).toBe("Unknown error");
|
|
expect(getErrorMessage([])).toBe("Unknown error");
|
|
});
|
|
|
|
it("should return message from Error with empty message", () => {
|
|
const error = new Error("");
|
|
expect(getErrorMessage(error)).toBe("");
|
|
});
|
|
|
|
it("should handle Error objects with custom properties", () => {
|
|
const error = new Error("Base message");
|
|
(error as any).customProp = "custom value";
|
|
expect(getErrorMessage(error)).toBe("Base message");
|
|
});
|
|
|
|
it("should handle Error objects created with different constructors", () => {
|
|
class CustomError extends Error {
|
|
constructor(message: string) {
|
|
super(message);
|
|
this.name = "CustomError";
|
|
}
|
|
}
|
|
|
|
const customError = new CustomError("Custom error message");
|
|
expect(getErrorMessage(customError)).toBe("Custom error message");
|
|
});
|
|
});
|
|
});
|