import assert from "node:assert/strict";
import test from "node:test";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";

import { loadZoneSourceProfile, ZONE_SOURCE_GENERATION_SCHEMA_VERSION } from "../src/zone-source-snapshot.mjs";

const root = resolve(dirname(fileURLToPath(import.meta.url)), "../zone-sources");

test("loads the immutable Department of Transport MMap generation", async () => {
  const loaded = await loadZoneSourceProfile(root, "moscow-pmd-slow-zones");
  const generation = loaded.generation;

  assert.equal(generation.schemaVersion, ZONE_SOURCE_GENERATION_SCHEMA_VERSION);
  assert.equal(generation.authorityId, "moscow-department-of-transport");
  assert.equal(generation.datasetId, "pmd-slow-zones");
  assert.equal(generation.adapterId, "depttrans-mmap-snapshot-v1");
  assert.equal(generation.complete, true);
  assert.equal(generation.metadata.sourceZoneCount, 905);
  assert.equal(generation.metadata.publishedZoneCount, 903);
  assert.equal(generation.metadata.excludedZoneCount, 2);
  assert.equal(generation.metadata.scheduleRowCount, 6335);
  assert.equal(generation.metadata.positionCount, 62955);
  assert.equal(generation.metadata.geometrySha256, "914797122297d06acaff8a32605f172abb6c769b879d1b75f567db450d29913f");
  assert.equal(generation.metadata.scheduleSha256, "8f3e50e2aec3889657814ea17982b2e75fcc2b83d0511c901016cd08e5844cd5");
  assert.match(generation.contentDigest, /^[a-f0-9]{64}$/);
  assert.ok(loaded.body.byteLength < 16 * 1024 * 1024);
  assert.equal(generation.zones.some((zone) => zone.nativeId === "1521" || zone.nativeId === "1522"), false);

  const zone = generation.zones.find((entry) => entry.nativeId === "1478");
  assert.equal(zone.displayName, "Петровский парк_6 (15 км/ч)");
  assert.equal(zone.geometry.type, "Polygon");
  assert.equal(zone.maxSpeedKph, 15);
  assert.equal(zone.weeklySpeedSchedule.length, 7);
});
