Download extension/test/split.test.ts from Gaurav711/HyperFlow: direct link, hf CLI and curl.
- Browser
- Download file 3.68 kB
-
https://huggingface.co/spaces/Gaurav711/HyperFlow/resolve/main/extension/test/split.test.ts
- Command line
-
hf download hf://spaces/Gaurav711/HyperFlow/extension/test/split.test.ts
-
curl -L -o split.test.ts https://huggingface.co/spaces/Gaurav711/HyperFlow/resolve/main/extension/test/split.test.ts
3.68 kB
| // @vitest-environment node | |
| import { existsSync, readFileSync } from 'node:fs'; | |
| import { fileURLToPath } from 'node:url'; | |
| import highsLoader, { type Highs } from 'highs'; | |
| import { solveSplit, type SplitItem, type StoreFees } from '../src/split'; | |
| const VECTORS = fileURLToPath(new URL('./vectors/split-vectors.json', import.meta.url)); | |
| interface PyOffer { price_minor: number; available: boolean; purchase_limit: number } | |
| interface PyStore { | |
| delivery_fee_minor: number; free_delivery_threshold_minor: number | null; handling_fee_minor: number; | |
| small_cart_fee_minor: number; small_cart_threshold_minor: number | null; | |
| } | |
| interface Vector { | |
| family: string; seed: number; status: string; total_minor: number | null; unpurchasable: string[]; | |
| items: { id: string; quantity: number; offers: Record<string, PyOffer> }[]; stores: Record<string, PyStore>; | |
| } | |
| const toItems = (v: Vector): SplitItem[] => v.items.map((it) => ({ | |
| id: it.id, quantity: it.quantity, | |
| offers: Object.fromEntries(Object.entries(it.offers).map(([s, o]) => [s, { priceMinor: o.price_minor, available: o.available, purchaseLimit: o.purchase_limit }])), | |
| })); | |
| const toStores = (v: Vector): Record<string, StoreFees> => Object.fromEntries(Object.entries(v.stores).map(([s, f]) => [s, { | |
| deliveryFeeMinor: f.delivery_fee_minor, freeDeliveryThresholdMinor: f.free_delivery_threshold_minor, handlingFeeMinor: f.handling_fee_minor, | |
| smallCartFeeMinor: f.small_cart_fee_minor, smallCartThresholdMinor: f.small_cart_threshold_minor, | |
| }])); | |
| let highs: Highs; | |
| beforeAll(async () => { highs = await highsLoader(); }); | |
| const fees = (over: Partial<StoreFees> = {}): StoreFees => ({ deliveryFeeMinor: 0, freeDeliveryThresholdMinor: null, | |
| handlingFeeMinor: 0, smallCartFeeMinor: 0, smallCartThresholdMinor: null, ...over }); | |
| it('agrees with the Python exhaustive oracle on every exported vector', () => { | |
| if (!existsSync(VECTORS)) { | |
| throw new Error('Generate vectors first: python scripts/export_split_vectors.py (from the repository root)'); | |
| } | |
| const vectors: Vector[] = JSON.parse(readFileSync(VECTORS, 'utf8')); | |
| expect(vectors.length).toBe(1000); | |
| const started = performance.now(); | |
| for (const v of vectors) { | |
| const r = solveSplit(highs, toItems(v), toStores(v)); | |
| expect([v.family, v.seed, r.status, r.totalMinor, r.unpurchasable]).toEqual([v.family, v.seed, v.status, v.total_minor, v.unpurchasable]); | |
| } | |
| console.info(`1000 vectors solved in ${Math.round(performance.now() - started)} ms`); | |
| }, 120_000); | |
| it('splits when the saving beats the extra delivery fee', () => { | |
| const r = solveSplit(highs, [ | |
| { id: 'atta', quantity: 1, offers: { zepto: { priceMinor: 20000, available: true, purchaseLimit: 5 }, blinkit: { priceMinor: 10000, available: true, purchaseLimit: 5 } } }, | |
| { id: 'ghee', quantity: 1, offers: { zepto: { priceMinor: 10000, available: true, purchaseLimit: 5 }, blinkit: { priceMinor: 15000, available: true, purchaseLimit: 5 } } }, | |
| ], { zepto: fees({ deliveryFeeMinor: 3000 }), blinkit: fees({ deliveryFeeMinor: 3000 }) }); | |
| expect(r).toMatchObject({ assignment: { atta: 'blinkit', ghee: 'zepto' }, totalMinor: 26000, | |
| bestSingleStore: { store: 'blinkit', totalMinor: 28000 }, savingsMinor: 2000 }); | |
| }); | |
| it.each([ | |
| [{ id: 'a', quantity: 1, offers: { s1: { priceMinor: 10.5, available: true, purchaseLimit: 1 } } }], | |
| [{ id: 'a', quantity: 0, offers: { s1: { priceMinor: 100, available: true, purchaseLimit: 1 } } }], | |
| [{ id: 'a', quantity: 1, offers: { s9: { priceMinor: 100, available: true, purchaseLimit: 1 } } }], | |
| ])('rejects malformed input %#', (bad) => { | |
| expect(() => solveSplit(highs, [bad as SplitItem], { s1: fees() })).toThrow(); | |
| }); | |