WFDASH-WEBAPP/src/lib/server/completed-window.test.ts
Dave 139a3f3bfe feat(quotes): enhance scoring logic to include price matching
- Updated the `scoreQuote` function to incorporate price scoring, allowing quotes to be evaluated based on selling and cost prices when the query is numeric.
- Introduced new weights for item selling and cost prices, ensuring they contribute to the overall score only when relevant.
- Enhanced the test suite to cover new price matching scenarios, ensuring accurate scoring behavior for quotes based on price inputs.
- Refactored related functions to support the new price scoring logic, maintaining consistency across the scoring process.
2026-08-24 07:09:39 +02:00

62 lines
2.3 KiB
TypeScript

import { describe, it, expect } from "vitest";
import {
COMPLETED_WINDOW_DAYS,
completedWindowCutoff,
completedTabFilter,
} from "./orders-service";
describe("completedWindowCutoff", () => {
it("returns an ISO string COMPLETED_WINDOW_DAYS before `now`", () => {
const now = new Date("2026-08-23T12:00:00.000Z");
const cutoff = completedWindowCutoff(now);
const expected = new Date(
now.getTime() - COMPLETED_WINDOW_DAYS * 24 * 60 * 60 * 1000,
);
expect(cutoff).toBe(expected.toISOString());
});
it("uses the supplied `now` argument (pure function, no clock dep)", () => {
const fixed = new Date("2026-01-15T00:00:00.000Z");
const cutoff = completedWindowCutoff(fixed);
// 30 days before 2026-01-15 = 2025-12-16
expect(cutoff).toBe("2025-12-16T00:00:00.000Z");
});
it("defaults to wall-clock `new Date()` when called with no args", () => {
const before = Date.now();
const cutoff = completedWindowCutoff();
const cutoffMs = new Date(cutoff).getTime();
const expectedLatest =
before - COMPLETED_WINDOW_DAYS * 24 * 60 * 60 * 1000 + 50;
expect(cutoffMs).toBeLessThanOrEqual(expectedLatest);
});
});
describe("completedTabFilter", () => {
it("applies the time-windowed predicate for both terminal statuses", () => {
const now = new Date("2026-08-23T00:00:00.000Z");
const cutoff = completedWindowCutoff(now);
const filter = completedTabFilter(now);
// delivered items are filtered by delivered_at, not updated, so
// older rows (delivered before the window) drop out.
expect(filter).toContain(
`(item_status = "delivered" && delivered_at >= "${cutoff}")`,
);
// completed items have no dedicated timestamp, so we use updated.
expect(filter).toContain(
`(item_status = "completed" && updated >= "${cutoff}")`,
);
// The two clauses must be OR-joined (PB doesn't have an IN-list
// for date predicates).
expect(filter).toContain(") || (");
});
it("uses the supplied `now` so the filter is stable for tests", () => {
const now = new Date("2026-07-01T00:00:00.000Z");
expect(completedTabFilter(now)).toBe(
`(item_status = "delivered" && delivered_at >= "${completedWindowCutoff(now)}")` +
` || (item_status = "completed" && updated >= "${completedWindowCutoff(now)}")`,
);
});
});