import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { WatchlistStore } from "../src/agent/watchlistStore"; let tempDir: string | undefined; afterEach(async () => { if (tempDir) { await rm(tempDir, { force: true, recursive: true }); tempDir = undefined; } }); describe("WatchlistStore", () => { it("adds normalized stocks once and persists them", async () => { tempDir = await mkdtemp(join(tmpdir(), "stock-agent-")); const path = join(tempDir, "watchlist.json"); const store = new WatchlistStore(path); await store.add("aapl", "Apple"); await store.add("AAPL", "Duplicate"); await store.add("00700.HK", "Tencent"); expect(await store.list()).toEqual([ expect.objectContaining({ display: "AAPL", market: "US", name: "Apple" }), expect.objectContaining({ display: "00700.HK", market: "HK", name: "Tencent" }) ]); const reloaded = new WatchlistStore(path); expect(await reloaded.list()).toHaveLength(2); }); it("removes stocks by any supported symbol form", async () => { tempDir = await mkdtemp(join(tmpdir(), "stock-agent-")); const store = new WatchlistStore(join(tempDir, "watchlist.json")); await store.add("600519", "Kweichow Moutai"); await store.remove("600519.SS"); expect(await store.list()).toEqual([]); }); });