52 lines
2.0 KiB
TypeScript
52 lines
2.0 KiB
TypeScript
import { test, expect, describe } from "bun:test";
|
|
import type { BoardItem } from "../src/types";
|
|
import { searchTasks, snippetAround, splitHighlights } from "../src/lib/taskSearch";
|
|
|
|
function bug(over: Record<string, unknown>): BoardItem {
|
|
return {
|
|
kind: "bug", id: "b1", title: "登录失败闪退", platform: "android",
|
|
status: "awaiting_fix", attempts: 0, createdAt: 1,
|
|
watchers: ["jiaopengwen@bytedance.com"],
|
|
watcherLabels: { "jiaopengwen@bytedance.com": "焦鹏文" },
|
|
clusters: ["tt-client"],
|
|
branchName: "fix/login-crash",
|
|
baseRef: "develop",
|
|
description: "",
|
|
failureReason: null,
|
|
...over,
|
|
} as BoardItem;
|
|
}
|
|
|
|
describe("searchTasks", () => {
|
|
const items = [
|
|
bug({}),
|
|
bug({ id: "b2", title: "iOS 推送丢失", watchers: ["alice@bytedance.com"], watcherLabels: { "alice@bytedance.com": "Alice" }, clusters: ["ios-push"], branchName: "fix/push", description: "APNs token 过期" }),
|
|
];
|
|
|
|
test("matches title, watcher, cluster, branch", () => {
|
|
expect(searchTasks(items, "闪退").map((h) => h.item.id)).toEqual(["b1"]);
|
|
expect(searchTasks(items, "焦鹏文").map((h) => h.item.id)).toEqual(["b1"]);
|
|
expect(searchTasks(items, "tt-client").map((h) => h.item.id)).toEqual(["b1"]);
|
|
expect(searchTasks(items, "fix/login").map((h) => h.item.id)).toEqual(["b1"]);
|
|
});
|
|
|
|
test("matches details when the list row still has them", () => {
|
|
expect(searchTasks(items, "APNs").map((h) => h.item.id)).toEqual(["b2"]);
|
|
expect(searchTasks(items, "APNs")[0].field).toBe("detail");
|
|
});
|
|
|
|
test("empty query returns nothing", () => {
|
|
expect(searchTasks(items, " ")).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("snippetAround / splitHighlights", () => {
|
|
test("clips around the first hit", () => {
|
|
expect(snippetAround("abcdefghijklmnop", "hij", 2)).toBe("…fghijkl…");
|
|
});
|
|
test("marks every occurrence", () => {
|
|
const parts = splitHighlights("aa x aa", "aa");
|
|
expect(parts.filter((p) => p.hit).map((p) => p.text)).toEqual(["aa", "aa"]);
|
|
});
|
|
});
|