import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import test from "node:test"; import { preserveShallowEqual } from "../src/lib/preserve-shallow-equal.ts"; const pageSource = readFileSync(new URL("../src/app/page.tsx", import.meta.url), "utf8"); test("preserves the current profile reference when refreshed values are unchanged", () => { const current = { name: "测试用户", date: "2000-01-01", time: "", birthTimeSource: "period_only", birthTimePeriod: "evening", timezoneId: "Asia/Shanghai", latitude: 31.23, longitude: 121.47, timezoneOffset: null, }; const refreshed = { ...current }; assert.notEqual(refreshed, current); assert.equal(preserveShallowEqual(current, refreshed), current); }); test("uses the refreshed profile reference when a value changed", () => { const current = { name: "测试用户", birthTimePeriod: "evening", timezoneId: "Asia/Shanghai", }; const refreshed = { ...current, birthTimePeriod: "morning", }; assert.equal(preserveShallowEqual(current, refreshed), refreshed); }); test("account refresh preserves an equivalent normalized profile instead of replacing it", () => { const refreshAccountStart = pageSource.indexOf("async function refreshAccount()"); const refreshAccountSource = pageSource.slice( refreshAccountStart, pageSource.indexOf("function updateSession", refreshAccountStart), ); assert.match( refreshAccountSource, /setProfile\(\(current\) => preserveShallowEqual\(current, nextProfile\)\)/, ); assert.doesNotMatch(refreshAccountSource, /setProfile\(nextProfile\)/); });