115 lines
3.8 KiB
TypeScript
115 lines
3.8 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { tmpdir } from "node:os";
|
|
import {
|
|
computeWindowCutoff,
|
|
getRecentCorpUsageScores,
|
|
loadUsageStats,
|
|
pruneExpiredStats,
|
|
recordCorpUsage,
|
|
resolveUsageFilePath,
|
|
sortCorpsByUsageAndRecency,
|
|
toDateKey,
|
|
USAGE_FILE_ENV
|
|
} from "./usage-tracker.js";
|
|
|
|
describe("usage-tracker", () => {
|
|
const originalUsageFileEnv = process.env[USAGE_FILE_ENV];
|
|
let testDir: string;
|
|
let testUsageFile: string;
|
|
|
|
beforeEach(() => {
|
|
testDir = join(tmpdir(), `seal-use-usage-test-${Date.now()}-${Math.random().toString(16).slice(2, 8)}`);
|
|
mkdirSync(testDir, { recursive: true });
|
|
testUsageFile = join(testDir, "usage.json");
|
|
process.env[USAGE_FILE_ENV] = testUsageFile;
|
|
});
|
|
|
|
afterEach(() => {
|
|
if (originalUsageFileEnv !== undefined) {
|
|
process.env[USAGE_FILE_ENV] = originalUsageFileEnv;
|
|
} else {
|
|
delete process.env[USAGE_FILE_ENV];
|
|
}
|
|
rmSync(testDir, { recursive: true, force: true });
|
|
});
|
|
|
|
test("resolves custom usage file path from env", () => {
|
|
expect(resolveUsageFilePath()).toBe(testUsageFile);
|
|
});
|
|
|
|
test("computes correct date keys and window cutoffs", () => {
|
|
const fixedDate = new Date("2026-09-13T12:00:00.000Z");
|
|
expect(toDateKey(fixedDate)).toBe("2026-09-13");
|
|
expect(computeWindowCutoff(7, fixedDate)).toBe("2026-09-07");
|
|
expect(computeWindowCutoff(1, fixedDate)).toBe("2026-09-13");
|
|
});
|
|
|
|
test("records usage and aggregates 7-day scores", () => {
|
|
const day1 = new Date("2026-09-10T10:00:00.000Z");
|
|
const day2 = new Date("2026-09-13T15:00:00.000Z");
|
|
|
|
recordCorpUsage("corp-a", day1);
|
|
recordCorpUsage("corp-a", day1);
|
|
recordCorpUsage("corp-a", day2);
|
|
recordCorpUsage("corp-b", day2);
|
|
|
|
const scores = getRecentCorpUsageScores(["corp-a", "corp-b", "corp-c"], 7, day2);
|
|
expect(scores.get("corp-a")).toBe(3);
|
|
expect(scores.get("corp-b")).toBe(1);
|
|
expect(scores.get("corp-c")).toBe(0);
|
|
});
|
|
|
|
test("prunes entries older than 7 days when recording new usage", () => {
|
|
const oldDay = new Date("2026-09-01T10:00:00.000Z");
|
|
const currentDay = new Date("2026-09-13T10:00:00.000Z");
|
|
|
|
recordCorpUsage("corp-old", oldDay);
|
|
const initialStats = loadUsageStats();
|
|
expect(initialStats["corp-old"]?.["2026-09-01"]).toBe(1);
|
|
|
|
// Recording on current day should prune the 2026-09-01 entry
|
|
recordCorpUsage("corp-new", currentDay);
|
|
const updatedStats = loadUsageStats();
|
|
expect(updatedStats["corp-new"]?.["2026-09-13"]).toBe(1);
|
|
expect(updatedStats["corp-old"]).toBeUndefined();
|
|
});
|
|
|
|
test("handles corrupted usage file gracefully without throwing", () => {
|
|
writeFileSync(testUsageFile, "not-valid-json{{{", "utf-8");
|
|
expect(() => recordCorpUsage("corp-safe")).not.toThrow();
|
|
|
|
const stats = loadUsageStats();
|
|
expect(stats["corp-safe"]).toBeDefined();
|
|
});
|
|
|
|
test("sorts corporations by current selection, usage score, then name", () => {
|
|
const corps = [
|
|
{ id: "corp-low", name: "低频企业" },
|
|
{ id: "corp-current", name: "当前企业" },
|
|
{ id: "corp-high", name: "高频企业" },
|
|
{ id: "corp-zero-b", name: "零频企业B" },
|
|
{ id: "corp-zero-a", name: "零频企业A" }
|
|
];
|
|
|
|
const scores = new Map<string, number>([
|
|
["corp-low", 2],
|
|
["corp-current", 1], // Even with lower score, currentCorpId should be on top
|
|
["corp-high", 20],
|
|
["corp-zero-b", 0],
|
|
["corp-zero-a", 0]
|
|
]);
|
|
|
|
const sorted = sortCorpsByUsageAndRecency(corps, "corp-current", scores);
|
|
|
|
expect(sorted.map((c) => c.id)).toEqual([
|
|
"corp-current",
|
|
"corp-high",
|
|
"corp-low",
|
|
"corp-zero-a",
|
|
"corp-zero-b"
|
|
]);
|
|
});
|
|
});
|