Files
claude-code/src/utils/__tests__/CircularBuffer.test.ts
claude-code-best acfaac5f14 test: Phase 1 — 添加 8 个纯函数测试文件 (+134 tests)
- errors.test.ts: 28 tests (isAbortError, toError, errorMessage, getErrnoCode, isFsInaccessible, classifyAxiosError 等)
- shellRuleMatching.test.ts: 22 tests (permissionRuleExtractPrefix, hasWildcards, matchWildcardPattern, parsePermissionRule 等)
- argumentSubstitution.test.ts: 18 tests (parseArguments, parseArgumentNames, generateProgressiveArgumentHint, substituteArguments)
- CircularBuffer.test.ts: 12 tests (add, addAll, getRecent, toArray, clear, length)
- sanitization.test.ts: 14 tests (partiallySanitizeUnicode, recursivelySanitizeUnicode)
- slashCommandParsing.test.ts: 8 tests (parseSlashCommand)
- contentArray.test.ts: 6 tests (insertBlockAfterToolResults)
- objectGroupBy.test.ts: 5 tests (objectGroupBy)

总计:781 tests / 40 files

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-02 08:50:29 +08:00

87 lines
2.3 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { CircularBuffer } from "../CircularBuffer";
describe("CircularBuffer", () => {
test("starts empty", () => {
const buf = new CircularBuffer<number>(5);
expect(buf.length()).toBe(0);
expect(buf.toArray()).toEqual([]);
});
test("adds items up to capacity", () => {
const buf = new CircularBuffer<number>(3);
buf.add(1);
buf.add(2);
buf.add(3);
expect(buf.length()).toBe(3);
expect(buf.toArray()).toEqual([1, 2, 3]);
});
test("evicts oldest when full", () => {
const buf = new CircularBuffer<number>(3);
buf.add(1);
buf.add(2);
buf.add(3);
buf.add(4);
expect(buf.length()).toBe(3);
expect(buf.toArray()).toEqual([2, 3, 4]);
});
test("evicts multiple oldest items", () => {
const buf = new CircularBuffer<number>(2);
buf.add(1);
buf.add(2);
buf.add(3);
buf.add(4);
buf.add(5);
expect(buf.toArray()).toEqual([4, 5]);
});
test("addAll adds multiple items", () => {
const buf = new CircularBuffer<number>(5);
buf.addAll([1, 2, 3]);
expect(buf.toArray()).toEqual([1, 2, 3]);
});
test("addAll with overflow", () => {
const buf = new CircularBuffer<number>(3);
buf.addAll([1, 2, 3, 4, 5]);
expect(buf.toArray()).toEqual([3, 4, 5]);
});
test("getRecent returns last N items", () => {
const buf = new CircularBuffer<number>(5);
buf.addAll([1, 2, 3, 4, 5]);
expect(buf.getRecent(3)).toEqual([3, 4, 5]);
});
test("getRecent returns fewer when not enough items", () => {
const buf = new CircularBuffer<number>(5);
buf.add(1);
buf.add(2);
expect(buf.getRecent(5)).toEqual([1, 2]);
});
test("getRecent works after wraparound", () => {
const buf = new CircularBuffer<number>(3);
buf.addAll([1, 2, 3, 4, 5]);
expect(buf.getRecent(2)).toEqual([4, 5]);
});
test("clear resets buffer", () => {
const buf = new CircularBuffer<number>(5);
buf.addAll([1, 2, 3]);
buf.clear();
expect(buf.length()).toBe(0);
expect(buf.toArray()).toEqual([]);
});
test("works with string type", () => {
const buf = new CircularBuffer<string>(2);
buf.add("a");
buf.add("b");
buf.add("c");
expect(buf.toArray()).toEqual(["b", "c"]);
});
});