import { assertEquals } from "jsr:@std/assert";
import { DatabaseSync } from "node:sqlite";
import { db_setup, select_closest } from "./db.ts";
import { assert } from "@std/assert/assert";

function select_closest_base_test() {
  //db setup
  const db = new DatabaseSync(":memory:");
  db_setup(db);
  db.exec(`
        INSERT INTO stops (href,lat,lon) VALUES ('a', 0, 0);
    `);
  db.exec(`
        INSERT INTO stops (href,lat,lon) VALUES ('b', 3, 4);
    `);
  db.exec(`
        INSERT INTO stops (href,lat,lon) VALUES ('c', 5, 12);
    `);
  db.exec(`
        INSERT INTO stops (href,lat,lon) VALUES ('d', 1, 1);
    `);

  const closest = select_closest(db, { lat: 0, lon: 0 }, 2);

  // actual testing
  assertEquals(
    typeof closest,
    "object",
    "select_closest return type is not an object",
  );
  assert(Array.isArray(closest), "select_closest return type is not an array");
  assertEquals(
    closest.length,
    2,
    "limit of 2 doesn't cause select_closest to return an array of 2 elements",
  );
  assertEquals(
    closest,
    ["a", "d"],
    "distance measure was changed away from euclidean/Manhattan distance of function is spewing nonsense",
  );
}

Deno.test(select_closest_base_test);
