import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { test } from "node:test"; import { MAX_LIFETIME_DAYS, draftFromListing } from "../src/draft.ts"; import { parseEuros, parseRoomsX2 } from "../src/parse.ts"; import { cellMetres, decodeGeohash, encodeGeohash, parseCoordinates } from "../src/geohash.ts"; import type { Listing } from "../src/verify.ts"; test("geohash encoding matches known vectors", () => { assert.equal(encodeGeohash(57.64911, 10.40744, 11), "u4pruydqqvj"); // the classic example assert.equal(encodeGeohash(42.605, -5.603, 5), "ezs42"); // from the geohash specification assert.equal(encodeGeohash(0, 0, 1), "s"); assert.equal(encodeGeohash(-90, -180, 6), "000000"); assert.equal(encodeGeohash(53.5511, 9.9937).length, 6); }); test("encoding agrees with the geohash the Go code wrote", () => { const fx = JSON.parse(readFileSync(new URL("../../internal/listing/testdata/fixture.json", import.meta.url), "utf8")) as { listing: Listing }; const g = fx.listing.geohash; const area = decodeGeohash(g)!; assert.equal(encodeGeohash(area.lat, area.lon, g.length), g); }); test("encode and decode round trip within the cell", () => { for (const [lat, lon] of [[53.5511, 9.9937], [48.1371, 11.5754], [-33.8688, 151.2093], [89.9, 179.9], [-89.9, -179.9], [0.0001, -0.0001]] as const) { const g = encodeGeohash(lat, lon); const a = decodeGeohash(g)!; assert.ok(Math.abs(a.lat - lat) <= a.latErr, `lat of ${g}`); assert.ok(Math.abs(a.lon - lon) <= a.lonErr, `lon of ${g}`); assert.equal(encodeGeohash(a.lat, a.lon), g, "the centre of a cell encodes to the same cell"); } }); test("a published cell is about a kilometre wide", () => { const c = cellMetres(encodeGeohash(53.5511, 9.9937))!; assert.ok(c.height > 500 && c.height < 700, `height ${c.height}`); assert.ok(c.width > 600 && c.width < 1300, `width ${c.width}`); assert.equal(cellMetres("!!"), null); }); test("coordinates are read from what people copy from maps", () => { const hh = { lat: 53.5511, lon: 9.9937 }; for (const text of [ "53.5511, 9.9937", "53.5511,9.9937", " 53.5511 , 9.9937 ", "53.5511; 9.9937", "53,5511; 9,9937", // German decimal comma, semicolon between "53.5511 9.9937", "53,5511 9,9937", "https://www.openstreetmap.org/#map=17/53.5511/9.9937", "https://www.openstreetmap.org/?mlat=53.5511&mlon=9.9937#map=14/53.5511/9.9937", "https://www.google.com/maps/place/Hamburg/@53.5511,9.9937,15z/data=!3m1", ]) { assert.deepEqual(parseCoordinates(text), hh, text); } assert.deepEqual(parseCoordinates("-33.8688, 151.2093"), { lat: -33.8688, lon: 151.2093 }); assert.deepEqual(parseCoordinates("53, 9"), { lat: 53, lon: 9 }); }); test("things that are not a position are refused", () => { for (const text of ["", "Hamburg", "53.5511", "91, 10", "10, 181", "53.5511, 9.9937, 12", "a, b", "1e3, 5", "NaN, 1", "53.5511 / 9.9937 / 4", "https://example.org/x"]) { assert.equal(parseCoordinates(text), null, JSON.stringify(text)); } }); test("euro amounts are read in German and English notation", () => { const ok: Record = { "1150": 115000, "1.150": 115000, // thousands separator "1,150": 115000, "1.150,50": 115050, "1,150.50": 115050, "1150.5": 115050, "1150,5": 115050, "1150.50": 115050, "€ 1 150": 115000, "1150 EUR": 115000, "485.000": 48500000, "1.485.000,00": 148500000, "0,5": 50, "12": 1200, }; for (const [text, cents] of Object.entries(ok)) assert.equal(parseEuros(text), cents, text); for (const text of ["", "0", "0,00", "-5", "abc", "1,2,3x", "12.345,678", "1e5", "99999999999999999999"]) { assert.equal(parseEuros(text), null, JSON.stringify(text)); } }); test("rooms are read in half steps", () => { assert.equal(parseRoomsX2("2"), 4); assert.equal(parseRoomsX2("2.5"), 5); assert.equal(parseRoomsX2("2,5"), 5); assert.equal(parseRoomsX2(" 0.5 "), 1); assert.equal(parseRoomsX2("0"), 0); for (const bad of ["", "2.3", "-1", "abc", "101", "1/2"]) assert.equal(parseRoomsX2(bad), null, bad); }); test("a draft reproduces a listing for a fresh term", () => { const l = { kind: "rent", category: "apartment", geohash: "u1x0nw", price: { amount_cents: 115000, currency: "EUR", period: "month" }, area_m2: 62, rooms_x2: 5, features: ["Balkon"], title: "Helle Wohnung", description: "Ruhig.\nSüdseite.", } as unknown as Listing; assert.deepEqual(draftFromListing(l), { kind: "rent", category: "apartment", geohash: "u1x0nw", price: { amount_cents: 115000, currency: "EUR", period: "month" }, area_m2: 62, rooms_x2: 5, features: ["Balkon"], title: "Helle Wohnung", description: "Ruhig.\nSüdseite.", expires_in_days: MAX_LIFETIME_DAYS, }); assert.equal(draftFromListing({ ...l, delegate: "12D3KooWabcdefgh" } as Listing, 7).delegate, "12D3KooWabcdefgh"); assert.equal(draftFromListing(l, 7).expires_in_days, 7); assert.equal(draftFromListing({ ...l, price: { amount_cents: 1, currency: "EUR", period: "once" } } as Listing).price.period, "once"); // Optional parts default to empty, not undefined: the API takes them as given. const bare = draftFromListing({ geohash: "u1x0nw", kind: "sale", category: "house", title: "x" } as unknown as Listing); assert.deepEqual([bare.area_m2, bare.rooms_x2, bare.features, bare.description], [0, 0, [], ""]); });