- 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>
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { objectGroupBy } from "../objectGroupBy";
|
|
|
|
describe("objectGroupBy", () => {
|
|
test("groups items by key", () => {
|
|
const result = objectGroupBy([1, 2, 3, 4], (n) =>
|
|
n % 2 === 0 ? "even" : "odd"
|
|
);
|
|
expect(result.even).toEqual([2, 4]);
|
|
expect(result.odd).toEqual([1, 3]);
|
|
});
|
|
|
|
test("returns empty object for empty input", () => {
|
|
const result = objectGroupBy([], () => "key");
|
|
expect(Object.keys(result)).toHaveLength(0);
|
|
});
|
|
|
|
test("handles single group", () => {
|
|
const result = objectGroupBy(["a", "b", "c"], () => "all");
|
|
expect(result.all).toEqual(["a", "b", "c"]);
|
|
});
|
|
|
|
test("passes index to keySelector", () => {
|
|
const result = objectGroupBy(["a", "b", "c", "d"], (_, i) =>
|
|
i < 2 ? "first" : "second"
|
|
);
|
|
expect(result.first).toEqual(["a", "b"]);
|
|
expect(result.second).toEqual(["c", "d"]);
|
|
});
|
|
|
|
test("works with objects", () => {
|
|
const items = [
|
|
{ name: "Alice", role: "admin" },
|
|
{ name: "Bob", role: "user" },
|
|
{ name: "Charlie", role: "admin" },
|
|
];
|
|
const result = objectGroupBy(items, (item) => item.role);
|
|
expect(result.admin).toHaveLength(2);
|
|
expect(result.user).toHaveLength(1);
|
|
});
|
|
});
|