feat(frontend): use offline global birth place hierarchy

This commit is contained in:
Jesse_Chen
2026-08-30 15:55:45 +08:00
parent f9a00dc568
commit 5c1d095f69
238 changed files with 655 additions and 42 deletions
+4 -4
View File
@@ -101,8 +101,8 @@ test("the picker offers three cascading levels instead of a free-text search", (
assert.match(pickerSource, /aria-label="出生城市"/);
assert.match(pickerSource, /aria-label="出生区县"/);
// The level is hidden rather than shown empty when it carries no choice.
assert.match(pickerSource, /\{province && !virtualCity && \(/);
assert.match(pickerSource, /\{city && districts\.length > 0 && \(/);
assert.match(pickerSource, /\{isChina && province && !virtualCity && \(/);
assert.match(pickerSource, /\{isChina && city && districts\.length > 0 && \(/);
assert.match(pickerSource, /不确定,用市区中心/);
assert.match(pickerSource, /精确到区 \/ 县就足够/);
});
@@ -126,7 +126,7 @@ test("mounting a saved birth place neither refetches nor clears the stored locat
// rectified birth-time candidate just because the profile dialog was opened.
assert.match(pickerSource, /const savedIsCurrent = resolved !== null/);
assert.match(pickerSource, /if \(syncKey === "saved"\) return;/);
assert.match(pickerSource, /if \(saved\.provinceCode \|\| saved\.timezoneId\) onChangeRef\.current\(null\);/);
assert.match(pickerSource, /if \(saved\.countryCode !== "CN" \|\| saved\.provinceCode \|\| saved\.timezoneId\) onChangeRef\.current\(null\);/);
// A changed birth date invalidates the stored offset, so the zone is resolved again.
assert.match(pickerSource, /mountedMoment === birthMoment/);
});
@@ -136,7 +136,7 @@ test("the profile stores administrative codes alongside coordinates and the IANA
assert.match(pageSource, /provinceCode: location\?\.provinceCode \|\| ""/);
assert.match(pageSource, /cityCode: location\?\.cityCode \|\| ""/);
assert.match(pageSource, /districtCode: location\?\.districtCode \|\| ""/);
assert.match(pageSource, /birthPlaceProvider: location \? "china_locations" : ""/);
assert.match(pageSource, /birthPlaceProvider: location\?\.provider \|\| ""/);
assert.match(pageSource, /timezoneSource: location \? "iana_historical" : ""/);
assert.match(pageSource, /birth_place_provider_id:\s*nextProfile\.birthPlaceProviderId/);
assert.match(pageSource, /timezone_id:\s*nextProfile\.timezoneId/);
@@ -0,0 +1,48 @@
import assert from "node:assert/strict";
import { existsSync, readFileSync, readdirSync } from "node:fs";
import { test } from "node:test";
const root = new URL("../", import.meta.url);
const index = JSON.parse(readFileSync(new URL("src/data/world-countries.json", root), "utf8")) as {
source: { repository: string; release: string; license: string };
countries: Array<{ code: string; name: string; regionCount: number }>;
};
const pickerSource = readFileSync(new URL("src/components/birth-place-picker.tsx", root), "utf8");
test("global location snapshot is country-split and sourced from the pinned release", () => {
assert.equal(index.source.repository, "dr5hn/countries-states-cities-database");
assert.equal(index.source.release, "v3.2-export.7");
assert.equal(index.source.license, "ODbL-1.0");
assert.equal(index.countries.length, 229);
assert.ok(index.countries.some((country) => country.code === "US"));
assert.ok(index.countries.every((country) => country.regionCount > 0));
const files = new Set(readdirSync(new URL("public/data/world-locations", root)));
assert.equal(files.size, index.countries.length);
for (const country of index.countries) assert.ok(files.has(`${country.code.toLowerCase()}.json`));
});
test("country data keeps the coordinates and IANA timezone needed for confirmation", () => {
const us = JSON.parse(readFileSync(new URL("public/data/world-locations/us.json", root), "utf8")) as {
code: string;
regions: Array<{ code: string; cities: Array<{ name: string; latitude: number; longitude: number; timezone: string }> }>;
};
const california = us.regions.find((region) => region.code === "US-CA");
const sanFrancisco = california?.cities.find((city) => city.name === "San Francisco");
assert.equal(us.code, "US");
assert.ok(california);
assert.ok(sanFrancisco);
assert.equal(typeof sanFrancisco?.latitude, "number");
assert.equal(typeof sanFrancisco?.longitude, "number");
assert.equal(sanFrancisco?.timezone, "America/Los_Angeles");
assert.equal(existsSync(new URL("src/data/WORLD_LOCATION_DATA.md", root)), true);
});
test("overseas UI uses cascading data files and has no search input", () => {
assert.match(pickerSource, /world-locations\/\$\{draft\.countryCode\.toLowerCase\(\)\}\.json/);
assert.match(pickerSource, /海外地点按国家、地区、城市逐级选择,不使用搜索/);
assert.match(pickerSource, /aria-label="出生国家或地区"/);
assert.match(pickerSource, /aria-label="出生州省或地区"/);
assert.match(pickerSource, /aria-label="出生海外城市"/);
assert.doesNotMatch(pickerSource, /role="combobox"/);
});