mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-22 16:25:51 +00:00
test: 添加一大堆测试文件
This commit is contained in:
54
src/utils/__tests__/lazySchema.test.ts
Normal file
54
src/utils/__tests__/lazySchema.test.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { lazySchema } from "../lazySchema";
|
||||
|
||||
describe("lazySchema", () => {
|
||||
test("returns a function", () => {
|
||||
const factory = lazySchema(() => 42);
|
||||
expect(typeof factory).toBe("function");
|
||||
});
|
||||
|
||||
test("calls factory on first invocation", () => {
|
||||
let callCount = 0;
|
||||
const factory = lazySchema(() => {
|
||||
callCount++;
|
||||
return "result";
|
||||
});
|
||||
factory();
|
||||
expect(callCount).toBe(1);
|
||||
});
|
||||
|
||||
test("returns cached result on subsequent invocations", () => {
|
||||
const factory = lazySchema(() => ({ value: Math.random() }));
|
||||
const first = factory();
|
||||
const second = factory();
|
||||
expect(first).toBe(second);
|
||||
});
|
||||
|
||||
test("factory is called only once", () => {
|
||||
let callCount = 0;
|
||||
const factory = lazySchema(() => {
|
||||
callCount++;
|
||||
return "cached";
|
||||
});
|
||||
factory();
|
||||
factory();
|
||||
factory();
|
||||
expect(callCount).toBe(1);
|
||||
});
|
||||
|
||||
test("works with different return types", () => {
|
||||
const numFactory = lazySchema(() => 123);
|
||||
expect(numFactory()).toBe(123);
|
||||
|
||||
const arrFactory = lazySchema(() => [1, 2, 3]);
|
||||
expect(arrFactory()).toEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
test("each call to lazySchema returns independent cache", () => {
|
||||
const a = lazySchema(() => ({ id: "a" }));
|
||||
const b = lazySchema(() => ({ id: "b" }));
|
||||
expect(a()).not.toBe(b());
|
||||
expect(a().id).toBe("a");
|
||||
expect(b().id).toBe("b");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user