star-chart-search-enhancer/tests/result-store.test.ts

102 lines
2.5 KiB
TypeScript

import { describe, expect, test } from "vitest";
import { createMarketResultStore } from "../src/content/market/result-store";
describe("result-store", () => {
test("creates loading records from current-page rows", () => {
const store = createMarketResultStore();
store.upsertMarketRow({
authorId: "123",
authorName: "Alice",
price21To60s: "450000"
});
store.setAuthorLoading("123");
expect(store.getRecord("123")).toMatchObject({
authorId: "123",
authorName: "Alice",
price21To60s: "450000",
status: "loading"
});
});
test("updates one author to success", () => {
const store = createMarketResultStore();
store.upsertMarketRow({
authorId: "123",
authorName: "Alice"
});
store.setAuthorSuccess("123", {
singleVideoAfterSearchRate: "<0.02%",
personalVideoAfterSearchRate: "0.02% - 0.1%"
});
expect(store.getRecord("123")).toMatchObject({
status: "success",
rates: {
singleVideoAfterSearchRate: "<0.02%",
personalVideoAfterSearchRate: "0.02% - 0.1%"
}
});
});
test("preserves failed authors instead of dropping them", () => {
const store = createMarketResultStore();
store.upsertMarketRow({
authorId: "123",
authorName: "Alice"
});
store.setAuthorFailed("123", "request-failed");
expect(store.listRecords()).toHaveLength(1);
expect(store.getRecord("123")).toMatchObject({
status: "failed",
failureReason: "request-failed"
});
});
test("dedupes the same author across repeated page writes", () => {
const store = createMarketResultStore();
store.upsertMarketRow({
authorId: "123",
authorName: "Alice",
price21To60s: "450000"
});
store.upsertMarketRow({
authorId: "123",
authorName: "Alice v2",
price21To60s: "470000"
});
expect(store.listRecords()).toHaveLength(1);
});
test("keeps the original major fields stable after repeated writes", () => {
const store = createMarketResultStore();
store.upsertMarketRow({
authorId: "123",
authorName: "Alice",
location: "Hangzhou",
price21To60s: "450000"
});
store.upsertMarketRow({
authorId: "123",
authorName: "Alice v2",
location: "Shanghai",
price21To60s: "470000"
});
expect(store.getRecord("123")).toMatchObject({
authorId: "123",
authorName: "Alice",
location: "Hangzhou",
price21To60s: "450000"
});
});
});