97 lines
3.4 KiB
TypeScript
97 lines
3.4 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
formatDistance,
|
|
haversineKm,
|
|
parseOsrmTable,
|
|
sortByDistance,
|
|
straightLineDistance,
|
|
type DistanceResult,
|
|
} from "./geo";
|
|
|
|
const SAMYAN = { latitude: 13.7343452, longitude: 100.5282654 };
|
|
const NGAMWONGWAN = { latitude: 13.8564363, longitude: 100.5419053 };
|
|
|
|
describe("haversineKm", () => {
|
|
it("ระยะทางเส้นตรง Samyan → งามวงศ์วาน ประมาณ 13.6 กม.", () => {
|
|
const km = haversineKm(SAMYAN, NGAMWONGWAN);
|
|
expect(km).toBeGreaterThan(13);
|
|
expect(km).toBeLessThan(14.5);
|
|
});
|
|
|
|
it("จุดเดียวกันได้ 0", () => {
|
|
expect(haversineKm(SAMYAN, SAMYAN)).toBeCloseTo(0, 5);
|
|
});
|
|
});
|
|
|
|
describe("straightLineDistance", () => {
|
|
it("ติดป้ายว่าเป็นค่าประมาณ (เส้นตรง × detour factor)", () => {
|
|
const result = straightLineDistance(SAMYAN, NGAMWONGWAN);
|
|
expect(result.source).toBe("straight");
|
|
expect(result.distance_km).toBeGreaterThan(haversineKm(SAMYAN, NGAMWONGWAN));
|
|
});
|
|
});
|
|
|
|
describe("parseOsrmTable", () => {
|
|
it("ข้ามคอลัมน์แรก (ต้นทาง) และแปลงเมตรเป็นกิโลเมตร", () => {
|
|
const parsed = parseOsrmTable([[0, 16989.1, 3576.3]]);
|
|
expect(parsed).toHaveLength(2);
|
|
expect(parsed[0]).toBeCloseTo(16.9891, 4);
|
|
expect(parsed[1]).toBeCloseTo(3.5763, 4);
|
|
});
|
|
|
|
it("ค่า null จาก OSRM กลายเป็น null", () => {
|
|
expect(parseOsrmTable([[0, null, 1000]])).toEqual([null, 1]);
|
|
});
|
|
|
|
it("ข้อมูลผิดรูปแบบคืนอาร์เรย์ว่าง", () => {
|
|
expect(parseOsrmTable(undefined)).toEqual([]);
|
|
expect(parseOsrmTable("nope")).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe("formatDistance", () => {
|
|
it("แสดงทศนิยมหนึ่งตำแหน่ง", () => {
|
|
expect(formatDistance({ distance_km: 2.44, source: "road" }, "—")).toBe(
|
|
"2.4 km.",
|
|
);
|
|
});
|
|
|
|
it("ค่าประมาณขึ้นต้นด้วย ≈", () => {
|
|
expect(
|
|
formatDistance({ distance_km: 16.99, source: "straight" }, "—"),
|
|
).toBe("≈ 17.0 km.");
|
|
});
|
|
|
|
it("ไม่มีข้อมูลใช้ข้อความสำรอง", () => {
|
|
expect(formatDistance(undefined, "— km.")).toBe("— km.");
|
|
});
|
|
});
|
|
|
|
describe("sortByDistance", () => {
|
|
const items = [{ slug: "far" }, { slug: "unknown" }, { slug: "near" }];
|
|
const distances: Record<string, DistanceResult | undefined> = {
|
|
far: { distance_km: 120, source: "road" },
|
|
near: { distance_km: 0.8, source: "road" },
|
|
};
|
|
|
|
it("เรียงใกล้สุดก่อน และโรงที่ไม่ทราบระยะทางไปท้ายสุด", () => {
|
|
expect(sortByDistance(items, distances).map((item) => item.slug)).toEqual([
|
|
"near",
|
|
"far",
|
|
"unknown",
|
|
]);
|
|
});
|
|
|
|
it("ไกล 100 กม. ก็ยังถูกเรียงตามระยะทาง ไม่ถูกตัดออก", () => {
|
|
const sorted = sortByDistance(items, distances);
|
|
expect(sorted).toHaveLength(items.length);
|
|
});
|
|
|
|
it("ไม่แก้ไขอาร์เรย์ต้นฉบับ", () => {
|
|
const original = [...items];
|
|
sortByDistance(items, distances);
|
|
expect(items).toEqual(original);
|
|
});
|
|
});
|