92 lines
2.2 KiB
TypeScript
92 lines
2.2 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import test from "node:test"
|
|
import {
|
|
hasTrustedPartsListings,
|
|
shouldShowEbayPartsPanel,
|
|
} from "@/lib/ebay-parts-visibility"
|
|
import type { EbayCacheState } from "@/lib/ebay-parts-match"
|
|
|
|
function createCacheState(
|
|
overrides: Partial<EbayCacheState> = {}
|
|
): EbayCacheState {
|
|
return {
|
|
key: "manual-parts",
|
|
status: "idle",
|
|
lastSuccessfulAt: null,
|
|
lastAttemptAt: null,
|
|
nextEligibleAt: null,
|
|
lastError: null,
|
|
consecutiveFailures: 0,
|
|
queryCount: 0,
|
|
itemCount: 0,
|
|
sourceQueries: [],
|
|
freshnessMs: null,
|
|
isStale: true,
|
|
listingCount: 0,
|
|
activeListingCount: 0,
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
test("hasTrustedPartsListings returns true when at least one part has listings", () => {
|
|
assert.equal(
|
|
hasTrustedPartsListings([
|
|
{ ebayListings: [] },
|
|
{ ebayListings: [{ itemId: "123" }] },
|
|
]),
|
|
true
|
|
)
|
|
})
|
|
|
|
test("shouldShowEbayPartsPanel hides panel for rate-limited cache with no listings", () => {
|
|
const result = shouldShowEbayPartsPanel({
|
|
isLoading: false,
|
|
parts: [{ ebayListings: [] }],
|
|
cache: createCacheState({
|
|
status: "rate_limited",
|
|
isStale: true,
|
|
listingCount: 0,
|
|
activeListingCount: 0,
|
|
freshnessMs: null,
|
|
lastAttemptAt: null,
|
|
lastSuccessfulAt: null,
|
|
nextEligibleAt: null,
|
|
lastError: "rate limited",
|
|
}),
|
|
error: null,
|
|
})
|
|
|
|
assert.equal(result, false)
|
|
})
|
|
|
|
test("shouldShowEbayPartsPanel shows panel while loading", () => {
|
|
const result = shouldShowEbayPartsPanel({
|
|
isLoading: true,
|
|
parts: [],
|
|
cache: null,
|
|
error: null,
|
|
})
|
|
|
|
assert.equal(result, true)
|
|
})
|
|
|
|
test("shouldShowEbayPartsPanel shows panel when trusted listings exist", () => {
|
|
const result = shouldShowEbayPartsPanel({
|
|
isLoading: false,
|
|
parts: [{ ebayListings: [{ itemId: "abc" }] }],
|
|
cache: createCacheState({
|
|
status: "success",
|
|
isStale: false,
|
|
listingCount: 12,
|
|
activeListingCount: 12,
|
|
freshnessMs: 5000,
|
|
lastAttemptAt: Date.now(),
|
|
lastSuccessfulAt: Date.now(),
|
|
nextEligibleAt: Date.now() + 1000,
|
|
lastError: null,
|
|
}),
|
|
error: null,
|
|
})
|
|
|
|
assert.equal(result, true)
|
|
})
|