76 lines
2.0 KiB
TypeScript
76 lines
2.0 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { describe, it } from "node:test";
|
|
|
|
import {
|
|
extractUnitCount,
|
|
parseMoney,
|
|
parseRating,
|
|
parseReviewCount,
|
|
parseStarBreakdown,
|
|
parseUnitPrice
|
|
} from "../src/parsers.js";
|
|
|
|
describe("parsers", () => {
|
|
it("parses USD money", () => {
|
|
assert.deepEqual(parseMoney("$19.99"), { amount: 19.99, currency: "USD", display: "$19.99" });
|
|
});
|
|
|
|
it("parses rating text", () => {
|
|
assert.equal(parseRating("4.6 out of 5 stars"), 4.6);
|
|
});
|
|
|
|
it("parses review count text", () => {
|
|
assert.equal(parseReviewCount("1,234 ratings"), 1234);
|
|
});
|
|
|
|
it("parses visible star histogram percentages", () => {
|
|
assert.deepEqual(parseStarBreakdown("5 star 72% 4 star 15% 3 star 7% 2 star 3% 1 star 3%"), {
|
|
five: 72,
|
|
four: 15,
|
|
three: 7,
|
|
two: 3,
|
|
one: 3,
|
|
basis: "percent"
|
|
});
|
|
});
|
|
|
|
it("extracts high-confidence unit counts", () => {
|
|
assert.deepEqual(extractUnitCount("LED bulbs, 100 Count, daylight"), {
|
|
count: 100,
|
|
confidence: "high",
|
|
source: "100 Count"
|
|
});
|
|
assert.deepEqual(extractUnitCount("Pack of 6 USB-C cables"), {
|
|
count: 6,
|
|
confidence: "high",
|
|
source: "Pack of 6"
|
|
});
|
|
});
|
|
|
|
it("distinguishes lower-confidence unit count phrases", () => {
|
|
assert.deepEqual(extractUnitCount("Set of 8 replacement filters"), {
|
|
count: 8,
|
|
confidence: "medium",
|
|
source: "Set of 8"
|
|
});
|
|
assert.deepEqual(extractUnitCount("6 bulbs soft white"), {
|
|
count: 6,
|
|
confidence: "low",
|
|
source: "6 bulbs"
|
|
});
|
|
});
|
|
|
|
it("parses visible unit prices", () => {
|
|
assert.deepEqual(parseUnitPrice("$0.33/Count"), {
|
|
amount: 0.33,
|
|
currency: "USD",
|
|
display: "$0.33/Count"
|
|
});
|
|
});
|
|
|
|
it("parses whole-dollar and one-decimal prices", () => {
|
|
assert.deepEqual(parseMoney("$20"), { amount: 20, currency: "USD", display: "$20" });
|
|
assert.deepEqual(parseMoney("$19.9"), { amount: 19.9, currency: "USD", display: "$19.9" });
|
|
});
|
|
});
|