From 85ef7026a669ea0da7ea1301d072abdc14d55fa5 Mon Sep 17 00:00:00 2001 From: JOY <5027251+JOY@users.noreply.github.com> Date: Sat, 21 Nov 2026 22:56:59 +0700 Subject: [PATCH 01/24] fix(importer): let the scoped spec backfill reach canonical masters (#1814) * fix(importer): let the scoped spec backfill reach canonical masters `--only` now accepts keys from CANONICAL_BACKFILL_SPECS_BY_MODEL_KEY as well as seed keys. A named canonical master is read by model_key, planned, diffed and counted by --expect like a seed, and written with the same payload as the unscoped run (shared canonicalMasterUpdate). A scoped run still reads no other canonical or AI master and no Item. A canonical key with no master row is never inserted: it stops an --apply (even with --insert-missing) before any write, and a dry run warns. A key that is neither a seed nor canonical still fails before any read. Closes #1804 * test(importer): pin canonical scope guards and unscoped canonical payload Review round 1 of #1814: the scoped canonical path refuses a row with no usable id before any write; tests cover an unnamed canonical master, a canonical problem blocking named seeds, a lookup error, repeated keys and the unscoped canonical payload. Co-Authored-By: Claude Opus 5.5 --- .../__tests__/backfill-specs-scope.test.ts | 297 +++++++++++++++++- .../src/shopee/jobs/backfill-specs.ts | 166 ++++++++-- 2 files changed, 438 insertions(+), 25 deletions(-) diff --git a/apps/importer/src/shopee/__tests__/backfill-specs-scope.test.ts b/apps/importer/src/shopee/__tests__/backfill-specs-scope.test.ts index 54aa1fe8..a0ce8639 100644 --- a/apps/importer/src/shopee/__tests__/backfill-specs-scope.test.ts +++ b/apps/importer/src/shopee/__tests__/backfill-specs-scope.test.ts @@ -8,9 +8,11 @@ import { parseBackfillSpecsArgs, runMasterSpecsBackfill, runMasterSpecsBackfillWith, + selectOnlyScope, selectOnlySeeds, type BackfillSpecsDeps, } from '../jobs/backfill-specs.js'; +import type { ProductSpecifications } from '@repo/shared-data'; import type { RateSupabaseClient } from '../types.js'; // The three masters of JOY's approved Production fix (DOS/Rate#1464). @@ -35,7 +37,9 @@ interface Op { */ function mockClient( existing: Record>, - masters: Array> = [] + masters: Array> = [], + /** model_keys whose lookup returns a database error. */ + failKeys: readonly string[] = [] ) { const ops: Op[] = []; const client = { @@ -69,6 +73,9 @@ function mockClient( }, maybeSingle() { const key = op.filters.find(([c]) => c === 'model_key')?.[1]; + if (typeof key === 'string' && failKeys.includes(key)) { + return Promise.resolve({ data: null, error: { message: 'boom' } }); + } const row = table === 'master_products' && typeof key === 'string' ? existing[key] : undefined; return Promise.resolve({ data: row ?? null, error: null }); @@ -249,7 +256,7 @@ test('an unknown --only key fails before any read or write', async () => { only: [APPROVED[0]!, 'apple:iphone-99:1tb'], expect: 2, }), - /no seed: "apple:iphone-99:1tb"/ + /no seed and no canonical entry: "apple:iphone-99:1tb"/ ); assert.equal(ops.length, 0, 'the client is never called'); assert.throws(() => selectOnlySeeds(['']), /no seed/); @@ -371,6 +378,27 @@ test('an unscoped backfill updates masters only and never inserts a seed (#1739) ], 'the existing seed, then the canonical master, in order; no insert' ); + // The canonical payload of an unscoped run (pinned for the #1804 refactor). + const canonEntry = CANONICAL_BACKFILL_SPECS_BY_MODEL_KEY[canonicalKey]!; + const canonPayload = live.writes()[1]!.payload!; + assert.deepEqual(Object.keys(canonPayload), [ + 'ram_gb', + 'storage_gb', + 'screen_size_inch', + 'battery_mah', + 'has_5g', + 'colors', + 'specifications', + 'key_specs', + 'updated_at', + ]); + assert.equal(canonPayload.battery_mah, canonEntry.battery_mah); + assert.equal(canonPayload.has_5g, canonEntry.has_5g); + assert.deepEqual(canonPayload.colors, canonEntry.colors); + assert.equal( + (canonPayload.specifications as ProductSpecifications).cellular?.bands_5g, + canonEntry.specifications.cellular?.bands_5g + ); assert.equal( live.writes().some(o => o.op === 'insert'), false, @@ -527,3 +555,268 @@ test('parseBackfillSpecsArgs reads --expect-inserts (#1739)', () => { /--audit-only/ ); }); + +// The three published canonical masters #1803 found out of reach of a scoped run (DOS/Rate#1804). +const CANONICAL = ['apple:iphone-15:128gb', 'apple:iphone-16:128gb', 'apple:iphone-16-plus:256gb']; +// The full master list read of an unscoped run. +const FULL_LIST_COLUMNS = 'id, name, model_key, key_specs, specifications'; + +/** The canonical masters as Production held them before #1758: template values. */ +function canonicalExisting(): Record> { + const rows: Record> = {}; + CANONICAL.forEach((key, i) => { + rows[key] = { + id: `canon-${i}`, + name: `Canonical ${i}`, + specifications: { + connectivity: { bluetooth: '5.3, A2DP, LE' }, + cellular: { bands_5g: 'SA/NSA/Sub6' }, + }, + key_specs: {}, + battery_mah: 1, + }; + }); + return rows; +} + +function captureLogs(run: () => Promise): Promise<{ result: T; text: string }> { + const lines: string[] = []; + const original = { log: console.log, warn: console.warn }; + console.log = (...args: unknown[]) => lines.push(args.map(String).join(' ')); + console.warn = (...args: unknown[]) => lines.push(args.map(String).join(' ')); + return run() + .then(result => ({ result, text: lines.join('\n') })) + .finally(() => { + console.log = original.log; + console.warn = original.warn; + }); +} + +test('selectOnlyScope splits seeds from canonical keys and refuses anything else (#1804)', () => { + assert.ok( + CANONICAL.every(k => Object.hasOwn(CANONICAL_BACKFILL_SPECS_BY_MODEL_KEY, k)), + 'the #1803 keys are canonical entries' + ); + const scope = selectOnlyScope([APPROVED[0]!, ...CANONICAL]); + assert.deepEqual( + scope.seeds.map(s => s.model_key), + [APPROVED[0]] + ); + assert.deepEqual(scope.canonicalKeys, CANONICAL); + assert.throws( + () => selectOnlyScope([CANONICAL[0]!, 'apple:iphone-99:1tb', '']), + /2 key\(s\) with no seed and no canonical entry: "apple:iphone-99:1tb", ""/ + ); + // A key that is a seed and a canonical entry stays a seed, as in an unscoped run. + const seed = FLAGSHIP_DEVICES_TO_SEED[0]!; + const both = selectOnlyScope([seed.model_key], FLAGSHIP_DEVICES_TO_SEED, { + [seed.model_key]: CANONICAL_BACKFILL_SPECS_BY_MODEL_KEY[CANONICAL[0]!]!, + }); + assert.deepEqual( + both.seeds.map(s => s.model_key), + [seed.model_key] + ); + assert.deepEqual(both.canonicalKeys, []); + // The publish job still takes seeds only. + assert.throws(() => selectOnlySeeds([CANONICAL[0]!]), /no seed/); +}); + +test('--only reaches the named canonical masters and nothing else (#1804)', async () => { + const otherCanonical = { id: 'canon-other', name: 'Other', model_key: 'acme:canon:1gb' }; + const { client, ops, writes } = mockClient({ ...allSeedsExisting(), ...canonicalExisting() }, [ + otherCanonical, + { id: 'ai', name: 'AI', model_key: 'acme:phone:64gb' }, + ]); + const enrichCalls: string[] = []; + const only = [APPROVED[0]!, ...CANONICAL]; + const result = await runMasterSpecsBackfillWith( + client, + { ...deps(enrichCalls), enricher: { isConfigured: () => true } }, + { apply: true, only, expect: 4 } + ); + assert.equal(result.planned, 4); + assert.equal(result.catalogUpdated, 4); + assert.equal(result.catalogInserted, 0); + assert.equal(result.itemsUpdated, 0); + + const lookedUp = ops + .filter(o => o.op === 'select' && o.filters.some(([c]) => c === 'model_key')) + .map(o => o.filters.find(([c]) => c === 'model_key')![1]); + assert.deepEqual([...lookedUp].sort(), [...only].sort(), 'only the named masters are read'); + assert.equal( + ops.some(o => o.columns === FULL_LIST_COLUMNS), + false, + 'no full master list read' + ); + assert.equal( + ops.some(o => o.table === 'items'), + false, + 'no rate.items read or write' + ); + assert.deepEqual(enrichCalls, [], 'no AI enrichment'); + + const canon = canonicalExisting(); + const w = writes(); + assert.ok(w.every(o => o.table === 'master_products' && o.op === 'update')); + assert.deepEqual( + w.map(o => o.filters[0]![1]), + [allSeedsExisting()[APPROVED[0]!]!.id, ...CANONICAL.map(k => canon[k]!.id)], + 'the seed, then the canonical masters in --only order' + ); + for (const [i, key] of CANONICAL.entries()) { + const entry = CANONICAL_BACKFILL_SPECS_BY_MODEL_KEY[key]!; + const payload = w[i + 1]!.payload!; + assert.deepEqual( + Object.keys(payload).sort(), + [ + 'battery_mah', + 'colors', + 'has_5g', + 'key_specs', + 'ram_gb', + 'screen_size_inch', + 'specifications', + 'storage_gb', + 'updated_at', + ], + `${key}: the canonical value columns only (no name, slug, status or category)` + ); + assert.equal(payload.battery_mah, entry.battery_mah); + const specs = payload.specifications as ProductSpecifications; + assert.equal(specs.connectivity?.bluetooth, entry.specifications.connectivity?.bluetooth); + assert.equal(specs.cellular?.bands_5g, entry.specifications.cellular?.bands_5g); + assert.notEqual(specs.connectivity?.bluetooth, '5.3, A2DP, LE'); + } +}); + +test('a scoped canonical dry run plans, diffs and checks --expect like a seed (#1804)', async () => { + const { client, writes } = mockClient(canonicalExisting()); + const { result, text } = await captureLogs(() => + runMasterSpecsBackfillWith(client, deps(), { only: CANONICAL, expect: 2 }) + ); + assert.equal(result.apply, false); + assert.equal(result.planned, 3); + assert.deepEqual(writes(), [], 'a dry run never writes'); + assert.match(text, /Plan \[mode=DRY-RUN, scoped\]: 3 master\(s\) to update or insert/); + assert.match(text, /not the expected 2/); + for (const key of CANONICAL) { + assert.ok(text.includes(`[Canonical Specs] ${key}: would change`), `${key}: would change`); + } + assert.match( + text, + /specifications\.connectivity\.bluetooth: \\?"5\.3, A2DP, LE\\?" -> \\?"5\.3\\?"/ + ); + assert.match(text, /specifications\.cellular\.bands_5g: \\?"SA\/NSA\/Sub6\\?" -> /); + + // The same --expect mismatch stops an --apply before the first write. + const live = mockClient(canonicalExisting()); + await assert.rejects( + runMasterSpecsBackfillWith(live.client, deps(), { apply: true, only: CANONICAL, expect: 2 }), + /--expect 2 does not match the plan of 3 master\(s\) \(apple:iphone-15:128gb, apple:iphone-16:128gb, apple:iphone-16-plus:256gb\): stopped before any write/ + ); + assert.deepEqual(live.writes(), []); +}); + +test('a named canonical key with no master row is never inserted (#1804)', async () => { + const existing = canonicalExisting(); + delete existing[CANONICAL[1]!]; + for (const options of [ + { apply: true, only: CANONICAL, expect: 2 }, + { apply: true, only: CANONICAL, expect: 3, insertMissing: true }, + ]) { + const live = mockClient(existing); + await assert.rejects( + runMasterSpecsBackfillWith(live.client, deps(), options), + new RegExp( + `no master row for canonical key\\(s\\) ${CANONICAL[1]!}; a canonical entry is never inserted: stopped before any write` + ) + ); + assert.deepEqual(live.writes(), [], 'no insert, no update'); + } + const dry = mockClient(existing); + const { result, text } = await captureLogs(() => + runMasterSpecsBackfillWith(dry.client, deps(), { only: CANONICAL }) + ); + assert.deepEqual(dry.writes(), []); + assert.equal(result.planned, 2); + assert.match(text, /a canonical entry is never inserted; an --apply would stop before any write/); +}); + +test('the CLI parser passes canonical keys through --only (#1804)', () => { + assert.deepEqual( + parseBackfillSpecsArgs(['--only', CANONICAL.join(','), '--apply', '--expect', '3']), + { apply: true, auditOnly: false, only: CANONICAL, expect: 3 } + ); +}); + +test('a canonical master that is not named is neither read nor written (#1804)', async () => { + const [named1, named2, unnamed] = CANONICAL as [string, string, string]; + const existing = canonicalExisting(); + const { client, ops, writes } = mockClient(existing, [ + { id: existing[unnamed]!.id, name: 'Unnamed', model_key: unnamed, specifications: {} }, + ]); + const result = await runMasterSpecsBackfillWith(client, deps(), { + apply: true, + only: [named2, named1], + expect: 2, + }); + assert.equal(result.planned, 2); + assert.equal( + ops.some(o => o.filters.some(([, v]) => v === unnamed || v === existing[unnamed]!.id)), + false, + `${unnamed} is never read or written` + ); + assert.deepEqual( + writes().map(o => o.filters[0]![1]), + [existing[named2]!.id, existing[named1]!.id] + ); +}); + +test('a canonical problem stops the named seeds too, before any write (#1804)', async () => { + const seedKey = APPROVED[0]!; + const only = [seedKey, ...CANONICAL]; + const withSeed = () => ({ ...allSeedsExisting(), ...canonicalExisting() }); + + // A missing canonical row: the seed update is not written either. + const missing = withSeed(); + delete missing[CANONICAL[2]!]; + const a = mockClient(missing); + await assert.rejects( + runMasterSpecsBackfillWith(a.client, deps(), { apply: true, only, expect: 3 }), + /no master row for canonical key\(s\).*stopped before any write/ + ); + assert.deepEqual(a.writes(), []); + + // A lookup error on a canonical key. + const b = mockClient(withSeed(), [], [CANONICAL[1]!]); + await assert.rejects( + runMasterSpecsBackfillWith(b.client, deps(), { apply: true, only, expect: 4 }), + new RegExp(`Failed to look up master product ${CANONICAL[1]!}: boom`) + ); + assert.deepEqual(b.writes(), []); + + // A row with no usable id. + for (const id of ['', ' ', 42, null]) { + const badId = withSeed(); + badId[CANONICAL[0]!] = { ...badId[CANONICAL[0]!]!, id }; + const c = mockClient(badId); + await assert.rejects( + runMasterSpecsBackfillWith(c.client, deps(), { apply: true, only, expect: 4 }), + new RegExp(`${CANONICAL[0]!} has no usable id: stopped before any write`), + String(id) + ); + assert.deepEqual(c.writes(), [], `id ${JSON.stringify(id)}: nothing written`); + } +}); + +test('repeated or padded canonical keys plan each master once (#1804)', async () => { + const { client, writes } = mockClient(canonicalExisting()); + const key = CANONICAL[0]!; + const result = await runMasterSpecsBackfillWith(client, deps(), { + apply: true, + only: [key, ` ${key} `, key], + expect: 1, + }); + assert.equal(result.planned, 1); + assert.equal(writes().length, 1); +}); diff --git a/apps/importer/src/shopee/jobs/backfill-specs.ts b/apps/importer/src/shopee/jobs/backfill-specs.ts index c7eb2733..3877aca1 100644 --- a/apps/importer/src/shopee/jobs/backfill-specs.ts +++ b/apps/importer/src/shopee/jobs/backfill-specs.ts @@ -12,9 +12,13 @@ * Scoped run (DOS/Rate#1464), for an approved fix of named masters only: * ... backfill-specs.ts --only (Dry-run: per-row field diff + planned count) * ... backfill-specs.ts --only --apply --expect - * --only Plans and writes only these seed model_keys: no other seed is inserted or - * updated, no canonical or AI master and no rate.items row is touched. A key - * with no seed fails before any database read. + * --only Plans and writes only these model_keys: each is a seed or a canonical + * entry (CANONICAL_BACKFILL_SPECS_BY_MODEL_KEY, DOS/Rate#1804). No other seed + * or canonical master is inserted or updated, no AI master and no rate.items + * row is touched. A key that is neither fails before any database read. A + * canonical key is planned, diffed and counted like a seed, and is never + * inserted: with no master row it stops an --apply (even with + * --insert-missing) before any write. * --expect With --apply, stops before the first write unless the plan updates or * inserts exactly n masters. A dry run logs whether the plan matches. * --expect-inserts Optional, like --expect for the inserts alone, so a row that appears @@ -97,9 +101,10 @@ export function requiredSpecGroups( export interface BackfillSpecsOptions { readonly apply?: boolean; /** - * `--only`: the seed `model_key`s to update. Nothing else is inserted or updated: no other - * seed, no canonical or AI master, no `rate.items` row. A key with no seed is an error - * before any read; a key with no master row stops an apply before any write. + * `--only`: the seed or canonical `model_key`s to update. Nothing else is inserted or + * updated: no other seed or canonical master, no AI master, no `rate.items` row. A key + * that is neither a seed nor a canonical entry is an error before any read; a key with no + * master row stops an apply before any write (a canonical key even with `insertMissing`). */ readonly only?: readonly string[]; /** @@ -621,6 +626,67 @@ export function selectOnlySeeds( return seeds.filter(s => wanted.has(s.model_key)); } +type CanonicalBackfillEntry = (typeof CANONICAL_BACKFILL_SPECS_BY_MODEL_KEY)[string]; + +/** + * The masters a backfill `--only` list names: the seeds, in seed order, and the canonical + * entries (`CANONICAL_BACKFILL_SPECS_BY_MODEL_KEY`), in list order (DOS/Rate#1804). A key + * that is both stays a seed, as in an unscoped run. Throws, naming them, on keys that are + * neither, so a typo never turns a scoped run into a smaller one. + */ +export function selectOnlyScope( + only: readonly string[], + seeds: readonly FlagshipDeviceSeed[] = FLAGSHIP_DEVICES_TO_SEED, + canonical: Readonly< + Record + > = CANONICAL_BACKFILL_SPECS_BY_MODEL_KEY +): { seeds: FlagshipDeviceSeed[]; canonicalKeys: string[] } { + if (!Array.isArray(only) || only.length === 0) { + throw new Error('--only needs at least one model_key'); + } + const wanted = new Set(only.map(k => (typeof k === 'string' ? k.trim() : ''))); + const seedKeys = new Set(seeds.map(s => s.model_key)); + const isCanonical = (k: string) => Object.hasOwn(canonical, k); + const unknown = [...wanted].filter(k => !k || (!seedKeys.has(k) && !isCanonical(k))); + if (unknown.length > 0) { + throw new Error( + `--only names ${unknown.length} key(s) with no seed and no canonical entry: ${unknown.map(k => JSON.stringify(k)).join(', ')}` + ); + } + return { + seeds: seeds.filter(s => wanted.has(s.model_key)), + canonicalKeys: [...wanted].filter(k => !seedKeys.has(k)), + }; +} + +/** + * The columns a canonical entry writes onto its existing master: its value columns, and its + * specs and key specs merged into the stored ones. No timestamp. Shared by the unscoped run + * and a scoped `--only` run, so both write the same thing. + */ +export function canonicalMasterUpdate( + existing: { specifications?: unknown; key_specs?: unknown } | null | undefined, + entry: CanonicalBackfillEntry +): Record { + return { + ram_gb: entry.ram_gb, + storage_gb: entry.storage_gb, + screen_size_inch: entry.screen_size_inch, + battery_mah: entry.battery_mah, + has_5g: entry.has_5g, + colors: entry.colors, + specifications: mergeProductSpecifications( + existing?.specifications, + entry.specifications, + entry.colors + ), + key_specs: mergeKeySpecs( + existing?.key_specs, + entry.key_specs as unknown as Record + ), + }; +} + /** What the job needs besides the client, so tests can pass stand-ins. */ export interface BackfillSpecsDeps { readonly enricher: { isConfigured(): boolean }; @@ -651,6 +717,8 @@ type MasterWrite = modelKey: string; name: string; payload: Record; + /** The field diff, for a canonical key named in a scoped run (DOS/Rate#1804). */ + changes?: string[]; } | { kind: 'ai-enrich'; id: string; name: string }; @@ -687,7 +755,7 @@ export async function runMasterSpecsBackfill( ): Promise { if (!options || typeof options !== 'object' || Array.isArray(options)) options = {}; // An unknown --only key fails before the client exists. - if (options.only !== undefined) selectOnlySeeds(options.only); + if (options.only !== undefined) selectOnlyScope(options.only); const { createClient } = await import('@supabase/supabase-js'); const { EntityMatcher } = await import('../entity-matcher.js'); @@ -730,7 +798,8 @@ export async function runMasterSpecsBackfillWith( const apply = options.apply === true; const allSeeds = deps.seeds ?? FLAGSHIP_DEVICES_TO_SEED; const scoped = options.only !== undefined; - const seeds = scoped ? selectOnlySeeds(options.only!, allSeeds) : allSeeds; + const scope = scoped ? selectOnlyScope(options.only!, allSeeds) : undefined; + const seeds = scope ? scope.seeds : allSeeds; const expect = options.expect; if (expect !== undefined && (!Number.isInteger(expect) || expect < 0)) { throw new Error(`expect must be a whole number of masters, got ${String(expect)}`); @@ -868,8 +937,51 @@ export async function runMasterSpecsBackfillWith( } } - // Canonical and AI masters: never in a scoped run. + // Canonical masters a scoped run names (DOS/Rate#1804): read by model_key, planned and + // diffed like a seed. A canonical entry has no insert payload, so a missing row is never + // inserted. No other canonical or AI master is read in a scoped run. const masterWrites: MasterWrite[] = []; + const scopedCanonicalMissing: string[] = []; + for (const modelKey of scope?.canonicalKeys ?? []) { + const entry = CANONICAL_BACKFILL_SPECS_BY_MODEL_KEY[modelKey]!; + const { data: existing, error: lookupErr } = await supabase + .from('master_products') + .select( + 'id, name, key_specs, specifications, ram_gb, storage_gb, screen_size_inch, battery_mah, has_5g, colors' + ) + .eq('model_key', modelKey) + .maybeSingle(); + if (lookupErr) { + throw new Error(`Failed to look up master product ${modelKey}: ${lookupErr.message}`); + } + if (!existing) { + scopedCanonicalMissing.push(modelKey); + continue; + } + const row = existing as unknown as { id: unknown; name?: unknown } & Record; + // The update goes by id: an id the unscoped run would skip stops the plan instead. + if (typeof row.id !== 'string' || !row.id.trim() || /\p{Cc}/u.test(row.id)) { + throw new Error(`Master product ${modelKey} has no usable id: stopped before any write`); + } + const update = canonicalMasterUpdate( + row as { specifications?: unknown; key_specs?: unknown }, + entry + ); + masterWrites.push({ + kind: 'canonical-update', + id: row.id, + modelKey, + name: + typeof row.name === 'string' + ? stripKeySpecControlChars(row.name).replace(/\s+/g, ' ').trim().slice(0, 200) || + '(unnamed)' + : '(unnamed)', + payload: { ...update, updated_at: new Date().toISOString() }, + changes: specUpdateChanges(row, update), + }); + } + + // Unnamed canonical and AI masters: never in a scoped run. let skipped = 0; if (!scoped) { const masters = await fetchAllPages('Failed to fetch master_products', (from, to) => @@ -911,20 +1023,9 @@ export async function runMasterSpecsBackfillWith( modelKey: safeModelKey, name: safeName, payload: { - ram_gb: canonicalEntry.ram_gb, - storage_gb: canonicalEntry.storage_gb, - screen_size_inch: canonicalEntry.screen_size_inch, - battery_mah: canonicalEntry.battery_mah, - has_5g: canonicalEntry.has_5g, - colors: canonicalEntry.colors, - specifications: mergeProductSpecifications( - (m as { specifications?: unknown }).specifications, - canonicalEntry.specifications, - canonicalEntry.colors - ), - key_specs: mergeKeySpecs( - (m as { key_specs?: unknown }).key_specs, - canonicalEntry.key_specs as unknown as Record + ...canonicalMasterUpdate( + m as { specifications?: unknown; key_specs?: unknown }, + canonicalEntry ), updated_at: new Date().toISOString(), }, @@ -937,6 +1038,18 @@ export async function runMasterSpecsBackfillWith( } } + // A named canonical key with no master row: there is nothing to update and a canonical + // entry is never inserted, so --expect alone must not hide it (DOS/Rate#1804). + if (scopedCanonicalMissing.length > 0) { + const message = `--only: no master row for canonical key(s) ${scopedCanonicalMissing.join(', ')}; a canonical entry is never inserted`; + if (apply) throw new Error(`${message}: stopped before any write`); + logger.warn( + 'backfill.scoped_canonical_missing', + `DRY-RUN: ${message}; an --apply would stop before any write`, + { modelKeys: scopedCanonicalMissing } + ); + } + // A scoped run fixes existing masters: a named key with no master row would insert a draft // (and through the master trigger possibly an Item), which --expect alone cannot catch. // Unless the run asks for the insert by name (--insert-missing, DOS/Rate#1737). @@ -1088,6 +1201,13 @@ export async function runMasterSpecsBackfillWith( } } catalogUpdated++; + if (w.changes) { + logDiff( + `[Canonical Specs] ${w.modelKey}: ${apply ? 'changed' : 'would change'}`, + w.modelKey, + w.changes + ); + } logger.info( 'backfill.canonical_updated', `[Canonical Specs] ${w.name} (${w.modelKey}): ${apply ? 'UPDATED' : 'DRY-RUN WOULD UPDATE'}`, From bf6b47279954e94c9526b6e2a114ec4dd9f964e7 Mon Sep 17 00:00:00 2001 From: JOY <5027251+JOY@users.noreply.github.com> Date: Sat, 21 Nov 2026 23:03:27 +0700 Subject: [PATCH 02/24] chore(ops): add po:antigravity runtime to claim-lock and PO config (#1806) * chore(ops): add po:antigravity runtime to claim-lock and PO config Register antigravity (po:antigravity) in PO_TOOLS alongside claude and qwen so board.mjs stamps po:antigravity on claim and enforces 3-way mutual exclusion across PO lines, while keeping claude as the sole dev -> main promoter in promote.mjs. Update docs/product-owner-config.md (po_labels, agent_model Antigravity mapping, cc-po 2.18.0 heartbeat and context compaction keys), AGENTS.md, and .claude/skills/product-owner/SKILL.md. Closes #1805 * chore(ops): address round-1 review on po:antigravity docs and labels Add po:antigravity preset to .github/labels.yml and Claimant example to .github/pull_request_template.md, qualify per-line Antigravity check-in file names in AGENTS.md and SKILL.md, and align multi-runtime wording in SKILL.md and board.mjs JSDoc. Refs #1805 --- .claude/skills/product-owner/SKILL.md | 67 +++++---- .github/labels.yml | 9 +- .github/pull_request_template.md | 2 +- AGENTS.md | 14 +- docs/product-owner-config.md | 23 +-- scripts/claim-lock.mjs | 11 +- scripts/ops/board.mjs | 11 +- scripts/ops/board.test.mjs | 208 ++++++++++++++++++++------ scripts/ops/promote.test.mjs | 21 ++- scripts/task-claim-check.test.mjs | 10 ++ 10 files changed, 272 insertions(+), 104 deletions(-) diff --git a/.claude/skills/product-owner/SKILL.md b/.claude/skills/product-owner/SKILL.md index 7c262f94..905176cd 100644 --- a/.claude/skills/product-owner/SKILL.md +++ b/.claude/skills/product-owner/SKILL.md @@ -7,8 +7,9 @@ user-invocable: true # Product Owner -One Claude Desktop session per product line is its Product Owner. The PO -coordinates; the sub-sessions it starts do the investigation and the work. +One session per product line and PO runtime (Claude Desktop, Qwen Code Desktop, +or Antigravity) is its Product Owner. The PO coordinates; the sub-sessions it +starts do the investigation and the work. `AGENTS.md` and the documents it routes to win over this skill. This skill adds the PO role on top of Rate's existing protocol (`AGENTS.md` sections 0, 1 and 5, `Docs/Resources/RELEASE.md`); it does not replace the claim labels, @@ -34,27 +35,31 @@ apply to it: "Starting a sub-session" (its brief), "Review gate", ## Lines and names -Two POs may run on the same repository at once: one in Claude Code and one -in Qwen Code Desktop (DOS/Rate#1673). They keep apart like this: +Multiple POs may run on the same repository at once: in Claude Code, Qwen +Code Desktop, and Antigravity (DOS/Rate#1673, DOS/Rate#1805). They keep apart +like this: -- **PO line label.** `po:claude` or `po:qwen` says which PO dispatched an - issue. A PO dispatches only issues with its own label or none, and adds its - label when it dispatches +- **PO line label.** `po:claude`, `po:qwen`, or `po:antigravity` says which PO + dispatched an issue. A PO dispatches only issues with its own label or none, + and adds its label when it dispatches (`gh issue edit -R DOS/Rate --add-label po:claude`). `board.mjs` adds - the claimant's line label on a claim and refuses an issue of the other line - without `--take-over` (JOY's assignment only). A claimant of a tool that - runs no PO (for example `codex:...`) gets no `po:*` label and is not + the claimant's line label on a claim and refuses an issue of another PO's + line without `--take-over` (JOY's assignment only). A claimant of a tool + that runs no PO (for example `codex:...`) gets no `po:*` label and is not refused by line; such an agent still respects the labels by this rule. - **Separate state.** Each PO keeps its own state and check-in file, named - per tool (`po-state.claude.md`, `po-state.qwen.md`), outside the - repository (in that PO's own notes folder), never committed, and never - writes the other's. + per tool and per line (`po-state.claude.md`, `po-state.qwen.md`, or + per-line `*-antigravity-po-checkin.md` under `~/.claude/po-state/` for + Antigravity: `dos-rate-core-antigravity-po-checkin.md` for Rate Core and + `dos-rate-services-services-antigravity-po-checkin.md` for Rate Services), + outside the repository (in that PO's own notes or `po-state` folder), never + committed, and never writes another PO's or line's file. - **One promoter.** Only the Claude Code PO runs `promote.mjs` and the - `verify-live.mjs` check after it. The Qwen PO asks the Claude Code PO (or - JOY, if it cannot reach it) when its merged work needs a promotion. - `promote.mjs` refuses to run when the session's claimant (resolved as - `board.mjs` does) is not `claude:` or cannot be resolved: a guard against - mistakes, not access control. + `verify-live.mjs` check after it. The Qwen and Antigravity POs ask the + Claude Code PO (or JOY, if they cannot reach it) when their merged work + needs a promotion. `promote.mjs` refuses to run when the session's claimant + (resolved as `board.mjs` does) is not `claude:` or cannot be resolved: a + guard against mistakes, not access control. - The claim lock (see "Claims") keeps their sub-sessions off each other's issues even when two take the same issue at the same moment. @@ -172,7 +177,9 @@ names a role and two sessions can share it: workflow runs on it. - The lock records the **claimant**, `:`. Claude Code sessions get `claude:` automatically (the desktop session id, - the one `SendMessage` uses). Qwen Code sessions pass `--claimant qwen:` on every call. + the one `SendMessage` uses). Qwen Code sessions pass + `--claimant qwen:` and Antigravity sessions pass + `--claimant antigravity:` on every call. `RATE_CLAIMANT` may only be set for one session, never in a shell profile or an environment other sessions (sub-sessions included) inherit; a Claude Code session id always wins over it. `board.mjs` prints @@ -189,7 +196,7 @@ names a role and two sessions can share it: - A lock taken before a merged PR last closed the issue is stale: the merge ended that work. On a reopened issue, a claim (`in-progress` or `in-review`) replaces a stale lock without `--take-over` (and its stale - `agent:*` label; the other PO's `po:*` label still needs `--take-over`), + `agent:*` label; another PO's `po:*` label still needs `--take-over`), and the lock comment names the lock it replaced. A release does not: on an open issue only the holder releases, or the PO with `--force-release`. A close that was not a merge (not planned, by hand) never makes a lock stale. @@ -270,8 +277,10 @@ the next session claims only after that comment. pages over Project #9 (more than 300 items) time out with 504 or return only the first 30 items; look an issue's board item up from the issue as `AGENTS.md` 1 shows, or page Project #9 GraphQL in pages of 50 or fewer. -2. Skip issues labelled with the other PO's line (`po:qwen` for the Claude - Code PO, `po:claude` for the Qwen PO); see "Lines and names". +2. Skip issues labelled with another PO's line (`po:qwen` and + `po:antigravity` for the Claude Code PO, `po:claude` and `po:antigravity` + for the Qwen PO, `po:claude` and `po:qwen` for the Antigravity PO); see + "Lines and names". Order: `incident` first, then `status:ready`, then other open issues. `status:in-progress` and `status:in-review` issues are someone's claim: check whether they are live or orphaned (see "Claims"). @@ -333,8 +342,9 @@ claim. Use `start_session` with a fresh context and its own worktree. Use `initiation: "user_asked"` only when JOY told the PO to dispatch work in the PO chat; otherwise the session is a proposal card JOY has to start. Worktree: -(`/.claude/worktrees/`, `AGENTS.md` 0.5). The brief is the only -context the session gets, so it carries: +(`/.claude/worktrees/` or `/.gemini/worktrees/` by +runtime, `AGENTS.md` 0.5). The brief is the only context the session gets, so +it carries: - **Task**: the issue link, the problem in a few lines, and the acceptance from the issue. @@ -346,7 +356,8 @@ context the session gets, so it carries: ("Starting a sub-session", "Review gate", "Coordinating", "Stop lines"). - **Process**: claim with `node scripts/ops/board.mjs --status in-progress --agent ` before - editing (a Qwen sub-session adds `--claimant qwen:`); + editing (a Qwen sub-session adds `--claimant qwen:`; an + Antigravity sub-session adds `--claimant antigravity:`); English issue, PR and commit text; PR into `dev` with `Closes #` and a `Claimant: ` line; `board.mjs --status in-review --agent ` once the PR is open; the review gate (below); the PO acceptance gate; then @@ -367,9 +378,9 @@ context the session gets, so it carries: or the shared notes folder, not here. - **Reporting**: the wrap-up is a recap only, never a question or buttons for JOY. Decisions, approvals and access needs go to the PO with SendMessage - (`to`: the PO's `local_...` session id). Reports are in Vietnamese with full - diacritics and no em-dashes or en-dashes; the first line says complete, - running or stopped. + (`to`: the PO's `local_...` session id or Antigravity `conversationId`). + Reports are in Vietnamese with full diacritics and no em-dashes or + en-dashes; the first line says complete, running or stopped. ## Review gate diff --git a/.github/labels.yml b/.github/labels.yml index fbcc2af2..96cac737 100644 --- a/.github/labels.yml +++ b/.github/labels.yml @@ -67,12 +67,15 @@ color: 5319e7 description: Task completed and verified -# Product Owner line, AGENTS.md section 6 (DOS/Rate#1673): which PO -# dispatched the issue. A PO dispatches only issues with its own label or -# none; scripts/ops/board.mjs adds the claimant's line on a claim. +# Product Owner line, AGENTS.md section 6 (DOS/Rate#1673, DOS/Rate#1805): +# which PO dispatched the issue. A PO dispatches only issues with its own +# label or none; scripts/ops/board.mjs adds the claimant's line on a claim. - name: po:claude color: 0052cc description: Dispatched by the Claude Code Product Owner - name: po:qwen color: 0052cc description: Dispatched by the Qwen Code Product Owner +- name: po:antigravity + color: 0052cc + description: Dispatched by the Antigravity Product Owner diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 3771bc68..4d5115b5 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -12,7 +12,7 @@ Claimant: : +claude:local_1234abcd, qwen:5678efgh or antigravity:. --> ## Checklist - [ ] Build passes: `pnpm --filter build` diff --git a/AGENTS.md b/AGENTS.md index a2d9c70a..d06318d5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -45,7 +45,7 @@ Tài liệu này tổng hợp đầy đủ **User-Level Hard Rules (`~/AGENTS.md - `node scripts/ops/merge-when-green.mjs `: chờ commit head xanh rồi merge, ghim theo SHA head. Xanh nghĩa là mọi check run và mọi commit status (Vercel) đều đạt, và các check mà ruleset bắt buộc đã báo kết quả. PR vào `main` luôn dùng merge commit, PR khác mặc định squash. `--ignore ""` chỉ dùng khi một check không bắt buộc đang hỏng có lý do đã ghi trong PR. - `node scripts/ops/promote.mjs`: mở hoặc dùng lại PR promote `dev` -> `main`, liệt kê các PR được phát hành, merge bằng merge commit khi CI xanh. Script từ chối chạy khi claimant của session (tính như `board.mjs`) không phải `claude:` (#1675, xem Mục 6). - `node scripts/ops/verify-live.mjs --wait 900`: kiểm tra rate.vn chạy đúng commit của `main` (`/api/health` trả `commit`), database đọc được và các route chính trả 200. Script đọc lại `main` mỗi vòng chờ. Chỉ khi bản live chậm hơn `main`, nó mới in lệnh `vercel promote --scope metados`; nếu bản live đi trước hoặc lệch nhánh so với `main`, nó dặn không làm gì. Không bao giờ redeploy hay promote một deployment build từ commit khác (`promote` không build lại, và bật lại tự gán domain sau một lần `vercel rollback`). - - `node scripts/ops/board.mjs --status in-progress|in-review|ready|backlog|done --agent [--claimant :]`: lấy, giữ hoặc nhả khóa claim `refs/claims/` của session, rồi cập nhật nhãn và Project #9 (xem mục 1). Script từ chối issue đang bị session khác giữ khóa, đang mang nhãn `agent:*` của agent khác hoặc nhãn `po:*` của PO kia; `--take-over` chỉ dùng khi agent đó đã bàn giao hoặc JOY chỉ định, `--adopt` chỉ cho claim cũ (trước khóa) của chính session này. `--force-release --agent PO --reason "..."` gỡ khóa mồ côi (PO). + - `node scripts/ops/board.mjs --status in-progress|in-review|ready|backlog|done --agent [--claimant :]`: lấy, giữ hoặc nhả khóa claim `refs/claims/` của session, rồi cập nhật nhãn và Project #9 (xem mục 1). Script từ chối issue đang bị session khác giữ khóa, đang mang nhãn `agent:*` của agent khác hoặc nhãn `po:*` của PO khác; `--take-over` chỉ dùng khi agent đó đã bàn giao hoặc JOY chỉ định, `--adopt` chỉ cho claim cũ (trước khóa) của chính session này. `--force-release --agent PO --reason "..."` gỡ khóa mồ côi (PO). 7. **Chủ động thực thi liên tục & đề xuất việc tiếp theo (Always-On Proactive Backlog - Skill `proactive-backlog`):** - **Luôn kích hoạt mặc định ở MỌI lượt hội thoại (không chỉ trong `/goal`):** Mọi Agent (Antigravity, Claude Code, Codex) phải luôn tuân theo quy trình khép kín của skill `proactive-backlog` (`~/.gemini/config/skills/proactive-backlog/SKILL.md`, `~/.claude/skills/proactive-backlog/SKILL.md`, `~/.codex/skills/proactive-backlog/SKILL.md`). @@ -88,10 +88,10 @@ gh issue list -R DOS/Rate-Services --state open **Luôn claim bằng `board.mjs`**, một lệnh làm cả khóa claim, nhãn lẫn board (tự thêm issue vào Project #9 nếu chưa có, chỉ giữ một nhãn `status:*`): `node scripts/ops/board.mjs --status in-progress --agent $agentNick`. Các trạng thái khác: `ready`, `in-review`, `done`, `backlog`. Các lệnh `gh` dưới đây chỉ đặt nhãn và board, **không lấy khóa claim**, nên chỉ để tham khảo hoặc sửa board bằng tay. - **Khóa claim nguyên tử theo từng session (#1673):** nickname chỉ là vai trò (`Core`, `Web`), nên hai session cùng nickname vẫn có thể giẫm lên nhau. Vì vậy `board.mjs` với `--status in-progress` hoặc `in-review` lấy khóa `refs/claims/`: một git ref trong repo của issue, GitHub chỉ tạo khi ref đó chưa tồn tại (HTTP 422 nếu đã có), nên hai session claim cùng lúc thì đúng một bên thắng, bên kia bị từ chối kèm tên session đang giữ. Ref này không chạy workflow nào và không vào bản clone thường. **Không tự tạo, sửa hay xóa ref `refs/claims/*` bằng tay.** - - **Claimant** là id của một session, dạng `:`, được ghi trong khóa và trong comment `board.mjs` tự đăng lên issue. Session Claude Code tự có `claude:` (từ `CLAUDE_CODE_HOST_SESSION_ID`, cũng là id để `SendMessage` tới session đó). Qwen Code và công cụ khác truyền `--claimant qwen:` ở mỗi lệnh. Biến môi trường `RATE_CLAIMANT` chỉ được đặt riêng cho một session, **không bao giờ** trong shell profile hay môi trường mà session khác (kể cả session con) thừa kế; id session Claude Code luôn được ưu tiên hơn biến này. Không có claimant thì `board.mjs` từ chối. + - **Claimant** là id của một session, dạng `:`, được ghi trong khóa và trong comment `board.mjs` tự đăng lên issue. Session Claude Code tự có `claude:` (từ `CLAUDE_CODE_HOST_SESSION_ID`, cũng là id để `SendMessage` tới session đó). Qwen Code truyền `--claimant qwen:`, Antigravity truyền `--claimant antigravity:` (và công cụ khác truyền `--claimant :`) ở mỗi lệnh. Biến môi trường `RATE_CLAIMANT` chỉ được đặt riêng cho một session, **không bao giờ** trong shell profile hay môi trường mà session khác (kể cả session con) thừa kế; id session Claude Code luôn được ưu tiên hơn biến này. Không có claimant thì `board.mjs` từ chối. - Claim lại bởi chính session đang giữ khóa thì không đổi gì. `in-review` giữ khóa. `ready`, `backlog` và `done` nhả khóa; chỉ session đang giữ được nhả, trừ khi issue đã đóng (đã merge) thì ai cũng nhả được. - Nhả khóa không xóa ref (#1675): `board.mjs` thay khóa bằng một commit "tombstone" (`release: ...`, `Released-By: `), cũng bằng compare-and-swap, nên một `--take-over` chen vào giữa lúc đọc và lúc nhả không bị xóa mất (lệnh nhả báo lỗi thay vì ghi đè). Tombstone tính là khóa trống: claim sau đó thay nó, check `task-claim` coi như không có khóa. Mỗi issue từng được claim giữ lại một ref. - - Khóa cũ (#1675): khóa được lấy **trước** lần gần nhất một PR đã merge đóng issue là khóa cũ, vì việc nó giữ đã xong khi PR merge. Khi issue được mở lại, claim (`--status in-progress` hoặc `in-review`) thay khóa cũ mà không cần `--take-over` (nhãn `agent:*` đi kèm khóa cũ cũng vậy; nhãn `po:*` của PO kia vẫn cần `--take-over`), và comment khóa ghi rõ khóa cũ bị thay. Nhả khóa thì không: issue đang mở chỉ session giữ khóa được nhả, hoặc PO dùng `--force-release`. Issue đóng mà không do PR merge (not planned, đóng tay) không làm khóa thành khóa cũ. Không có workflow nào tự gỡ khóa khi issue đóng. + - Khóa cũ (#1675): khóa được lấy **trước** lần gần nhất một PR đã merge đóng issue là khóa cũ, vì việc nó giữ đã xong khi PR merge. Khi issue được mở lại, claim (`--status in-progress` hoặc `in-review`) thay khóa cũ mà không cần `--take-over` (nhãn `agent:*` đi kèm khóa cũ cũng vậy; nhãn `po:*` của PO khác vẫn cần `--take-over`), và comment khóa ghi rõ khóa cũ bị thay. Nhả khóa thì không: issue đang mở chỉ session giữ khóa được nhả, hoặc PO dùng `--force-release`. Issue đóng mà không do PR merge (not planned, đóng tay) không làm khóa thành khóa cũ. Không có workflow nào tự gỡ khóa khi issue đóng. - Session vẫn làm tiếp một issue sau khi issue được mở lại thì chạy lại ngay `board.mjs --status in-progress --agent ` để lấy khóa mới; nếu không, khóa của nó là khóa cũ và session khác claim được. - `--take-over` (chỉ khi có comment bàn giao hoặc JOY giao) chuyển khóa sang session mới, và chỉ thành công nếu khóa không đổi kể từ lúc đọc. - Bàn giao: session cũ chạy `board.mjs --status ready` (nhả khóa, `board.mjs` comment "Claim lock ... released"); session mới chỉ claim sau khi thấy comment đó. @@ -234,13 +234,13 @@ Mỗi finding khi review (subagent fresh-context, bot, hoặc người) phải > > Bài học 2026-10-27: session PO của Rate Core chạy nhiều giờ mà không nạp skill, chỉ biết quy tắc qua brief của chính nó, và hỏi JOY quá nhiều. -JOY có thể giao một session Claude Desktop làm **Product Owner (PO)** cho một line: **Rate Core** (issue của `DOS/Rate`) hoặc **Rate Services** (issue của `DOS/Rate-Services`). PO điều phối backlog, mở session con cho từng issue, trả lời câu hỏi của chúng và chỉ mang quyết định thật sự lên JOY. Toàn bộ quy trình nằm trong skill [`.claude/skills/product-owner/SKILL.md`](./.claude/skills/product-owner/SKILL.md); skill dùng lại giao thức claim ở Mục 1 (`agent:`, `status:*`, khóa `refs/claims/` theo claimant, `board.mjs`, check `task-claim`), không thêm giao thức claim thứ hai. +JOY có thể giao một session (Claude Desktop, Qwen Code Desktop, hoặc Antigravity) làm **Product Owner (PO)** cho một line: **Rate Core** (issue của `DOS/Rate`) hoặc **Rate Services** (issue của `DOS/Rate-Services`). PO điều phối backlog, mở session con cho từng issue, trả lời câu hỏi của chúng và chỉ mang quyết định thật sự lên JOY. Toàn bộ quy trình nằm trong skill [`.claude/skills/product-owner/SKILL.md`](./.claude/skills/product-owner/SKILL.md) (và [`docs/product-owner-config.md`](./docs/product-owner-config.md) cho plugin `cc-po`); skill dùng lại giao thức claim ở Mục 1 (`agent:`, `status:*`, khóa `refs/claims/` theo claimant, `board.mjs`, check `task-claim`), không thêm giao thức claim thứ hai. Quy tắc cho chính các PO: -- **Hai PO trên cùng repo (#1673):** JOY có thể chạy hai PO song song, một trong Claude Code và một trong Qwen Code Desktop. Nhãn `po:claude` và `po:qwen` cho biết PO nào điều phối issue. Mỗi PO chỉ giao issue mang nhãn của mình hoặc chưa có nhãn `po:*`, và gắn nhãn của mình khi giao (`gh issue edit -R DOS/Rate --add-label po:claude`). `board.mjs` tự thêm nhãn line theo claimant khi claim, và từ chối issue mang nhãn của PO kia nếu không có `--take-over` (chỉ khi JOY giao). Claimant của công cụ không chạy PO (ví dụ `codex:...`) không được thêm nhãn `po:*` và không bị chặn theo line; agent đó vẫn phải tôn trọng nhãn `po:*` theo quy tắc này. Session con của PO Qwen dùng claimant `qwen:`, của PO Claude Code dùng `claude:`. -- **Trạng thái PO tách riêng:** mỗi PO có file trạng thái và check-in riêng theo công cụ, `po-state.claude.md` hoặc `po-state.qwen.md`, đặt **ngoài repo** (trong thư mục ghi chú của chính PO đó, ví dụ thư mục notes của session PO), không bao giờ commit, và không bao giờ ghi vào file của PO kia. -- **Chỉ một PO promote:** `promote.mjs` (`dev` -> `main`) và `verify-live.mjs` sau đó chỉ do PO Claude Code chạy. PO Qwen cần promote thì nhắn PO Claude Code (qua JOY nếu không nhắn trực tiếp được), không tự chạy. `promote.mjs` từ chối chạy khi claimant của session không phải `claude:` hoặc không xác định được (#1675). Đây là rào chắn chống nhầm, không phải phân quyền: claimant có thể truyền tay bằng `--claimant`. +- **Nhiều PO song song trên cùng repo (#1673, #1805):** JOY có thể chạy các PO song song trên cùng repo trong Claude Code (`po:claude`), Qwen Code Desktop (`po:qwen`), và Antigravity (`po:antigravity`). Nhãn `po:claude`, `po:qwen`, và `po:antigravity` cho biết PO nào điều phối issue. Mỗi PO chỉ giao issue mang nhãn của mình hoặc chưa có nhãn `po:*`, và gắn nhãn của mình khi giao (ví dụ `gh issue edit -R DOS/Rate --add-label po:claude`). `board.mjs` tự thêm nhãn line theo claimant khi claim, và từ chối issue mang nhãn `po:*` của PO khác nếu không có `--take-over` (chỉ khi JOY giao). Claimant của công cụ không chạy PO (ví dụ `codex:...`) không được thêm nhãn `po:*` và không bị chặn theo line; agent đó vẫn phải tôn trọng nhãn `po:*` theo quy tắc này. Session con của PO Claude Code dùng claimant `claude:`, của PO Qwen dùng `qwen:`, của PO Antigravity dùng `antigravity:`. +- **Trạng thái PO tách riêng:** mỗi PO có file trạng thái và check-in riêng theo công cụ và theo line (`po-state.claude.md`, `po-state.qwen.md`, hoặc file `*-antigravity-po-checkin.md` theo từng line trong `~/.claude/po-state/` cho PO Antigravity: `dos-rate-core-antigravity-po-checkin.md` cho Rate Core, `dos-rate-services-services-antigravity-po-checkin.md` cho Rate Services), đặt **ngoài repo** (trong thư mục ghi chú hoặc `po-state` riêng của chính PO đó), không bao giờ commit, và không bao giờ ghi vào file của PO khác hay line khác. +- **Chỉ một PO promote:** `promote.mjs` (`dev` -> `main`) và `verify-live.mjs` sau đó chỉ do PO Claude Code chạy. PO Qwen hoặc PO Antigravity cần promote thì nhắn PO Claude Code (qua JOY nếu không nhắn trực tiếp được), không tự chạy. `promote.mjs` từ chối chạy khi claimant của session không phải `claude:` hoặc không xác định được (#1675). Đây là rào chắn chống nhầm, không phải phân quyền: claimant có thể truyền tay bằng `--claimant`. Khi một session do PO mở (session con), các quy tắc sau thay đổi so với Mục 0 và Mục 1: diff --git a/docs/product-owner-config.md b/docs/product-owner-config.md index b4bbecae..12ea6e68 100644 --- a/docs/product-owner-config.md +++ b/docs/product-owner-config.md @@ -1,10 +1,10 @@ # Product Owner config: DOS/Rate Values for the cc-po plugin's `product-owner` and `task-claim` skills in -this repository (used by the Qwen Code PO; the Claude Code PO runs Rate's own -`.claude/skills/product-owner` skill). `AGENTS.md` and the documents it -routes to win over the plugin wherever they differ. Check this file with the -plugin's `check-config.mjs`. +this repository (used by the Qwen Code PO and the Antigravity PO; the Claude +Code PO runs Rate's own `.claude/skills/product-owner` skill). `AGENTS.md` +and the documents it routes to win over the plugin wherever they differ. +Check this file with the plugin's `check-config.mjs`. This file lives at `docs/product-owner-config.md` (lower-case `docs`), the one path cc-po reads, although Rate's documentation folder is `Docs/`. On a @@ -20,9 +20,11 @@ This config covers Rate Core (DOS/Rate). Rate Services issues `.claude/skills/product-owner/SKILL.md`: no `dev` branch, no `task-claim` check, PRs into `main`. -The PO's own state file follows `AGENTS.md` section 6, which wins over the -plugin's check-in and handoff file names: `po-state.qwen.md` (or -`po-state.claude.md`) in that PO's own notes folder, outside the repository. +The PO's own state file follows `AGENTS.md` section 6, which keeps each +runtime's state outside the repository: `po-state.claude.md` or +`po-state.qwen.md` in that PO's own notes folder, and +`dos-rate-core-antigravity-po-checkin.md` (under `~/.claude/po-state/`) for +the Antigravity PO. ## Owner and reporting @@ -100,7 +102,7 @@ side as "Triage" in that skill says. says: a fresh-context subagent review of the diff (`AGENTS.md` sections 0.2 and 5), then a Codex review where Codex is available; a PR without one says so -- agent_model: sonnet +- agent_model: sonnet (Antigravity: inherit on Orchestrator (Neon), Orchestrator (Carbon), Gemini 4 Neon, or Gemini 4 Carbon) ## Claim lock and parallel POs @@ -109,9 +111,12 @@ side as "Triage" in that skill says. DOS/Rate checkout. Never set `RATE_CLAIMANT` in a shell profile or an environment other sessions (sub-sessions included) inherit. - pr_issue_keyword: Closes -- po_labels: claude: po:claude, qwen: po:qwen +- po_labels: claude: po:claude, qwen: po:qwen, antigravity: po:antigravity - promotion_po: claude ## Sessions - gatekeeper_model: haiku +- gatekeeper_heartbeat_active_minutes: 180 +- gatekeeper_heartbeat_idle_minutes: 720 +- po_context_compact_tokens: 120000 diff --git a/scripts/claim-lock.mjs b/scripts/claim-lock.mjs index 624a009c..3ce9b2a1 100644 --- a/scripts/claim-lock.mjs +++ b/scripts/claim-lock.mjs @@ -31,13 +31,14 @@ export const lockRef = (number) => `${LOCK_REF_PREFIX}${number}`; /** * A claimant names one session: `:`, for example - * `claude:local_ad470353-...` or `qwen:`. + * `claude:local_ad470353-...`, `qwen:` or + * `antigravity:`. */ export const CLAIMANT_RE = /^[a-z][a-z0-9-]{1,19}:[A-Za-z0-9][A-Za-z0-9._-]{3,99}$/; /** The tools that run a Product Owner, each with its `po:` label. */ -export const PO_TOOLS = ['claude', 'qwen']; +export const PO_TOOLS = ['claude', 'qwen', 'antigravity']; export const poLabel = (tool) => `po:${tool}`; export function isClaimant(value) { @@ -56,8 +57,8 @@ export function poTool(claimant) { * to message this one), else the Claude Code session id, else * `RATE_CLAIMANT`. The Claude ids win over `RATE_CLAIMANT` so that a value * left in a shell profile can never make every Claude session share one - * claimant. Qwen Code and other tools pass `--claimant`; `RATE_CLAIMANT` - * must be set per session, never in a shared profile. + * claimant. Qwen Code, Antigravity and other tools pass `--claimant`; + * `RATE_CLAIMANT` must be set per session, never in a shared profile. */ export function resolveClaimant({ flag, env = {} } = {}) { const sources = [ @@ -79,7 +80,7 @@ export function resolveClaimant({ flag, env = {} } = {}) { throw new Error( 'no claimant: pass --claimant : ' + '(Claude Code sessions get claude: automatically; ' + - 'Qwen Code sessions use qwen:)' + 'Qwen Code and Antigravity sessions pass qwen: or antigravity:)' ); } if (!isClaimant(value)) { diff --git a/scripts/ops/board.mjs b/scripts/ops/board.mjs index d081db23..c0fce294 100644 --- a/scripts/ops/board.mjs +++ b/scripts/ops/board.mjs @@ -23,9 +23,10 @@ // // The status label replaces any other status:* label, the agent label // (agent:) is created when missing, a claim adds the PO line label -// (po:claude or po:qwen) of the claimant's tool, and an issue not yet on the -// board is added to it. An issue another agent, session or PO line holds is -// refused unless --take-over is passed (a hand-over or JOY's assignment). +// (po:claude, po:qwen or po:antigravity) of the claimant's tool, and an issue +// not yet on the board is added to it. An issue another agent, session or PO +// line holds is refused unless --take-over is passed (a hand-over or JOY's +// assignment). import { parseArgs } from 'node:util'; import { lockRef, poLabel, poTool, resolveClaimant } from '../claim-lock.mjs'; @@ -66,11 +67,11 @@ export const isPoNick = (nick) => nick.trim().toLowerCase() === 'po'; /** * Label edits that leave exactly one status label and one agent label, plus, * when `po` names the claimant's PO line, that line's `po:*` label. - * An issue claimed by another agent or dispatched by the other PO is never + * An issue claimed by another agent or dispatched by another PO is never * taken silently (AGENTS.md sections 1 and 6): that needs `takeOver`, and the * other claim label is then removed. `staleClaim` (a claim, and a merged PR * closed the issue after its lock was taken) waives that for the agent - * label, which is as stale as the lock, but not for the other PO's line. + * label, which is as stale as the lock, but not for another PO's line. */ export function labelChanges( current, diff --git a/scripts/ops/board.test.mjs b/scripts/ops/board.test.mjs index 2aea06bb..080d6772 100644 --- a/scripts/ops/board.test.mjs +++ b/scripts/ops/board.test.mjs @@ -2,10 +2,14 @@ import assert from 'node:assert/strict'; import test from 'node:test'; import { + PO_TOOLS, declaredClaimants, formatLockMessage, formatReleaseMessage, + isClaimant, parseLockMessage, + poLabel, + poTool, resolveClaimant, } from '../claim-lock.mjs'; import { @@ -107,6 +111,7 @@ test('every status has a Project #9 option id', () => { const A = 'claude:local_aaaa1111'; const B = 'qwen:bbbb2222'; +const C = 'antigravity:d0dc3655-856b-4c89-b2bd-3283a4e5e3da'; /** * An in-memory refs/claims store with GitHub's semantics: creating an @@ -356,43 +361,73 @@ test('force-release is for the PO and needs a reason', async () => { assert.equal(isPoNick('Ops'), false); }); +test('poTool and poLabel cover claude, qwen, and antigravity (DOS/Rate#1805)', () => { + assert.deepEqual(PO_TOOLS, ['claude', 'qwen', 'antigravity']); + assert.equal(isClaimant(C), true); + assert.equal(poTool(A), 'claude'); + assert.equal(poTool(B), 'qwen'); + assert.equal(poTool(C), 'antigravity'); + assert.equal(poTool('codex:1234abcd'), null); + assert.equal(poLabel('claude'), 'po:claude'); + assert.equal(poLabel('qwen'), 'po:qwen'); + assert.equal(poLabel('antigravity'), 'po:antigravity'); + assert.equal(resolveClaimant({ flag: C, env: {} }), C); +}); + test('a claim adds the PO line label of the claimant', () => { - assert.deepEqual( - labelChanges(['status:ready'], 'in-progress', 'Ops', { po: 'claude' }), - { - add: ['status:in-progress', 'agent:ops', 'po:claude'], - remove: ['status:ready'], - } - ); - assert.deepEqual( - labelChanges( - ['status:in-progress', 'agent:ops', 'po:claude'], - 'in-review', - 'Ops', + for (const tool of PO_TOOLS) { + assert.deepEqual( + labelChanges(['status:ready'], 'in-progress', 'Ops', { po: tool }), { - po: 'claude', + add: ['status:in-progress', 'agent:ops', `po:${tool}`], + remove: ['status:ready'], } - ), - { add: ['status:in-review'], remove: ['status:in-progress'] } - ); + ); + assert.deepEqual( + labelChanges( + ['status:in-progress', 'agent:ops', `po:${tool}`], + 'in-review', + 'Ops', + { + po: tool, + } + ), + { add: ['status:in-review'], remove: ['status:in-progress'] } + ); + } }); -test("never claims an issue of the other PO's line without --take-over", () => { - assert.throws( - () => - labelChanges(['status:ready', 'po:qwen'], 'in-progress', 'Ops', { - po: 'claude', - }), - /dispatched by po:qwen, another PO's line/ - ); +test("never claims an issue of another PO's line without --take-over (3-way mutual exclusion)", () => { + for (const from of PO_TOOLS) { + for (const to of PO_TOOLS) { + if (from === to) continue; + assert.throws( + () => + labelChanges(['status:ready', `po:${from}`], 'in-progress', 'Ops', { + po: to, + }), + new RegExp(`dispatched by po:${from}, another PO's line`) + ); + assert.deepEqual( + labelChanges(['status:ready', `po:${from}`], 'in-progress', 'Ops', { + po: to, + takeOver: true, + }), + { + add: ['status:in-progress', 'agent:ops', `po:${to}`], + remove: ['status:ready', `po:${from}`], + } + ); + } + } + // A tool without a PO line adds no po:* label and is not refused by line. assert.deepEqual( - labelChanges(['status:ready', 'po:qwen'], 'in-progress', 'Ops', { - po: 'claude', - takeOver: true, + labelChanges(['status:ready', 'po:antigravity'], 'in-progress', 'Ops', { + po: poTool('codex:1234abcd'), }), { - add: ['status:in-progress', 'agent:ops', 'po:claude'], - remove: ['status:ready', 'po:qwen'], + add: ['status:in-progress', 'agent:ops'], + remove: ['status:ready'], } ); }); @@ -1116,30 +1151,35 @@ test('setBoard: another session cannot release a stale lock of a reopened issue' assert.equal(await holder(api, 7), B); }); -test('stale: the agent label of a stale claim needs no --take-over, the other PO line still does', () => { +test('stale: the agent label of a stale claim needs no --take-over, another PO line still does', () => { assert.deepEqual( labelChanges(['status:in-review', 'agent:web'], 'in-progress', 'Ops', { staleClaim: true, - po: 'claude', + po: 'antigravity', }), { - add: ['status:in-progress', 'agent:ops', 'po:claude'], + add: ['status:in-progress', 'agent:ops', 'po:antigravity'], remove: ['status:in-review', 'agent:web'], } ); - assert.throws( - () => - labelChanges( - ['status:in-review', 'agent:web', 'po:qwen'], - 'in-progress', - 'Ops', - { - staleClaim: true, - po: 'claude', - } - ), - /another PO's line/ - ); + for (const from of PO_TOOLS) { + for (const to of PO_TOOLS) { + if (from === to) continue; + assert.throws( + () => + labelChanges( + ['status:in-review', 'agent:web', `po:${from}`], + 'in-progress', + 'Ops', + { + staleClaim: true, + po: to, + } + ), + /another PO's line/ + ); + } + } }); test('the lock comment names a stale lock it replaced', () => { @@ -1276,3 +1316,81 @@ test('github lock api: a release is a compare-and-swap from the lock read to its assert.ok(!fake.calls.some((c) => c.includes('DELETE'))); assert.equal((await api.read(7)).released, true); }); + +test('setBoard: enforces 3-way mutual exclusion across po:claude, po:qwen, and po:antigravity (DOS/Rate#1805)', async () => { + const claimants = { claude: A, qwen: B, antigravity: C }; + + // Claiming an unlabelled issue with antigravity stamps po:antigravity. + { + const edits = []; + const base = fakeIo(['status:ready']); + const io = { + ...base.io, + gh(args) { + if (args[0] === 'issue' && args[1] === 'edit') edits.push(args); + return base.io.gh(args); + }, + }; + const api = loggingLocks(base.log); + const out = await setBoard({ + number: 7, + status: 'in-progress', + nick: 'Ops', + claimant: C, + api, + io, + }); + assert.match(out, /antigravity:d0dc3655.*lock taken/); + assert.equal(await holder(api, 7), C); + assert.ok(edits[0].includes('po:antigravity')); + } + + // Every cross-PO pair is refused before any lock, comment, label or board edit. + for (const from of PO_TOOLS) { + for (const to of PO_TOOLS) { + if (from === to) continue; + const { io, log } = fakeIo(['status:ready', `po:${from}`]); + const api = loggingLocks(log); + await assert.rejects( + setBoard({ + number: 7, + status: 'in-progress', + nick: 'Ops', + claimant: claimants[to], + api, + io, + }), + new RegExp(`dispatched by po:${from}, another PO's line`) + ); + assert.deepEqual(log, []); + assert.equal(await holder(api, 7), null); + } + } + + // With --take-over, antigravity replaces po:claude cleanly. + { + const edits = []; + const base = fakeIo(['status:ready', 'po:claude']); + const io = { + ...base.io, + gh(args) { + if (args[0] === 'issue' && args[1] === 'edit') edits.push(args); + return base.io.gh(args); + }, + }; + const api = loggingLocks(base.log); + await setBoard({ + number: 7, + status: 'in-progress', + nick: 'Ops', + claimant: C, + takeOver: true, + api, + io, + }); + assert.equal(await holder(api, 7), C); + const edit = edits[0].join(' '); + assert.match(edit, /--add-label po:antigravity/); + assert.match(edit, /--remove-label po:claude/); + } +}); diff --git a/scripts/ops/promote.test.mjs b/scripts/ops/promote.test.mjs index 160717d4..57fa7028 100644 --- a/scripts/ops/promote.test.mjs +++ b/scripts/ops/promote.test.mjs @@ -2,6 +2,7 @@ import assert from 'node:assert/strict'; import test from 'node:test'; import { + PROMOTER_TOOL, ownPromotionPr, promoterClaimant, promotionBody, @@ -64,7 +65,8 @@ test('a fork PR from its own dev is never taken as the promotion', () => { assert.equal(ownPromotionPr([]), null); }); -test('only a Claude Code session may promote (DOS/Rate#1675)', () => { +test('only a Claude Code session may promote (DOS/Rate#1675, DOS/Rate#1805)', () => { + assert.equal(PROMOTER_TOOL, 'claude'); assert.equal( promoterClaimant({ env: { CLAUDE_CODE_HOST_SESSION_ID: 'local_abcd1234' }, @@ -83,6 +85,23 @@ test('only a Claude Code session may promote (DOS/Rate#1675)', () => { () => promoterClaimant({ env: { RATE_CLAIMANT: 'qwen:abcd1234' } }), /only the Claude Code PO promotes/ ); + assert.throws( + () => + promoterClaimant({ + flag: 'antigravity:d0dc3655-856b-4c89-b2bd-3283a4e5e3da', + env: {}, + }), + /only the Claude Code PO promotes.*antigravity: one/ + ); + assert.throws( + () => + promoterClaimant({ + env: { + RATE_CLAIMANT: 'antigravity:d0dc3655-856b-4c89-b2bd-3283a4e5e3da', + }, + }), + /only the Claude Code PO promotes.*antigravity: one/ + ); // No claimant at all: refused, not assumed to be Claude. assert.throws(() => promoterClaimant({ env: {} }), /no claimant/); }); diff --git a/scripts/task-claim-check.test.mjs b/scripts/task-claim-check.test.mjs index 87f0d095..ad75a401 100644 --- a/scripts/task-claim-check.test.mjs +++ b/scripts/task-claim-check.test.mjs @@ -152,6 +152,16 @@ test('passes an issue locked by the claimant the PR declares', () => { assert.equal(r.ok, true); assert.equal(r.claimant, ME); assert.match(r.reason, /locked by the declared claimant/); + + const agy = 'antigravity:d0dc3655-856b-4c89-b2bd-3283a4e5e3da'; + const agyRes = evaluateClaim( + pr({ + body: `Closes #1805\n\nClaimant: ${agy}\n`, + closingIssues: [issue(claimed, 1805, { claimant: agy, agent: 'Ops' })], + }) + ); + assert.equal(agyRes.ok, true); + assert.equal(agyRes.claimant, agy); }); test('accepts the declared claimant in backticks', () => { From 8ebadcb77d3400fe1d06c3c85a099481839405fa Mon Sep 17 00:00:00 2001 From: JOY <5027251+JOY@users.noreply.github.com> Date: Sat, 21 Nov 2026 23:43:34 +0700 Subject: [PATCH 03/24] fix(extension): clear stale SPA state on exit and validate message senders (#1815) * fix(extension): clear stale spa state and validate message senders (#1809) * fix(extension): harden exact-origin sender checks and spa debounce races (#1809) --- .../src/background/service-worker.ts | 393 +++++- apps/extension/src/content/content-script.ts | 117 +- apps/extension/src/test-extension-pipeline.ts | 1077 +++++++++++++++++ 3 files changed, 1569 insertions(+), 18 deletions(-) diff --git a/apps/extension/src/background/service-worker.ts b/apps/extension/src/background/service-worker.ts index 5e9505ad..bed9ff0a 100644 --- a/apps/extension/src/background/service-worker.ts +++ b/apps/extension/src/background/service-worker.ts @@ -1,8 +1,13 @@ import { RATE_API_V1_ENDPOINTS } from '@repo/shared-data'; +import { + EXTENSION_MARKETPLACES, + isExtensionMarketplaceHost, +} from '@repo/shared-data/rules'; import type { ExtensionLookupData, ExtensionSettings, ExtractedPageProductSignal, + SupportedEcommercePlatform, } from '../types.js'; import { detectEcommercePlatform, @@ -205,17 +210,218 @@ function normalizeQuickQueryBareMarketplaceUrl( return sanitizeProductUrl(`https://${hostAndPath}`); } -// The latest product lookup per tab (DOS/Rate#1300, #1664). +// The latest product lookup per tab (DOS/Rate#1300, #1664, #1809). let detectRequestCounter = 0; const latestDetectByTab = new Map(); let fullLookupRequestCounter = 0; const latestFullLookupByTab = new Map(); let latestSelectionCounter = 0; +let latestPageSelectionCounter = 0; let pendingQuickQueryReceipt: { updatedAt: number; apiBaseUrl: string; data: ExtensionLookupData | null; } | null = null; + +interface VerifiedMarketplaceUrl { + marketplace: SupportedEcommercePlatform; + origin: string; +} + +function resolvePositiveIntId(value: unknown): number | undefined { + return typeof value === 'number' && Number.isInteger(value) && value > 0 + ? value + : undefined; +} + +function resolveMarketplaceFromHttpsUrl( + rawUrl: unknown +): VerifiedMarketplaceUrl | null { + if (typeof rawUrl !== 'string') return null; + const trimmed = rawUrl.trim(); + if (!trimmed || trimmed.length > 8192 || /[\s\p{Cc}]/u.test(trimmed)) { + return null; + } + try { + const parsed = new URL(trimmed); + if ( + parsed.protocol !== 'https:' || + parsed.username || + parsed.password || + parsed.port !== '' + ) { + return null; + } + const host = parsed.hostname.toLowerCase(); + if ( + !host || + host.startsWith('.') || + host.endsWith('.') || + host.includes('..') + ) { + return null; + } + for (const marketplace of EXTENSION_MARKETPLACES) { + if (isExtensionMarketplaceHost(marketplace, host)) { + return { marketplace, origin: parsed.origin.toLowerCase() }; + } + } + return null; + } catch { + return null; + } +} + +interface ContentScriptMessageSender { + id?: string; + frameId?: number; + documentLifecycle?: string; + origin?: string; + tab?: unknown; + url?: string; +} + +function validateContentScriptSender( + sender: ContentScriptMessageSender | null | undefined, + options?: { requireSignal?: boolean; rawSignal?: unknown } +): { ok: true } | { ok: false; error: string } { + if (!sender || typeof sender !== 'object' || Array.isArray(sender)) { + return { ok: false, error: 'Invalid message sender' }; + } + const runtimeId = + typeof chrome !== 'undefined' && + typeof chrome.runtime?.id === 'string' && + chrome.runtime.id.length > 0 + ? chrome.runtime.id + : undefined; + if ( + runtimeId && + typeof sender.id === 'string' && + sender.id.length > 0 && + sender.id !== runtimeId + ) { + return { ok: false, error: 'Untrusted extension sender' }; + } + if (typeof sender.frameId === 'number' && sender.frameId !== 0) { + return { + ok: false, + error: 'Subframe content-script messages are not allowed', + }; + } + if ( + typeof sender.documentLifecycle === 'string' && + sender.documentLifecycle !== 'active' + ) { + return { + ok: false, + error: 'Inactive document lifecycle messages are not allowed', + }; + } + if ( + !sender.tab || + typeof sender.tab !== 'object' || + Array.isArray(sender.tab) + ) { + return { + ok: false, + error: 'Content-script messages must originate from a browser tab', + }; + } + const senderTab = sender.tab as { + id?: unknown; + windowId?: unknown; + url?: unknown; + }; + if ( + resolvePositiveIntId(senderTab.id) === undefined && + resolvePositiveIntId(senderTab.windowId) === undefined + ) { + return { + ok: false, + error: 'Content-script sender tab lacks a valid positive identifier', + }; + } + let senderIdentity: VerifiedMarketplaceUrl | null = null; + if (sender.url !== undefined && sender.url !== null) { + senderIdentity = resolveMarketplaceFromHttpsUrl(sender.url); + if (!senderIdentity) { + return { + ok: false, + error: 'Sender URL is not an allowed marketplace origin', + }; + } + } + if (sender.origin !== undefined && sender.origin !== null) { + const originIdentity = resolveMarketplaceFromHttpsUrl(sender.origin); + if ( + !originIdentity || + (senderIdentity !== null && + (originIdentity.marketplace !== senderIdentity.marketplace || + originIdentity.origin !== senderIdentity.origin)) + ) { + return { + ok: false, + error: 'Sender origin is not an allowed marketplace origin', + }; + } + senderIdentity ??= originIdentity; + } + if ( + senderIdentity === null && + senderTab.url !== undefined && + senderTab.url !== null + ) { + senderIdentity = resolveMarketplaceFromHttpsUrl(senderTab.url); + if (!senderIdentity) { + return { + ok: false, + error: 'Sender tab URL is not an allowed marketplace origin', + }; + } + } + if (options?.requireSignal) { + const rawSignal = options.rawSignal; + if ( + !rawSignal || + typeof rawSignal !== 'object' || + Array.isArray(rawSignal) + ) { + return { ok: false, error: 'Invalid product signal' }; + } + const candidate = rawSignal as { + platform?: unknown; + canonicalUrl?: unknown; + }; + const declaredPlatform = + typeof candidate.platform === 'string' + ? candidate.platform.trim().toLowerCase() + : ''; + const canonicalIdentity = resolveMarketplaceFromHttpsUrl( + candidate.canonicalUrl + ); + if ( + !canonicalIdentity || + declaredPlatform !== canonicalIdentity.marketplace + ) { + return { + ok: false, + error: + 'Signal platform and canonicalUrl must match an allowed marketplace', + }; + } + if ( + senderIdentity !== null && + (declaredPlatform !== senderIdentity.marketplace || + canonicalIdentity.origin !== senderIdentity.origin) + ) { + return { + ok: false, + error: 'Sender URL origin or platform does not match product signal', + }; + } + } + return { ok: true }; +} async function maybeSendOptInCrowdSignal( rawSignal: ExtractedPageProductSignal ): Promise { @@ -358,25 +564,181 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { // CRITICAL MV3 Rule: Call chrome.sidePanel.open synchronously in the first turn // before any await so the user gesture from clicking the Shadow DOM Pill is preserved! if (msgType === 'RATE_OPEN_SIDE_PANEL') { - const tabId = sender.tab?.id; - const windowId = sender.tab?.windowId; - if (tabId) { - void chrome.sidePanel.open({ tabId }); - } else if (windowId) { - void chrome.sidePanel.open({ windowId }); + const senderCheck = validateContentScriptSender(sender); + if (!senderCheck.ok) { + sendResponse({ ok: false, error: senderCheck.error }); + return false; + } + const tabId = resolvePositiveIntId(sender.tab?.id); + const windowId = resolvePositiveIntId(sender.tab?.windowId); + try { + if (tabId) { + void Promise.resolve(chrome.sidePanel.open({ tabId })).catch(() => { + // Ignore rejections when user gesture expired or tab closed + }); + } else if (windowId) { + void Promise.resolve(chrome.sidePanel.open({ windowId })).catch(() => { + // Ignore rejections when user gesture expired or window closed + }); + } + } catch { + // Ignore synchronous errors if sidePanel API is unavailable } sendResponse({ ok: true }); return false; } + if (msgType === 'RATE_PRODUCT_CLEARED') { + const senderCheck = validateContentScriptSender(sender); + if (!senderCheck.ok) { + sendResponse({ ok: false, error: senderCheck.error }); + return false; + } + const clearId = ++detectRequestCounter; + const tabId = resolvePositiveIntId(sender.tab?.id); + const isBackgroundTab = Boolean(sender.tab && sender.tab.active === false); + const clearPageSeq = !isBackgroundTab ? ++latestPageSelectionCounter : null; + const selectionAtClearEntry = latestSelectionCounter; + if (tabId) latestDetectByTab.set(tabId, clearId); + const isLatestClear = () => + tabId + ? latestDetectByTab.get(tabId) === clearId + : clearPageSeq !== null && + latestPageSelectionCounter === clearPageSeq && + latestSelectionCounter === selectionAtClearEntry; + (async () => { + try { + await updateActionBadge(tabId, null, isLatestClear); + if (!isLatestClear()) { + sendResponse({ ok: true, stale: true }); + return; + } + const readKeys = tabId + ? [`tab_${tabId}`, 'tab_latest', 'lookup_tab_latest'] + : ['tab_latest', 'lookup_tab_latest']; + const sessionData = await chrome.storage.session.get(readKeys); + if (!isLatestClear()) { + sendResponse({ ok: true, stale: true }); + return; + } + const tabEntry = tabId + ? (sessionData[`tab_${tabId}`] as + | { + signal?: ExtractedPageProductSignal; + updatedAt?: number; + source?: 'page' | 'quick_query'; + } + | undefined) + : undefined; + const latestEntry = sessionData.tab_latest as + | { + signal?: ExtractedPageProductSignal; + updatedAt?: number; + source?: 'page' | 'quick_query'; + } + | undefined; + const hasNewerQuickQuery = + latestEntry?.source === 'quick_query' && + Boolean(latestEntry.signal) && + (typeof latestEntry.updatedAt !== 'number' || + typeof tabEntry?.updatedAt !== 'number' || + latestEntry.updatedAt >= tabEntry.updatedAt); + const canClearLatestNow = () => + !hasNewerQuickQuery && + latestSelectionCounter === selectionAtClearEntry && + (clearPageSeq !== null + ? latestPageSelectionCounter === clearPageSeq + : Boolean( + tabEntry?.signal && + latestEntry?.signal && + tabEntry.updatedAt !== undefined && + tabEntry.updatedAt === latestEntry.updatedAt && + tabEntry.signal.canonicalUrl === latestEntry.signal.canonicalUrl + )); + + const ownsCurrentLatest = canClearLatestNow(); + if (ownsCurrentLatest) { + pendingQuickQueryReceipt = null; + } + const sessionStorage = chrome.storage + .session as typeof chrome.storage.session & { + remove?: (keys: string | string[]) => Promise; + }; + if (typeof sessionStorage.remove === 'function') { + if ( + ownsCurrentLatest && + sessionData.lookup_tab_latest === undefined + ) { + await sessionStorage.set({ lookup_tab_latest: null }); + if (!isLatestClear()) { + sendResponse({ ok: true, stale: true }); + return; + } + } + const finalKeysToClear: string[] = []; + if (tabId) { + finalKeysToClear.push(`tab_${tabId}`, `lookup_tab_${tabId}`); + } + if (canClearLatestNow()) { + pendingQuickQueryReceipt = null; + finalKeysToClear.push('tab_latest', 'lookup_tab_latest'); + } + if (finalKeysToClear.length > 0) { + await sessionStorage.remove(finalKeysToClear); + } + } else { + if (ownsCurrentLatest && sessionData.lookup_tab_latest === null) { + await sessionStorage.set({ lookup_tab_latest: false }); + if (!isLatestClear()) { + sendResponse({ ok: true, stale: true }); + return; + } + } + const keysToNull: string[] = []; + if (tabId) { + keysToNull.push(`tab_${tabId}`, `lookup_tab_${tabId}`); + } + if (canClearLatestNow()) { + pendingQuickQueryReceipt = null; + keysToNull.push('tab_latest', 'lookup_tab_latest'); + } + if (keysToNull.length > 0) { + await sessionStorage.set( + Object.fromEntries(keysToNull.map((k) => [k, null])) + ); + } + } + sendResponse({ ok: true }); + } catch (err) { + sendResponse({ + ok: false, + error: + err instanceof Error ? err.message : 'Product state clear failed', + }); + } + })(); + return true; + } + if (msgType === 'RATE_PRODUCT_DETECTED') { + const senderCheck = validateContentScriptSender(sender, { + requireSignal: true, + rawSignal: (message as { signal?: unknown }).signal, + }); + if (!senderCheck.ok) { + sendResponse({ ok: false, error: senderCheck.error }); + return false; + } const requestId = ++detectRequestCounter; - const tabId = sender.tab?.id; + const tabId = resolvePositiveIntId(sender.tab?.id); const isBackgroundTab = Boolean(sender.tab && sender.tab.active === false); const latestOwnerSeq = !isBackgroundTab ? ++latestSelectionCounter : null; + const pageOwnerSeq = !isBackgroundTab ? ++latestPageSelectionCounter : null; if (tabId) latestDetectByTab.set(tabId, requestId); const ownsTabLatest = () => - latestOwnerSeq !== null && latestSelectionCounter === latestOwnerSeq; + latestOwnerSeq !== null && + latestSelectionCounter === latestOwnerSeq && + latestPageSelectionCounter === pageOwnerSeq; // Only the tab's latest lookup may set its badge, stored lookup or // panel: an earlier one can answer after a later one (DOS/Rate#1300). const isLatest = () => @@ -497,6 +859,8 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { } const fullSeq = ++fullLookupRequestCounter; const detectSnapshotAtEntry = new Map(latestDetectByTab); + const selectionAtEntry = latestSelectionCounter; + const pageSelectionAtEntry = latestPageSelectionCounter; (async () => { try { const cleanedVariant = @@ -569,6 +933,11 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { trackedTabId !== undefined && latestDetectByTab.get(trackedTabId) === detectSeqAtEntry && latestFullLookupByTab.get(trackedTabId) === fullSeq; + const isCurrentFallbackLookup = () => + fullSeq === fullLookupRequestCounter && + latestSelectionCounter === selectionAtEntry && + (isQuickQuerySelected || + latestPageSelectionCounter === pageSelectionAtEntry); const effectiveSignal: ExtractedPageProductSignal = overrideVariant ? { ...baseSignal, selectedVariant: overrideVariant } @@ -591,7 +960,7 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { }, }); } - } else if (fullSeq === fullLookupRequestCounter) { + } else if (isCurrentFallbackLookup()) { await chrome.storage.session.set({ tab_latest: { signal: effectiveSignal, @@ -609,7 +978,7 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { latestEntry?.updatedAt === pendingQuickQueryReceipt.updatedAt && settings.apiBaseUrl === pendingQuickQueryReceipt.apiBaseUrl ) { - if (fullSeq !== fullLookupRequestCounter) { + if (!isCurrentFallbackLookup()) { sendResponse({ ok: true, settings, @@ -646,7 +1015,7 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { } const stale = isActiveTabPageSignal ? !isCurrentTabLookup() - : fullSeq !== fullLookupRequestCounter; + : !isCurrentFallbackLookup(); sendResponse( outcome.ok ? { diff --git a/apps/extension/src/content/content-script.ts b/apps/extension/src/content/content-script.ts index e0e033b8..1612f6de 100644 --- a/apps/extension/src/content/content-script.ts +++ b/apps/extension/src/content/content-script.ts @@ -1,4 +1,7 @@ -import { extractProductSignalFromDom } from '../extractors/ecommerce-detector.js'; +import { + extractProductSignalFromDom, + sanitizeProductUrl, +} from '../extractors/ecommerce-detector.js'; import { buildProductSignalSignature, formatSafeExtensionVnd, @@ -40,13 +43,27 @@ function formatCurrencyVnd(amount: number | null | undefined): string { * Render an isolated Shadow DOM Floating Trust Pill on supported e-commerce pages. * Zero host CSS pollution; clicking opens the Chrome Native Side Panel. */ -function renderShadowTrustPill(lookup: ExtensionLookupData): void { +function renderShadowTrustPill( + lookup: ExtensionLookupData, + expectedGeneration = detectGeneration, + expectedCanonicalUrl?: string +): void { if (!shouldRenderTrustPill(lookup)) { removeTrustPillHost(); return; } requestAnimationFrame(() => { + if (expectedGeneration !== detectGeneration) { + return; + } + if ( + expectedCanonicalUrl && + sanitizeProductUrl(window.location.href) !== expectedCanonicalUrl + ) { + void inspectCurrentPage(); + return; + } let host = document.getElementById(HOST_ID); if (!host) { host = document.createElement('div'); @@ -254,7 +271,7 @@ function renderShadowTrustPill(lookup: ExtensionLookupData): void { const trigger = shadow.getElementById('rate-pill-trigger'); if (trigger) { const openSidePanel = () => { - void chrome.runtime.sendMessage({ type: 'RATE_OPEN_SIDE_PANEL' }); + sendRuntimeMessageFireAndForget({ type: 'RATE_OPEN_SIDE_PANEL' }); }; trigger.addEventListener('click', openSidePanel); trigger.addEventListener('keydown', (event: KeyboardEvent) => { @@ -267,27 +284,79 @@ function renderShadowTrustPill(lookup: ExtensionLookupData): void { }); } +function sendRuntimeMessageFireAndForget( + payload: Record +): void { + try { + void Promise.resolve(chrome.runtime.sendMessage(payload)).catch(() => { + // Ignore messaging rejections during page unload or worker restart + }); + } catch { + // Ignore synchronous messaging errors if extension context was invalidated + } +} + // Lookups sent from this page, so a late answer for an earlier product is -// ignored (DOS/Rate#1300). +// ignored (DOS/Rate#1300, #1809). let detectGeneration = 0; let lastProcessedSignature = ''; +let hasDetectedProduct = false; +let prerenderListenerAttached = false; // After a rate limit the same product is looked up again once the wait is over // (DOS/Rate#1466). const rateLimitRetry = new RateLimitRetry(); +function deferWhilePrerendering(): boolean { + const prerenderingDoc = document as Document & { + prerendering?: boolean; + addEventListener?: ( + type: string, + listener: () => void, + options?: { once?: boolean } + ) => void; + }; + if (prerenderingDoc.prerendering !== true) { + return false; + } + if ( + !prerenderListenerAttached && + typeof prerenderingDoc.addEventListener === 'function' + ) { + prerenderListenerAttached = true; + prerenderingDoc.addEventListener( + 'prerenderingchange', + () => { + prerenderListenerAttached = false; + void inspectCurrentPage(); + }, + { once: true } + ); + } + return true; +} + async function inspectCurrentPage(): Promise { + if (deferWhilePrerendering()) return; requestAnimationFrame(async () => { + if (deferWhilePrerendering()) return; const signal = extractProductSignalFromDom(document, window.location.href); if (!signal) { + ++detectGeneration; + const shouldNotifyCleared = hasDetectedProduct; + hasDetectedProduct = false; lastProcessedSignature = ''; rateLimitRetry.track(''); removeTrustPillHost(); + if (shouldNotifyCleared) { + sendRuntimeMessageFireAndForget({ type: 'RATE_PRODUCT_CLEARED' }); + } return; } const signature = buildProductSignalSignature(signal); if (signature === lastProcessedSignature) return; lastProcessedSignature = signature; + hasDetectedProduct = true; rateLimitRetry.track(signature); // Only the latest page's answer may draw or clear the pill: on a // single-page shop an earlier lookup can answer after a later one. @@ -306,12 +375,22 @@ async function inspectCurrentPage(): Promise { }; if (generation !== detectGeneration || response?.stale === true) return; + if (sanitizeProductUrl(window.location.href) !== signal.canonicalUrl) { + void inspectCurrentPage(); + return; + } + if (response?.ok === false && response.status === undefined) { + lastProcessedSignature = ''; + hasDetectedProduct = false; + removeTrustPillHost(); + return; + } rateLimitRetry.after(signature, response, () => { lastProcessedSignature = ''; void inspectCurrentPage(); }); if (response?.ok && response.data) { - renderShadowTrustPill(response.data); + renderShadowTrustPill(response.data, generation, signal.canonicalUrl); } else { // Nothing matched or the lookup failed: never leave the previous // product's pill on a single-page shop (DOS/Rate#1300). @@ -325,13 +404,39 @@ async function inspectCurrentPage(): Promise { }); } +let debounceTimer: ReturnType | null = null; +function scheduleImmediateInspection(): void { + if (debounceTimer) { + clearTimeout(debounceTimer); + debounceTimer = null; + } + void inspectCurrentPage(); +} + // Initial inspection + lightweight observation for SPA route/variant selection changes void inspectCurrentPage(); -let debounceTimer: ReturnType | null = null; +if (typeof window !== 'undefined') { + if (typeof window.addEventListener === 'function') { + window.addEventListener('popstate', scheduleImmediateInspection); + window.addEventListener('hashchange', scheduleImmediateInspection); + } + const nav = ( + window as Window & { + navigation?: { + addEventListener?: (type: string, listener: () => void) => void; + }; + } + ).navigation; + if (nav && typeof nav.addEventListener === 'function') { + nav.addEventListener('currententrychange', scheduleImmediateInspection); + } +} + const observer = new MutationObserver(() => { if (debounceTimer) clearTimeout(debounceTimer); debounceTimer = setTimeout(() => { + debounceTimer = null; void inspectCurrentPage(); }, 700); }); diff --git a/apps/extension/src/test-extension-pipeline.ts b/apps/extension/src/test-extension-pipeline.ts index 813f0202..f81b8a1e 100644 --- a/apps/extension/src/test-extension-pipeline.ts +++ b/apps/extension/src/test-extension-pipeline.ts @@ -8624,6 +8624,1083 @@ async function runTests() { } } } + + // DOS/Rate#1809: clear stale pill, badge, and session lookup state on SPA exit, + // catch chrome.sidePanel.open rejections, and validate sender.url on content-script messages. + { + const g = globalThis as Record; + const saved = [ + 'chrome', + 'document', + 'window', + 'fetch', + 'MutationObserver', + 'requestAnimationFrame', + 'setTimeout', + 'clearTimeout', + ].map((k) => [k, Object.getOwnPropertyDescriptor(g, k)] as const); + try { + // 1. Content-script SPA exit: in-flight PDP lookup discarded, pill removed, and RATE_PRODUCT_CLEARED emitted once. + let pillHost: { shadowRoot?: { innerHTML: string } } | null = null; + let shadowHtml = ''; + let mutCb: (() => void) | undefined; + let popstateCb: (() => void) | undefined; + let navEntryChangeCb: (() => void) | undefined; + let prerenderChangeCb: (() => void) | undefined; + let isPrerendering = false; + let currentHref = 'https://shopee.vn/Dien-thoai-iPhone-16-Pro-i.101.202'; + const h1Title = 'iPhone 16 Pro 256GB'; + const rafs: Array<() => unknown> = []; + let nextTimerId = 1; + const plannedTimers = new Map unknown>(); + const drainTimers = () => { + const cbs = [...plannedTimers.values()]; + plannedTimers.clear(); + for (const cb of cbs) cb(); + }; + const sentMessages: Array<{ type?: string; signal?: unknown }> = []; + const pendingDetectResolvers: Array< + (res: Record) => void + > = []; + const flushContent = async () => { + for (let i = 0; i < 6; i++) { + while (rafs.length) void rafs.shift()!(); + await Promise.resolve(); + } + }; + g.window = { + location: { + get href() { + return currentHref; + }, + set href(v: string) { + currentHref = v; + }, + }, + addEventListener: (ev: string, cb: () => void) => { + if (ev === 'popstate') popstateCb = cb; + }, + navigation: { + addEventListener: (ev: string, cb: () => void) => { + if (ev === 'currententrychange') navEntryChangeCb = cb; + }, + }, + }; + g.document = { + get prerendering() { + return isPrerendering; + }, + addEventListener: (ev: string, cb: () => void) => { + if (ev === 'prerenderingchange') prerenderChangeCb = cb; + }, + body: { + appendChild: (el: { shadowRoot?: { innerHTML: string } }) => { + pillHost = el; + }, + }, + getElementById: (id: string) => + id === 'rate-vn-trust-pill-host' ? pillHost : null, + createElement: () => { + const shadow = { + get innerHTML() { + return shadowHtml; + }, + set innerHTML(v: string) { + shadowHtml = v; + }, + getElementById: () => ({ addEventListener() {} }), + }; + const el = { + id: '', + shadowRoot: null as typeof shadow | null, + attachShadow() { + el.shadowRoot = shadow; + return shadow; + }, + remove() { + pillHost = null; + }, + }; + return el; + }, + querySelectorAll: () => [], + querySelector: (s: string) => + s === 'h1' + ? { textContent: h1Title } + : s === '.pqTWkA' + ? { textContent: '28.990.000₫' } + : null, + }; + g.MutationObserver = class { + constructor(cb: () => void) { + mutCb = cb; + } + observe() {} + }; + g.requestAnimationFrame = (cb: () => unknown) => { + rafs.push(cb); + return rafs.length; + }; + g.setTimeout = ((cb: () => unknown) => { + const id = nextTimerId++; + plannedTimers.set(id, cb); + return id; + }) as unknown; + g.clearTimeout = ((id: number) => { + plannedTimers.delete(id); + }) as unknown; + g.chrome = { + runtime: { + sendMessage: (msg: { type?: string; signal?: unknown }) => { + sentMessages.push(msg); + if (msg.type === 'RATE_PRODUCT_DETECTED') { + return new Promise>((resolve) => { + pendingDetectResolvers.push(resolve); + }); + } + return Promise.resolve({ ok: true }); + }, + }, + }; + + const dealPayload = { + ok: true, + data: { + matchStatus: 'exact_listing', + isAccessoryOrPart: false, + priceAssessment: { + verdict: 'genuine_deal', + badgeLabel: 'Good deal', + explanation: 'Good deal', + cheaperOfferCount: 0, + maxSavingsAmount: 0, + marketMinPrice: 28_990_000, + }, + masterProduct: { rating_score_5: 4.9 }, + reviewSummary: null, + radarSignal: null, + }, + }; + + await import( + new URL('./content/content-script.js?case=1809', import.meta.url).href + ); + await flushContent(); + assert.equal(pendingDetectResolvers.length, 1); + assert.equal( + sentMessages.filter((m) => m.type === 'RATE_PRODUCT_CLEARED').length, + 0 + ); + + // Navigate SPA away via pushState BEFORE the 700ms MutationObserver timer fires, + // then resolve the in-flight PDP lookup during the debounce window: + // canonicalUrl mismatch must discard the pill and immediately emit RATE_PRODUCT_CLEARED. + currentHref = 'https://shopee.vn/search?keyword=iphone'; + mutCb!(); + assert.equal( + plannedTimers.size, + 1, + '700ms MutationObserver debounce timer still pending' + ); + pendingDetectResolvers.shift()!(dealPayload); + await flushContent(); + assert.equal( + pillHost, + null, + 'in-flight PDP lookup resolving during pushState debounce window must never render a pill' + ); + assert.equal( + sentMessages.filter((m) => m.type === 'RATE_PRODUCT_CLEARED').length, + 1, + 'URL mismatch on late lookup resolution must immediately inspect and emit RATE_PRODUCT_CLEARED' + ); + + // Extra DOM mutations while remaining on /search must NOT spam duplicate RATE_PRODUCT_CLEARED messages. + drainTimers(); + mutCb!(); + drainTimers(); + await flushContent(); + assert.equal( + sentMessages.filter((m) => m.type === 'RATE_PRODUCT_CLEARED').length, + 1, + 'repeated non-PDP mutations must not re-send RATE_PRODUCT_CLEARED' + ); + + // Navigating back to the same PDP renders the pill, and Navigation API currententrychange to / clears it immediately. + currentHref = 'https://shopee.vn/Dien-thoai-iPhone-16-Pro-i.101.202'; + navEntryChangeCb!(); + await flushContent(); + assert.equal(pendingDetectResolvers.length, 1); + pendingDetectResolvers.shift()!(dealPayload); + await flushContent(); + assert.ok(pillHost, 'pill renders when returning to PDP'); + + currentHref = 'https://shopee.vn/'; + navEntryChangeCb!(); + await flushContent(); + assert.equal(pillHost, null, 'SPA exit to / must remove rendered pill'); + assert.equal( + sentMessages.filter((m) => m.type === 'RATE_PRODUCT_CLEARED').length, + 2, + 'transitioning away from rendered PDP to / must emit RATE_PRODUCT_CLEARED' + ); + + // Already-scheduled 429 rateLimitRetry timer is genuinely cancelled via clearTimeout on SPA exit to /cart. + currentHref = 'https://shopee.vn/Dien-thoai-iPhone-16-Pro-i.101.202'; + popstateCb!(); + await flushContent(); + assert.equal(pendingDetectResolvers.length, 1); + pendingDetectResolvers.shift()!({ + ok: false, + status: 429, + retryAfterSeconds: 5, + }); + await flushContent(); + assert.equal( + plannedTimers.size, + 1, + '429 on PDP schedules one retry timer' + ); + currentHref = 'https://shopee.vn/cart'; + popstateCb!(); + await flushContent(); + assert.equal( + plannedTimers.size, + 0, + 'SPA exit to /cart must cancel already-scheduled 429 retry timer via clearTimeout' + ); + drainTimers(); + await flushContent(); + assert.equal(pendingDetectResolvers.length, 0); + + // Speculation Rules prerendering defers detection until prerenderingchange fires. + isPrerendering = true; + currentHref = 'https://shopee.vn/Dien-thoai-Galaxy-S25-i.101.303'; + navEntryChangeCb!(); + await flushContent(); + assert.equal( + pendingDetectResolvers.length, + 0, + 'prerendered document must defer RATE_PRODUCT_DETECTED until activated' + ); + assert.ok(prerenderChangeCb, 'must attach prerenderingchange listener'); + isPrerendering = false; + prerenderChangeCb!(); + await flushContent(); + assert.equal( + pendingDetectResolvers.length, + 1, + 'activating prerendered PDP must run inspectCurrentPage' + ); + pendingDetectResolvers.shift()!(dealPayload); + await flushContent(); + assert.ok(pillHost, 'pill renders after prerender activation'); + } finally { + for (const [key, desc] of saved) { + if (desc) Object.defineProperty(g, key, desc); + else delete g[key]; + } + } + + const savedSw = ['chrome', 'fetch'].map( + (k) => [k, Object.getOwnPropertyDescriptor(g, k)] as const + ); + try { + // 2. Service worker: RATE_PRODUCT_CLEARED, sidePanel.open rejection handling, and sender.url validation. + let swOnMsg: + | (( + m: Record, + s: Record, + cb: (r: unknown) => void + ) => boolean | void) + | undefined; + const sessionStore = new Map(); + const sessionChangedEvents: Array< + Record + > = []; + const badges = new Map(); + const sidePanelCalls: Array> = []; + let sidePanelShouldReject = false; + let activeTabId = 42; + let allowSessionRemove = true; + let delaySessionGetOnce = false; + let onSessionGetBlocked: (() => void) | null = null; + let releaseSessionGet: (() => void) | null = null; + let delayLocalGetOnce = false; + let onLocalGetBlocked: (() => void) | null = null; + let releaseLocalGet: (() => void) | null = null; + const fetchedUrls: string[] = []; + let swFetchImpl: (u: string) => Promise = async (u) => { + fetchedUrls.push(u); + return new Response( + JSON.stringify({ + ok: true, + data: { + matchStatus: 'exact_listing', + isAccessoryOrPart: false, + priceAssessment: { verdict: 'genuine_deal' }, + queryIndex: fetchedUrls.length, + }, + }), + { status: 200 } + ); + }; + g.chrome = { + runtime: { + id: 'rate-ext-1809', + getManifest: () => ({ version: '2.0.0' }), + onStartup: { addListener() {} }, + onInstalled: { addListener() {} }, + onMessage: { + addListener(fn: NonNullable) { + swOnMsg = fn; + }, + }, + getURL: (p: string) => `chrome-extension://rate-ext-1809/${p}`, + }, + sidePanel: { + setPanelBehavior: async () => {}, + open: (opts: Record) => { + sidePanelCalls.push(opts); + if (sidePanelShouldReject) { + return Promise.reject( + new Error( + 'sidePanel.open() may only be called in response to a user gesture' + ) + ); + } + return Promise.resolve(); + }, + }, + storage: { + local: { + get: async () => { + if (delayLocalGetOnce) { + delayLocalGetOnce = false; + const gate = new Promise((r) => { + releaseLocalGet = r; + }); + onLocalGetBlocked?.(); + await gate; + } + return { + apiBaseUrl: 'https://rate.vn', + crowdContributeOptIn: false, + }; + }, + set: async () => {}, + remove: async () => {}, + }, + session: { + get: async (keys?: string | string[]) => { + if (delaySessionGetOnce) { + delaySessionGetOnce = false; + const gate = new Promise((r) => { + releaseSessionGet = r; + }); + onSessionGetBlocked?.(); + await gate; + } + const list = Array.isArray(keys) ? keys : keys ? [keys] : []; + return Object.fromEntries( + list + .filter((k) => sessionStore.has(k)) + .map((k) => [k, sessionStore.get(k)]) + ); + }, + set: async (obj: Record) => { + const changes: Record< + string, + { oldValue?: unknown; newValue?: unknown } + > = {}; + for (const [k, v] of Object.entries(obj)) { + const hadOld = sessionStore.has(k); + const oldVal = sessionStore.get(k); + sessionStore.set(k, v); + if (!hadOld || oldVal !== v) { + changes[k] = { + ...(hadOld ? { oldValue: oldVal } : {}), + newValue: v, + }; + } + } + if (Object.keys(changes).length > 0) { + sessionChangedEvents.push(changes); + } + }, + get remove() { + if (!allowSessionRemove) return undefined; + return async (keys: string | string[]) => { + const changes: Record< + string, + { oldValue?: unknown; newValue?: unknown } + > = {}; + for (const k of Array.isArray(keys) ? keys : [keys]) { + if (sessionStore.has(k)) { + changes[k] = { oldValue: sessionStore.get(k) }; + sessionStore.delete(k); + } + } + if (Object.keys(changes).length > 0) { + sessionChangedEvents.push(changes); + } + }; + }, + }, + }, + action: { + setBadgeText: async ({ + text, + tabId, + }: { + text: string; + tabId?: number; + }) => { + if (tabId) badges.set(tabId, { ...badges.get(tabId), text }); + }, + setBadgeBackgroundColor: async ({ + color, + tabId, + }: { + color: string; + tabId?: number; + }) => { + if (tabId) { + badges.set(tabId, { + text: badges.get(tabId)?.text ?? '', + color, + }); + } + }, + onClicked: { addListener() {} }, + }, + tabs: { + query: async () => [{ id: activeTabId, active: true }], + create: async () => ({ id: 1 }), + }, + }; + g.fetch = ((u: string | URL) => swFetchImpl(String(u))) as unknown; + await import( + new URL('./background/service-worker.js?case=1809', import.meta.url) + .href + ); + assert.ok(swOnMsg, 'service worker registered onMessage'); + + const callSw = ( + msg: Record, + sender: Record + ) => + new Promise>((resolve) => { + swOnMsg!(msg, sender, (r) => resolve(r as Record)); + }); + const panelSender = { + id: 'rate-ext-1809', + tab: undefined, + url: 'chrome-extension://rate-ext-1809/sidepanel/sidepanel.html', + }; + const shopeeSignal = { + platform: 'shopee', + canonicalUrl: 'https://shopee.vn/Phone-A-i.10.20', + externalItemId: '20', + externalShopId: '10', + title: 'Phone A 256GB', + selectedVariant: '256GB', + price: 25_990_000, + originalPrice: 29_990_000, + ratingStar: 4.9, + ratingCount: 100, + }; + + // (a) PDP -> /search clears badge, tab_42, lookup_tab_42, tab_latest, and lookup_tab_latest. + const detectRes = await callSw( + { type: 'RATE_PRODUCT_DETECTED', signal: shopeeSignal }, + { + id: 'rate-ext-1809', + tab: { id: 42, active: true }, + url: 'https://shopee.vn/Phone-A-i.10.20?sp_atk=abc', + } + ); + assert.equal(detectRes.ok, true); + assert.equal(badges.get(42)?.text, 'TỐT'); + assert.ok(sessionStore.get('tab_42')); + assert.ok(sessionStore.get('lookup_tab_42')); + assert.ok(sessionStore.get('tab_latest')); + assert.ok(sessionStore.get('lookup_tab_latest')); + + const clearRes = await callSw( + { type: 'RATE_PRODUCT_CLEARED' }, + { + id: 'rate-ext-1809', + tab: { id: 42, active: true }, + url: 'https://shopee.vn/search?keyword=phone', + } + ); + assert.equal(clearRes.ok, true); + assert.equal(badges.get(42)?.text, ''); + assert.equal(sessionStore.has('tab_42'), false); + assert.equal(sessionStore.has('lookup_tab_42'), false); + assert.equal(sessionStore.has('tab_latest'), false); + assert.equal(sessionStore.has('lookup_tab_latest'), false); + + const fullAfterClear = await callSw( + { type: 'RATE_SIDEPANEL_FETCH_FULL', selectedVariant: null }, + panelSender + ); + assert.equal(fullAfterClear.ok, true); + assert.equal(fullAfterClear.signal, null); + assert.equal(fullAfterClear.data, null); + + // (b) PDP -> manual quick_query -> /search clears tab_42 & badge while preserving newer quick_query tab_latest. + await callSw( + { type: 'RATE_PRODUCT_DETECTED', signal: shopeeSignal }, + { + id: 'rate-ext-1809', + tab: { id: 42, active: true }, + url: 'https://shopee.vn/Phone-A-i.10.20', + } + ); + assert.equal(badges.get(42)?.text, 'TỐT'); + const quickRes = await callSw( + { type: 'RATE_SIDEPANEL_QUICK_QUERY', query: 'Galaxy S25 Ultra' }, + panelSender + ); + assert.equal(quickRes.ok, true); + const fetchCountAfterQuick = fetchedUrls.length; + + const clearAfterQuick = await callSw( + { type: 'RATE_PRODUCT_CLEARED' }, + { + id: 'rate-ext-1809', + tab: { id: 42, active: true }, + url: 'https://shopee.vn/', + } + ); + assert.equal(clearAfterQuick.ok, true); + assert.equal( + badges.get(42)?.text, + '', + 'tab badge must still clear on SPA exit even when quick_query owns tab_latest' + ); + assert.equal(sessionStore.has('tab_42'), false); + assert.equal(sessionStore.has('lookup_tab_42'), false); + assert.equal( + (sessionStore.get('tab_latest') as { source?: string })?.source, + 'quick_query', + 'newer quick_query tab_latest must be preserved across SPA exit' + ); + assert.deepEqual(sessionStore.get('lookup_tab_latest'), quickRes.data); + + const fullUsingPreservedQuick = await callSw( + { type: 'RATE_SIDEPANEL_FETCH_FULL', selectedVariant: null }, + panelSender + ); + assert.equal(fullUsingPreservedQuick.ok, true); + assert.deepEqual(fullUsingPreservedQuick.data, quickRes.data); + assert.equal( + fetchedUrls.length, + fetchCountAfterQuick, + 'pendingQuickQueryReceipt must remain intact when SPA exit preserves quick_query' + ); + + // (c) Minimal storage mock without session.remove clears keys to null cleanly. + allowSessionRemove = false; + await callSw( + { type: 'RATE_PRODUCT_DETECTED', signal: shopeeSignal }, + { + id: 'rate-ext-1809', + tab: { id: 42, active: true }, + url: 'https://shopee.vn/Phone-A-i.10.20', + } + ); + await callSw( + { type: 'RATE_PRODUCT_CLEARED' }, + { + id: 'rate-ext-1809', + tab: { id: 42, active: true }, + url: 'https://shopee.vn/cart', + } + ); + assert.equal(sessionStore.get('tab_42'), null); + assert.equal(sessionStore.get('lookup_tab_42'), null); + assert.equal(sessionStore.get('tab_latest'), null); + assert.equal(sessionStore.get('lookup_tab_latest'), null); + allowSessionRemove = true; + sessionStore.clear(); + + // (d) Background tab RATE_PRODUCT_CLEARED clears only its own tab state without wiping active tab_latest. + await callSw( + { type: 'RATE_PRODUCT_DETECTED', signal: shopeeSignal }, + { + id: 'rate-ext-1809', + tab: { id: 42, active: true }, + url: 'https://shopee.vn/Phone-A-i.10.20', + } + ); + const lazadaSignal = { + ...shopeeSignal, + platform: 'lazada', + canonicalUrl: 'https://www.lazada.vn/products/Phone-B-i30-s40.html', + externalItemId: '30', + externalShopId: '40', + title: 'Phone B', + }; + await callSw( + { type: 'RATE_PRODUCT_DETECTED', signal: lazadaSignal }, + { + id: 'rate-ext-1809', + tab: { id: 99, active: false }, + url: 'https://www.lazada.vn/products/Phone-B-i30-s40.html', + } + ); + assert.equal(badges.get(99)?.text, 'TỐT'); + assert.ok(sessionStore.get('tab_99')); + + await callSw( + { type: 'RATE_PRODUCT_CLEARED' }, + { + id: 'rate-ext-1809', + tab: { id: 99, active: false }, + url: 'https://www.lazada.vn/cart', + } + ); + assert.equal(badges.get(99)?.text, ''); + assert.equal(sessionStore.has('tab_99'), false); + assert.equal(sessionStore.has('lookup_tab_99'), false); + assert.equal( + ( + sessionStore.get('tab_latest') as { + signal?: { canonicalUrl?: string }; + } + )?.signal?.canonicalUrl, + shopeeSignal.canonicalUrl, + 'background tab SPA exit must not wipe active tab_latest' + ); + assert.ok(sessionStore.get('lookup_tab_latest')); + + // (e) In-flight RATE_PRODUCT_DETECTED resolving after RATE_PRODUCT_CLEARED stays stale and writes nothing, + // and cold-session lookup_tab_latest === undefined still fires lookup_tab_latest storage.onChanged. + sessionStore.delete('lookup_tab_latest'); + sessionChangedEvents.length = 0; + let resolveSlowPill!: (res: Response) => void; + swFetchImpl = () => + new Promise((resolve) => { + resolveSlowPill = resolve; + }); + const slowDetectPromise = callSw( + { type: 'RATE_PRODUCT_DETECTED', signal: shopeeSignal }, + { + id: 'rate-ext-1809', + tab: { id: 42, active: true }, + url: 'https://shopee.vn/Phone-A-i.10.20', + } + ); + await new Promise((r) => setImmediate(r)); + await callSw( + { type: 'RATE_PRODUCT_CLEARED' }, + { + id: 'rate-ext-1809', + tab: { id: 42, active: true }, + url: 'https://shopee.vn/search?keyword=phone', + } + ); + assert.equal(badges.get(42)?.text, ''); + assert.equal(sessionStore.has('lookup_tab_42'), false); + assert.equal(sessionStore.has('lookup_tab_latest'), false); + assert.equal( + sessionChangedEvents.some((ev) => 'lookup_tab_latest' in ev), + true, + 'SPA exit before lookup_tab_latest was ever written must still fire lookup_tab_latest storage.onChanged' + ); + + resolveSlowPill( + new Response( + JSON.stringify({ + ok: true, + data: { + matchStatus: 'exact_listing', + isAccessoryOrPart: false, + priceAssessment: { verdict: 'inflated_fake_sale' }, + }, + }), + { status: 200 } + ) + ); + const slowDetectRes = await slowDetectPromise; + assert.equal(slowDetectRes.stale, true); + assert.equal( + badges.get(42)?.text, + '', + 'superseded PDP lookup must not restore stale badge after RATE_PRODUCT_CLEARED' + ); + assert.equal(sessionStore.has('lookup_tab_42'), false); + assert.equal(sessionStore.has('lookup_tab_latest'), false); + + swFetchImpl = async (u) => { + fetchedUrls.push(u); + return new Response( + JSON.stringify({ + ok: true, + data: { + matchStatus: 'exact_listing', + isAccessoryOrPart: false, + priceAssessment: { verdict: 'genuine_deal' }, + }, + }), + { status: 200 } + ); + }; + + // (e2) In-flight RATE_PRODUCT_CLEARED superseded by a newer RATE_PRODUCT_DETECTED must not wipe newer state. + delaySessionGetOnce = true; + const clearBlockedOnGet = new Promise((r) => { + onSessionGetBlocked = r; + }); + const supersededClearPromise = callSw( + { type: 'RATE_PRODUCT_CLEARED' }, + { + id: 'rate-ext-1809', + tab: { id: 42, active: true }, + url: 'https://shopee.vn/search?keyword=phone', + } + ); + await clearBlockedOnGet; + const newerDetectRes = await callSw( + { type: 'RATE_PRODUCT_DETECTED', signal: shopeeSignal }, + { + id: 'rate-ext-1809', + tab: { id: 42, active: true }, + url: 'https://shopee.vn/Phone-A-i.10.20', + } + ); + assert.equal(newerDetectRes.ok, true); + releaseSessionGet!(); + const supersededClearRes = await supersededClearPromise; + assert.equal(supersededClearRes.stale, true); + assert.equal(badges.get(42)?.text, 'TỐT'); + assert.ok(sessionStore.get('tab_42')); + assert.ok(sessionStore.get('tab_latest')); + assert.ok(sessionStore.get('lookup_tab_42')); + assert.ok(sessionStore.get('lookup_tab_latest')); + + // (e3) In-flight RATE_SIDEPANEL_FETCH_FULL fallback (reading tab_latest from non-product active tab) + // superseded by RATE_PRODUCT_CLEARED must return stale: true and never resurrect tab_latest. + activeTabId = 88; + delayLocalGetOnce = true; + const fullBlockedOnSettings = new Promise((r) => { + onLocalGetBlocked = r; + }); + const fallbackFullPromise = callSw( + { type: 'RATE_SIDEPANEL_FETCH_FULL', selectedVariant: '512GB' }, + panelSender + ); + await fullBlockedOnSettings; + await callSw( + { type: 'RATE_PRODUCT_CLEARED' }, + { + id: 'rate-ext-1809', + tab: { id: 42, active: true }, + url: 'https://shopee.vn/', + } + ); + assert.equal(sessionStore.has('tab_latest'), false); + releaseLocalGet!(); + const fallbackFullRes = await fallbackFullPromise; + assert.equal( + fallbackFullRes.stale, + true, + 'fallback full lookup superseded by RATE_PRODUCT_CLEARED must be marked stale' + ); + assert.equal( + sessionStore.has('tab_latest'), + false, + 'superseded fallback overrideVariant write must not resurrect cleared tab_latest' + ); + activeTabId = 42; + + // (f) chrome.sidePanel.open rejections on RATE_OPEN_SIDE_PANEL are caught cleanly without unhandledRejection. + const unhandled: unknown[] = []; + const onUnhandled = (reason: unknown) => { + unhandled.push(reason); + }; + process.on('unhandledRejection', onUnhandled); + try { + sidePanelShouldReject = true; + sidePanelCalls.length = 0; + const openTabRes = await callSw( + { type: 'RATE_OPEN_SIDE_PANEL' }, + { + id: 'rate-ext-1809', + tab: { id: 42, windowId: 7 }, + url: 'https://shopee.vn/Phone-A-i.10.20', + } + ); + const openWindowRes = await callSw( + { type: 'RATE_OPEN_SIDE_PANEL' }, + { + id: 'rate-ext-1809', + tab: { windowId: 7 }, + url: 'https://www.topzone.vn/iphone/iphone-16-pro-max', + } + ); + await new Promise((r) => setTimeout(r, 20)); + assert.equal(openTabRes.ok, true); + assert.equal(openWindowRes.ok, true); + assert.deepEqual(sidePanelCalls, [{ tabId: 42 }, { windowId: 7 }]); + assert.equal( + unhandled.length, + 0, + 'rejected chrome.sidePanel.open must never trigger unhandledRejection' + ); + } finally { + sidePanelShouldReject = false; + process.off('unhandledRejection', onUnhandled); + } + + // (g) sender.url origin & platform validation on RATE_PRODUCT_DETECTED, RATE_PRODUCT_CLEARED, and RATE_OPEN_SIDE_PANEL. + await callSw( + { type: 'RATE_PRODUCT_DETECTED', signal: shopeeSignal }, + { + id: 'rate-ext-1809', + tab: { id: 42, active: true }, + url: 'https://shopee.vn/Phone-A-i.10.20', + } + ); + assert.equal(badges.get(42)?.text, 'TỐT'); + const fetchesBeforeUntrusted = fetchedUrls.length; + sidePanelCalls.length = 0; + + for (const badUrl of [ + 'https://evil.example.com/Phone-A-i.10.20', + 'https://shopee.vn.evil.com/Phone-A-i.10.20', + 'https://evilshopee.vn/Phone-A-i.10.20', + 'https://rate.vn/smartphones/iphone-16', + 'https://www.rate.vn/', + 'http://shopee.vn/Phone-A-i.10.20', + 'https://shopee.vn:8443/Phone-A-i.10.20', + 'https://user:pass@shopee.vn/Phone-A-i.10.20', + 'https://m.tiktok.com/v/12345', + 'chrome-extension://rate-ext-1809/onboarding/onboarding.html', + 'chrome-extension://rate-ext-1809/sidepanel/sidepanel.html', + 'about:blank', + '', + ]) { + const badSender = { + id: 'rate-ext-1809', + tab: { id: 42, windowId: 7, active: true }, + url: badUrl, + }; + const rejDetect = await callSw( + { type: 'RATE_PRODUCT_DETECTED', signal: shopeeSignal }, + badSender + ); + const rejClear = await callSw( + { type: 'RATE_PRODUCT_CLEARED' }, + badSender + ); + const rejOpen = await callSw( + { type: 'RATE_OPEN_SIDE_PANEL' }, + badSender + ); + assert.equal(rejDetect.ok, false, `must reject detect from ${badUrl}`); + assert.equal(rejClear.ok, false, `must reject clear from ${badUrl}`); + assert.equal(rejOpen.ok, false, `must reject open from ${badUrl}`); + } + + // Platform-mismatched and cross-origin sister-domain/subdomain sender.url on RATE_PRODUCT_DETECTED. + for (const [mismatchSenderUrl, mismatchSignal] of [ + ['https://www.lazada.vn/products/p-i1-s2.html', shopeeSignal], + ['https://shopee.vn/Phone-A-i.10.20', lazadaSignal], + [ + 'https://shopee.vn/Phone-A-i.10.20', + { + ...shopeeSignal, + canonicalUrl: 'https://www.lazada.vn/products/p-i1-s2.html', + }, + ], + [ + 'https://shopee.vn/Phone-A-i.10.20', + { + ...shopeeSignal, + canonicalUrl: 'https://evil.example.com/p-i.10.20', + }, + ], + [ + 'https://shopee.vn/Phone-A-i.10.20', + { ...shopeeSignal, canonicalUrl: '' }, + ], + [ + 'https://topzone.vn/iphone/iphone-16-pro-max', + { + ...shopeeSignal, + platform: 'tgdd', + canonicalUrl: + 'https://www.thegioididong.com/dtdd/iphone-16-pro-max', + }, + ], + ['https://pages.lazada.vn/wow/gcp/vn', lazadaSignal], + [ + 'https://www.tiktok.com/@apple/product/12345', + { + ...shopeeSignal, + platform: 'tiktok', + canonicalUrl: 'https://shop.tiktok.com/view/product/12345', + }, + ], + ] as const) { + const mismatchRes = await callSw( + { type: 'RATE_PRODUCT_DETECTED', signal: mismatchSignal }, + { + id: 'rate-ext-1809', + tab: { id: 42, active: true }, + url: mismatchSenderUrl, + } + ); + assert.equal( + mismatchRes.ok, + false, + `must reject mismatched sender ${mismatchSenderUrl}` + ); + } + + // Missing/undefined signal, tabless sender, TAB_ID_NONE (-1), non-active documentLifecycle, + // subframe sender (frameId !== 0), opaque origin, and foreign extension id must all be rejected. + assert.equal( + ( + await callSw( + { type: 'RATE_PRODUCT_DETECTED' }, + { + id: 'rate-ext-1809', + tab: { id: 42, active: true }, + url: 'https://www.lazada.vn/products/Phone-B-i30-s40.html', + } + ) + ).ok, + false, + 'RATE_PRODUCT_DETECTED without signal must be rejected' + ); + assert.equal( + ( + await callSw( + { type: 'RATE_PRODUCT_CLEARED' }, + { id: 'rate-ext-1809' } + ) + ).ok, + false, + 'tabless sender must be rejected' + ); + assert.equal( + ( + await callSw( + { type: 'RATE_PRODUCT_CLEARED' }, + { + id: 'rate-ext-1809', + tab: { id: -1, active: true }, + url: 'https://shopee.vn/search', + } + ) + ).ok, + false, + 'TAB_ID_NONE (-1) sender must be rejected' + ); + for (const lifecycle of ['prerender', 'cached', 'pending_deletion']) { + assert.equal( + ( + await callSw( + { type: 'RATE_PRODUCT_CLEARED' }, + { + id: 'rate-ext-1809', + documentLifecycle: lifecycle, + tab: { id: 42, active: true }, + url: 'https://shopee.vn/search', + } + ) + ).ok, + false, + `inactive documentLifecycle ${lifecycle} must be rejected` + ); + } + assert.equal( + ( + await callSw( + { type: 'RATE_PRODUCT_CLEARED' }, + { + id: 'rate-ext-1809', + frameId: 1, + tab: { id: 42, active: true }, + url: 'https://shopee.vn/search', + } + ) + ).ok, + false + ); + assert.equal( + ( + await callSw( + { type: 'RATE_OPEN_SIDE_PANEL' }, + { + id: 'foreign-ext-id', + tab: { id: 42, windowId: 7 }, + url: 'https://shopee.vn/Phone-A-i.10.20', + } + ) + ).ok, + false + ); + assert.equal( + ( + await callSw( + { type: 'RATE_PRODUCT_CLEARED' }, + { + id: 'rate-ext-1809', + origin: 'null', + tab: { id: 42, active: true }, + url: 'https://shopee.vn/search', + } + ) + ).ok, + false + ); + + assert.equal( + fetchedUrls.length, + fetchesBeforeUntrusted, + 'rejected senders must never trigger lookup network requests' + ); + assert.equal( + sidePanelCalls.length, + 0, + 'rejected senders must never invoke chrome.sidePanel.open' + ); + assert.equal( + badges.get(42)?.text, + 'TỐT', + 'rejected RATE_PRODUCT_CLEARED/DETECTED must not mutate existing badge or session state' + ); + assert.ok(sessionStore.get('tab_42')); + assert.ok(sessionStore.get('lookup_tab_42')); + + // Valid TikTok SPA exit to /foryou on www.tiktok.com is accepted on RATE_PRODUCT_CLEARED. + const tiktokClearRes = await callSw( + { type: 'RATE_PRODUCT_CLEARED' }, + { + id: 'rate-ext-1809', + tab: { id: 42, active: true }, + url: 'https://www.tiktok.com/foryou', + } + ); + assert.equal(tiktokClearRes.ok, true); + assert.equal(badges.get(42)?.text, ''); + + console.log( + ' ✅ [SPA exit stale pill/badge/session cleanup, sidePanel.open catch & sender.url checks (#1809)] Verified' + ); + } finally { + for (const [key, desc] of savedSw) { + if (desc) Object.defineProperty(g, key, desc); + else delete g[key]; + } + } + } console.log('🎉 All Rate Browser Extension MV3 v2 tests passed!'); } From c3e97a977c0d2201b2e32e68440cb6b28869490d Mon Sep 17 00:00:00 2001 From: JOY <5027251+JOY@users.noreply.github.com> Date: Sun, 22 Nov 2026 00:06:10 +0700 Subject: [PATCH 04/24] fix(web): group item variants before home battles and deals, cache catalog reads (#1816) * fix(web): group item variants before home battles and deals, cache catalog reads - Group homepage catalog rows by Item before building popular 1v1 battles so storage variants of the same Item never pair and battle URLs use canonical Item slugs. - Apply groupCatalogRowsByItem on /deals consistently with category pages under RATE_ITEM_MODEL. - Switch uncached getMasterProducts calls on / and /contact-us to getCachedMasterProducts so rate:products tag invalidation applies. - Add unit tests in home-deals-item-grouping.test.ts. Closes #1808 * fix(web): keep per-variant deal spread and guard home battle versus slugs - Preserve the standing variant max_price on Item-grouped /deals rows so spreadSavingsVnd reflects cross-platform savings rather than cross-capacity price deltas. - Guard homepage 1v1 battles with versusHref, itemSlugFromProductAnswer, and isParseableVersusPair, skipping -vs- slugs and side collisions. - Expand home-deals-item-grouping.test.ts for review round 1 findings. --- apps/web/package.json | 2 +- apps/web/src/app/[locale]/contact-us/page.tsx | 11 +- apps/web/src/app/[locale]/deals/page.tsx | 41 +- apps/web/src/app/[locale]/page.tsx | 129 ++- .../home-deals-item-grouping.test.ts | 789 ++++++++++++++++++ 5 files changed, 951 insertions(+), 21 deletions(-) create mode 100644 apps/web/src/lib/__tests__/home-deals-item-grouping.test.ts diff --git a/apps/web/package.json b/apps/web/package.json index a3f97999..4cb25eab 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -8,7 +8,7 @@ "lint": "eslint . --max-warnings 0", "preview": "next build && next start", "start": "next start", - "test": "pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/server/rate/__tests__/core-rate-architecture.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/server/rate/__tests__/item-model.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/server/rate/__tests__/item-directory.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/server/rate/__tests__/compare-selection.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/app/api/v1/rate/__tests__/write-paths.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/app/api/v1/rate/__tests__/spec-scope.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/app/api/v1/rate/__tests__/versus-parity.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/app/api/v1/rate/__tests__/item-fields.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/logger.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/review-display.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/review-form-v2.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/review-value-display.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/concurrency.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/merchant-profile-reads.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/merchant-review-risk.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/item-directory.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/watchlist-sizes.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/item-compare.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-variant-links.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/compare-params.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/catalog-listing.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/discount-verdict-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-detail-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-competitors.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/analyzer-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/sold-count.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/content-lang.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/image-url.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/client-messages.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/seo.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/offer-plausibility.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-market-stats.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/sitemap-lastmod.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/leaderboard-badge-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/leaderboard-criteria.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/trust-depth-v2.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/rating-score.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/rating-display.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/focus-trap.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/display-truth.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/spec-template.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-spec-rows.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/sale-state.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/csp.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/device-images.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/render-timing.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/model-match.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/developers-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/plural-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/locale-format.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/paged-reads.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/server-timing.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/hero-products.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/components/elementary/__tests__/locale-switcher-a11y.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/components/compare/__tests__/quick-toggle-a11y.test.ts", + "test": "pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/server/rate/__tests__/core-rate-architecture.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/server/rate/__tests__/item-model.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/server/rate/__tests__/item-directory.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/server/rate/__tests__/compare-selection.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/app/api/v1/rate/__tests__/write-paths.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/app/api/v1/rate/__tests__/spec-scope.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/app/api/v1/rate/__tests__/versus-parity.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/app/api/v1/rate/__tests__/item-fields.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/logger.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/review-display.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/review-form-v2.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/review-value-display.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/concurrency.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/merchant-profile-reads.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/merchant-review-risk.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/item-directory.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/home-deals-item-grouping.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/watchlist-sizes.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/item-compare.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-variant-links.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/compare-params.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/catalog-listing.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/discount-verdict-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-detail-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-competitors.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/analyzer-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/sold-count.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/content-lang.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/image-url.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/client-messages.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/seo.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/offer-plausibility.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-market-stats.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/sitemap-lastmod.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/leaderboard-badge-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/leaderboard-criteria.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/trust-depth-v2.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/rating-score.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/rating-display.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/focus-trap.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/display-truth.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/spec-template.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-spec-rows.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/sale-state.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/csp.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/device-images.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/render-timing.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/model-match.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/developers-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/plural-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/locale-format.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/paged-reads.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/server-timing.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/hero-products.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/components/elementary/__tests__/locale-switcher-a11y.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/components/compare/__tests__/quick-toggle-a11y.test.ts", "typecheck": "tsc --noEmit" }, "browserslist": [ diff --git a/apps/web/src/app/[locale]/contact-us/page.tsx b/apps/web/src/app/[locale]/contact-us/page.tsx index 1e0d6629..2dd03a46 100644 --- a/apps/web/src/app/[locale]/contact-us/page.tsx +++ b/apps/web/src/app/[locale]/contact-us/page.tsx @@ -4,8 +4,7 @@ import { Link } from '@/lib/navigation'; import PageShell from '../components/page-shell'; import HeroSearchInspector from '@/components/home/HeroSearchInspector'; import CommunityQuarantineSubmitForm from '@/components/contact/CommunityQuarantineSubmitForm'; -import { getMasterProducts } from '@/lib/rate-api'; -import { getCachedMerchantsList } from '@/server/rate'; +import { getCachedMasterProducts, getCachedMerchantsList } from '@/server/rate'; import { BsCodeSquare, BsEnvelopeAt, @@ -42,6 +41,12 @@ export async function generateMetadata({ }; } +export function loadContactHeroProducts( + readProducts: typeof getCachedMasterProducts = getCachedMasterProducts +) { + return readProducts({ limit: 6 }); +} + export default async function ContactUsPage({ params, }: { @@ -50,7 +55,7 @@ export default async function ContactUsPage({ const { locale } = await params; setRequestLocale(locale); const [products, merchants, t] = await Promise.all([ - getMasterProducts({ limit: 6 }), + loadContactHeroProducts(), getCachedMerchantsList(), getTranslations({ locale, namespace: 'staticPages' }), ]); diff --git a/apps/web/src/app/[locale]/deals/page.tsx b/apps/web/src/app/[locale]/deals/page.tsx index 915caa49..dcdc52b2 100644 --- a/apps/web/src/app/[locale]/deals/page.tsx +++ b/apps/web/src/app/[locale]/deals/page.tsx @@ -1,9 +1,12 @@ import type { Metadata } from 'next'; import { getTranslations, setRequestLocale } from 'next-intl/server'; import { Link } from '@/lib/navigation'; +import type { MasterProduct } from '@repo/shared-data'; import { getCachedMasterProducts, getCrossPlatformDealRadar, + groupCatalogRowsByItem, + type ItemCatalogRow, } from '@/server/rate'; import { normalizeRateLocale } from '@/lib/rate-i18n'; import PageShell from '../components/page-shell'; @@ -32,17 +35,45 @@ export async function generateMetadata({ }; } +export async function loadDealsCatalogProducts( + readProducts: typeof getCachedMasterProducts = getCachedMasterProducts, + groupByItem: typeof groupCatalogRowsByItem = groupCatalogRowsByItem +): Promise { + const rows = await readProducts({ + categorySlug: 'all', + limit: 100, + sort: 'rating_desc', + }); + // RATE_ITEM_MODEL on: one ranked deal card per Item (DOS/Rate#1155, #1808). + const grouped = await groupByItem(rows).catch((): ItemCatalogRow[] => rows); + if (grouped === rows) return rows; + // DealFinderWorkspace reports max_price - min_price as cross-platform savings + // for the standing (cheapest) variant, so keep that variant's own max_price + // rather than the multi-capacity range groupRowsByItem builds for ProductCard. + const variantById = new Map(rows.map((r) => [r.id, r])); + return grouped.map((row) => { + if (!row.item_slug || !row.id) return row; + const standing = variantById.get(row.id); + if (!standing) return row; + const min = Number(row.min_price) || 0; + const rawStandingMax = Number(standing.max_price); + const standingMax = + Number.isFinite(rawStandingMax) && rawStandingMax > 0 + ? Math.max(rawStandingMax, min) + : min; + return standingMax === row.max_price + ? row + : { ...row, max_price: standingMax }; + }); +} + export default async function DealsPage({ params }: PageProps) { const { locale } = await params; setRequestLocale(locale); const lang = normalizeRateLocale(locale); const t = await getTranslations({ locale: lang, namespace: 'deals' }); const [products, dealRadar] = await Promise.all([ - getCachedMasterProducts({ - categorySlug: 'all', - limit: 100, - sort: 'rating_desc', - }), + loadDealsCatalogProducts(), getCrossPlatformDealRadar({ categorySlug: 'all', // Max page size so every ranked card gets its own Radar badge. diff --git a/apps/web/src/app/[locale]/page.tsx b/apps/web/src/app/[locale]/page.tsx index 50c0bccb..cdc72d41 100644 --- a/apps/web/src/app/[locale]/page.tsx +++ b/apps/web/src/app/[locale]/page.tsx @@ -9,15 +9,25 @@ import PageShell from './components/page-shell'; import { EMPTY_RATE_PLATFORM_STATS, getMasterProductHref, - getMasterProducts, } from '@/lib/rate-api'; -import { buildPopular1v1BattlesForCategory } from '@/lib/leaderboard-engine'; import { + buildPopular1v1BattlesForCategory, + rankMasterProductsForLeaderboard, +} from '@/lib/leaderboard-engine'; +import { + canonicalVersusOrder, + isParseableVersusPair, + itemSlugFromProductAnswer, + versusHref, +} from '@/lib/item-compare'; +import { + getCachedMasterProducts, getCachedMerchantsList, getCachedRatePlatformStats, getCrossPlatformDealRadar, groupCatalogRowsByItem, type CrossPlatformDealRadarResponse, + type ItemCatalogRow, type MerchantSummary, type RatePlatformStats, } from '@/server/rate'; @@ -281,6 +291,107 @@ export function buildHomeQuickCompareSlugs( return slugs; } +export function loadHomeMasterProducts( + readProducts: typeof getCachedMasterProducts = getCachedMasterProducts +) { + return readProducts({ categorySlug: 'all', limit: 24 }); +} + +export function buildHomePopularBattles( + products: MasterProduct[], + maxPairs = 3 +) { + const itemSlugBySlug = Object.create(null) as Record; + for (const row of products) { + if (!row || typeof row.slug !== 'string' || !row.slug) continue; + const itemSlug = (row as ItemCatalogRow).item_slug; + if (typeof itemSlug === 'string' && itemSlug) { + itemSlugBySlug[row.slug] = itemSlug; + } + } + + const ranked = rankMasterProductsForLeaderboard(products, { + category: 'smartphones', + criterion: 'combined', + }); + const seenSides = new Set(); + const sideByVariantSlug = Object.create(null) as Record; + const uniqueCandidates: MasterProduct[] = []; + + for (const entry of ranked) { + const slug = entry.slug; + if (!slug) continue; + const rawItemSlug = (entry.product as ItemCatalogRow).item_slug; + if ( + typeof rawItemSlug === 'string' && + rawItemSlug.toLowerCase().includes('-vs-') + ) { + continue; + } + const effectiveSide = rawItemSlug + ? itemSlugFromProductAnswer( + { ok: true, data: { item: { slug: rawItemSlug } } }, + slug + ) + : slug; + if (!effectiveSide || effectiveSide.toLowerCase().includes('-vs-')) { + continue; + } + const sideKey = effectiveSide.toLowerCase(); + if (seenSides.has(sideKey)) continue; + seenSides.add(sideKey); + sideByVariantSlug[slug] = effectiveSide; + uniqueCandidates.push(entry.product); + } + + const rawBattles = buildPopular1v1BattlesForCategory( + uniqueCandidates, + 'smartphones', + 20 + ); + const seenHrefs = new Set(); + const resolved: typeof rawBattles = []; + for (const battle of rawBattles) { + const slugA = battle.productA.slug; + const slugB = battle.productB.slug; + const sideA = sideByVariantSlug[slugA]; + const sideB = sideByVariantSlug[slugB]; + if (!sideA || !sideB || sideA.toLowerCase() === sideB.toLowerCase()) { + continue; + } + const [first, second] = canonicalVersusOrder(sideA, sideB); + if ( + !isParseableVersusPair(first, second) || + !isParseableVersusPair(first.toLowerCase(), second.toLowerCase()) + ) { + continue; + } + const href = versusHref(slugA, slugB, itemSlugBySlug); + const hrefKey = href.toLowerCase(); + if (seenHrefs.has(hrefKey)) continue; + seenHrefs.add(hrefKey); + resolved.push({ + ...battle, + versusSlug: href.replace(/^\/compare\//, ''), + versusHref: href, + }); + if (resolved.length >= maxPairs) break; + } + return resolved; +} + +export async function resolveHomeGroupedCatalog( + masterProducts: MasterProduct[], + groupByItem: typeof groupCatalogRowsByItem = groupCatalogRowsByItem +) { + // RATE_ITEM_MODEL on: one grid card and one 1v1 battle slot per Item (DOS/Rate#1155, #1808). + const gridProducts = await groupByItem(masterProducts).catch( + () => masterProducts + ); + const popularHomeBattles = buildHomePopularBattles(gridProducts, 3); + return { gridProducts, popularHomeBattles }; +} + export function sanitizeHomeReviewStars(value: unknown): number | null { const parsed = parseStrictFiniteNumber(value); if (parsed == null) return null; @@ -861,7 +972,7 @@ export default async function Home({ // it) must not blank the sections that did load. const [productsRes, merchantsRes, statsRes, radarRes] = await Promise.allSettled([ - getMasterProducts({ categorySlug: 'all', limit: 24 }), + loadHomeMasterProducts(), getCachedMerchantsList(), getCachedRatePlatformStats(), getCrossPlatformDealRadar({ categorySlug: 'all', limit: 50 }), @@ -879,11 +990,6 @@ export default async function Home({ } } - const popularHomeBattles = buildPopular1v1BattlesForCategory( - masterProducts, - 'smartphones', - 3 - ); const radarIndex = buildHomeRadarIndex(dealRadar); const catSlugById = new Map(); @@ -891,10 +997,9 @@ export default async function Home({ catSlugById.set(c.id, c.slug); } - // RATE_ITEM_MODEL on: one grid card per Item (DOS/Rate#1155). - const gridProducts = await groupCatalogRowsByItem(masterProducts).catch( - () => masterProducts - ); + // RATE_ITEM_MODEL on: one grid card and one 1v1 battle slot per Item (DOS/Rate#1155, #1808). + const { gridProducts, popularHomeBattles } = + await resolveHomeGroupedCatalog(masterProducts); const quickCompareSlugs = buildHomeQuickCompareSlugs( gridProducts, catSlugById diff --git a/apps/web/src/lib/__tests__/home-deals-item-grouping.test.ts b/apps/web/src/lib/__tests__/home-deals-item-grouping.test.ts new file mode 100644 index 00000000..c8cdc9bb --- /dev/null +++ b/apps/web/src/lib/__tests__/home-deals-item-grouping.test.ts @@ -0,0 +1,789 @@ +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import type { MasterProduct } from '@repo/shared-data'; + +process.env.NEXT_PUBLIC_SUPABASE_URL = 'https://stub.supabase.test'; +process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY = 'anon-test-key'; + +const testDir = dirname(fileURLToPath(import.meta.url)); + +const CATEGORY_SMARTPHONES = 'c0000000-0000-4000-8000-000000000001'; +const ITEM_IPHONE = '11111111-1111-4111-8111-111111111801'; +const ITEM_GALAXY = '11111111-1111-4111-8111-111111111802'; +const V_IPHONE_256 = '22222222-2222-4222-8222-222222222256'; +const V_IPHONE_512 = '22222222-2222-4222-8222-222222222512'; +const V_GALAXY_256 = '22222222-2222-4222-8222-222222223256'; +const V_XIAOMI_LONE = '22222222-2222-4222-8222-222222224256'; + +function makePhone( + id: string, + slug: string, + name: string, + brand: string, + minPrice: number, + maxPrice: number, + ratingScore: number, + expertRating: number, + storageGb: number | null = 256 +): MasterProduct { + return { + id, + slug, + name, + brand, + model: name, + model_key: `${brand.toLowerCase()}:${slug}`, + variant_name: storageGb ? `${storageGb}GB` : null, + category_id: CATEGORY_SMARTPHONES, + status: 'published', + storage_gb: storageGb, + ram_gb: 12, + min_price: minPrice, + max_price: maxPrice, + merchant_count: 3, + rating_score: ratingScore, + user_rating_weighted: ratingScore, + average_rating: ratingScore, + expert_rating: expertRating, + authentic_review_count: 18, + total_reviews: 20, + images: [], + thumbnail: null, + key_specs: { + screen: '6.9" LTPO OLED 120Hz', + chip: 'Flagship SoC', + battery: '5000 mAh', + }, + metadata: { category_slug: 'smartphones' }, + specifications: { + category_slug: 'smartphones', + } as MasterProduct['specifications'], + } as unknown as MasterProduct; +} + +const CATALOG_ROWS: MasterProduct[] = [ + makePhone( + V_IPHONE_512, + 'apple-iphone-16-pro-max-512gb', + 'Apple iPhone 16 Pro Max 512GB', + 'Apple', + 34_990_000, + 36_990_000, + 4.9, + 9.4, + 512 + ), + makePhone( + V_IPHONE_256, + 'apple-iphone-16-pro-max-256gb', + 'Apple iPhone 16 Pro Max 256GB', + 'Apple', + 29_990_000, + 31_990_000, + 4.9, + 9.4, + 256 + ), + makePhone( + V_GALAXY_256, + 'samsung-galaxy-s25-ultra-256gb', + 'Samsung Galaxy S25 Ultra 256GB', + 'Samsung', + 27_990_000, + 33_990_000, + 4.8, + 9.2, + 256 + ), + makePhone( + V_XIAOMI_LONE, + 'xiaomi-15-ultra', + 'Xiaomi 15 Ultra', + 'Xiaomi', + 24_990_000, + 25_990_000, + 4.7, + 9.0, + 512 + ), +]; + +const ITEM_ROWS = [ + { + id: ITEM_IPHONE, + item_key: 'apple:iphone-16-pro-max', + slug: 'apple-iphone-16-pro-max', + name: 'Apple iPhone 16 Pro Max', + brand: 'Apple', + model: 'iPhone 16 Pro Max', + category_id: CATEGORY_SMARTPHONES, + default_variant_id: V_IPHONE_256, + min_price: 29_990_000, + max_price: 36_990_000, + price_variant_count: 2, + merchant_count: 8, + listing_count: 10, + unknown_variant_listing_count: 0, + updated_at: '2026-11-20T00:00:00Z', + }, + { + id: ITEM_GALAXY, + item_key: 'samsung:galaxy-s25-ultra', + slug: 'samsung-galaxy-s25-ultra', + name: 'Samsung Galaxy S25 Ultra', + brand: 'Samsung', + model: 'Galaxy S25 Ultra', + category_id: CATEGORY_SMARTPHONES, + default_variant_id: V_GALAXY_256, + min_price: 27_990_000, + max_price: 33_990_000, + price_variant_count: 1, + merchant_count: 6, + listing_count: 7, + unknown_variant_listing_count: 0, + updated_at: '2026-11-20T00:00:00Z', + }, +]; + +const ITEM_VARIANT_ROWS = [ + { + id: V_IPHONE_256, + item_id: ITEM_IPHONE, + slug: 'apple-iphone-16-pro-max-256gb', + name: 'Apple iPhone 16 Pro Max 256GB', + variant_key: '256gb', + variant_name: '256GB', + sort_order: 1, + storage_gb: 256, + min_price: 29_990_000, + max_price: 31_990_000, + merchant_count: 5, + }, + { + id: V_IPHONE_512, + item_id: ITEM_IPHONE, + slug: 'apple-iphone-16-pro-max-512gb', + name: 'Apple iPhone 16 Pro Max 512GB', + variant_key: '512gb', + variant_name: '512GB', + sort_order: 2, + storage_gb: 512, + min_price: 34_990_000, + max_price: 36_990_000, + merchant_count: 3, + }, + { + id: V_GALAXY_256, + item_id: ITEM_GALAXY, + slug: 'samsung-galaxy-s25-ultra-256gb', + name: 'Samsung Galaxy S25 Ultra 256GB', + variant_key: '256gb', + variant_name: '256GB', + sort_order: 1, + storage_gb: 256, + min_price: 27_990_000, + max_price: 33_990_000, + merchant_count: 6, + }, +]; + +interface RecordedRpcCall { + path: string; + body: Record | null; +} + +let rpcCalls: RecordedRpcCall[] = []; +let itemTableReads = 0; +let itemsEndpointFailing = false; + +globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { + const url = new URL( + typeof input === 'string' + ? input + : input instanceof URL + ? input.href + : input.url + ); + const query = decodeURIComponent(url.search); + const parsedBody = + typeof init?.body === 'string' && init.body.trim().startsWith('{') + ? (JSON.parse(init.body) as Record) + : null; + + const json = (body: unknown, status = 200) => + new Response(init?.method === 'HEAD' ? null : JSON.stringify(body), { + status, + headers: { + 'content-type': 'application/json', + 'content-range': '0-0/*', + }, + }); + + if (Number(url.searchParams.get('offset') ?? 0) > 0) { + return json([]); + } + + switch (url.pathname) { + case '/rest/v1/categories': + return json([ + { + id: CATEGORY_SMARTPHONES, + parent_id: null, + name: 'Smartphones', + slug: 'smartphones', + icon: null, + display_order: 1, + }, + ]); + + case '/rest/v1/rpc/search_master_products': { + rpcCalls.push({ path: url.pathname, body: parsedBody }); + const rawLimit = Number(parsedBody?.p_limit ?? 200); + return json(CATALOG_ROWS.slice(0, Math.max(1, rawLimit))); + } + + case '/rest/v1/items': + itemTableReads += 1; + if (itemsEndpointFailing) { + return json({ code: '57014', message: 'statement timeout' }, 500); + } + return json(ITEM_ROWS); + + case '/rest/v1/master_products': + if (query.includes('select=id,item_id')) { + itemTableReads += 1; + return json([ + { id: V_IPHONE_512, item_id: ITEM_IPHONE }, + { id: V_IPHONE_256, item_id: ITEM_IPHONE }, + { id: V_GALAXY_256, item_id: ITEM_GALAXY }, + { id: V_XIAOMI_LONE, item_id: null }, + ]); + } + if (query.includes('item_id=in.') || query.includes('item_id=eq.')) { + itemTableReads += 1; + return json(ITEM_VARIANT_ROWS); + } + return json([]); + + default: + return json([]); + } +}) as typeof fetch; + +async function installCssExtensionStub() { + const nodeModule = await import('node:module'); + const extMap = ( + nodeModule.default as unknown as { + _extensions?: Record void>; + } + )._extensions; + if (extMap && !extMap['.css']) { + extMap['.css'] = (mod) => { + mod.exports = {}; + }; + } +} + +async function main() { + console.warn( + '🧪 homepage battles, Deals Item grouping & cached catalog reads suite (DOS/Rate#1808)' + ); + await installCssExtensionStub(); + + const { + buildHomePopularBattles, + loadHomeMasterProducts, + resolveHomeGroupedCatalog, + } = await import('../../app/[locale]/page'); + const { loadDealsCatalogProducts } = + await import('../../app/[locale]/deals/page'); + const { loadContactHeroProducts } = + await import('../../app/[locale]/contact-us/page'); + const { rankProductsForDealFinder } = + await import('../../components/deals/DealFinderWorkspace'); + const { buildRadarIndex, getRadarSignal } = await import('../deal-radar'); + const { toHeroProductSuggestions } = await import('../hero-products'); + const { + RATE_CACHE_TAGS, + canonicalizeCacheParams, + getCachedMasterProducts, + getMasterProducts, + resolveRevalidationTargets, + } = await import('../../server/rate'); + + // 1. Flag off: no Item reads, raw variants preserved on both / and /deals. + delete process.env.RATE_ITEM_MODEL; + itemTableReads = 0; + const flagOffHome = await resolveHomeGroupedCatalog(CATALOG_ROWS); + assert.equal(flagOffHome.gridProducts, CATALOG_ROWS); + assert.equal(itemTableReads, 0, 'flag off must not read Item tables'); + assert.equal( + flagOffHome.popularHomeBattles[0]?.versusSlug, + 'apple-iphone-16-pro-max-256gb-vs-apple-iphone-16-pro-max-512gb', + 'flag off pairs raw variant rows as legacy control baseline' + ); + + const flagOffDeals = await loadDealsCatalogProducts(async () => CATALOG_ROWS); + assert.equal(flagOffDeals, CATALOG_ROWS); + assert.equal(itemTableReads, 0, 'flag off /deals must not read Item tables'); + + // 2. RATE_ITEM_MODEL active ('item' and 'nullable_variant'): homepage 1v1 + // battles group by Item BEFORE pairing, never pair two storage variants of + // the same Item, and emit canonical Item slugs instead of variant slugs. + for (const mode of ['item', 'nullable_variant'] as const) { + process.env.RATE_ITEM_MODEL = mode; + + const { gridProducts, popularHomeBattles } = + await resolveHomeGroupedCatalog(CATALOG_ROWS); + + assert.equal( + gridProducts.length, + 3, + `[${mode}] 4 rows (2 variants of iPhone 16 Pro Max + S25 Ultra + lone Xiaomi) collapse to 3 Items` + ); + assert.deepEqual( + gridProducts.map((p) => p.name), + ['Apple iPhone 16 Pro Max', 'Samsung Galaxy S25 Ultra', 'Xiaomi 15 Ultra'] + ); + + assert.equal(popularHomeBattles.length, 3); + for (const battle of popularHomeBattles) { + assert.notEqual( + battle.productA.productHref, + battle.productB.productHref, + `[${mode}] battle must never pair two variants of the same Item` + ); + assert.ok( + !battle.versusSlug.includes('-256gb') && + !battle.versusSlug.includes('-512gb'), + `[${mode}] battle versusSlug must use canonical Item slugs, got ${battle.versusSlug}` + ); + } + + assert.deepEqual( + popularHomeBattles.map((b) => b.versusHref), + [ + '/compare/apple-iphone-16-pro-max-vs-samsung-galaxy-s25-ultra', + '/compare/apple-iphone-16-pro-max-vs-xiaomi-15-ultra', + '/compare/samsung-galaxy-s25-ultra-vs-xiaomi-15-ultra', + ] + ); + assert.equal( + popularHomeBattles[0]?.productA.name, + 'Apple iPhone 16 Pro Max' + ); + assert.equal( + popularHomeBattles[0]?.productB.name, + 'Samsung Galaxy S25 Ultra' + ); + + // Only two storage variants of a SINGLE Item exist: must yield 0 battles + // rather than pairing #1 vs #2 of the same Item (which 404s per #1764). + const sameItemOnly = await resolveHomeGroupedCatalog( + CATALOG_ROWS.slice(0, 2) + ); + assert.equal(sameItemOnly.gridProducts.length, 1); + assert.deepEqual( + sameItemOnly.popularHomeBattles, + [], + `[${mode}] two storage variants of one Item must not form a 1v1 battle` + ); + } + + // Fallback resilience on homepage if Item grouping throws unexpectedly. + const recoveredHome = await resolveHomeGroupedCatalog( + CATALOG_ROWS, + async () => { + throw new Error('synthetic item grouping failure'); + } + ); + assert.equal(recoveredHome.gridProducts, CATALOG_ROWS); + + // Edge cases in buildHomePopularBattles: + // - skips Items whose item_slug contains "-vs-" (would 404 on /compare/[versus]) + // - falls back to variant slug when item_slug fails isSafeVersusItemSlug (>120 chars / sentinel) + // - deduplicates nullable_variant collision between an Item row and an unlinked master sharing side slug + // - skips unparseable pairs where both sides end with "-vs" + const overlongItemSlug = `apple-iphone-${'x'.repeat(120)}`; + const edgeBattles = buildHomePopularBattles([ + { + ...makePhone( + 'edge-1', + 'bad-vs-item-256gb', + 'Bad VS Item', + 'Test', + 25_000_000, + 25_000_000, + 5.0, + 9.6 + ), + item_slug: 'bad-vs-slug', + } as MasterProduct, + { + ...makePhone( + 'edge-2', + 'xiaomi-15-ultra-256gb', + 'Xiaomi 15 Ultra', + 'Xiaomi', + 24_000_000, + 24_000_000, + 4.9, + 9.4 + ), + item_slug: 'xiaomi-15-ultra', + } as MasterProduct, + // Unlinked master colliding with Xiaomi 15 Ultra's item_slug during nullable_variant + makePhone( + 'edge-3', + 'xiaomi-15-ultra', + 'Xiaomi 15 Ultra Legacy', + 'Xiaomi', + 24_500_000, + 24_500_000, + 4.85, + 9.3 + ), + { + ...makePhone( + 'edge-4', + 'overlong-phone-256gb', + 'Overlong Slug Phone', + 'Apple', + 23_000_000, + 23_000_000, + 4.8, + 9.2 + ), + item_slug: overlongItemSlug, + } as MasterProduct, + ]); + assert.equal(edgeBattles.length, 1); + assert.equal( + edgeBattles[0]?.versusHref, + '/compare/overlong-phone-256gb-vs-xiaomi-15-ultra', + 'skips -vs- item_slug, deduplicates colliding side slugs, and falls back to variant slug for >120-char item_slug' + ); + + const unparseableVsSuffixBattles = buildHomePopularBattles([ + makePhone( + 'edge-vs-1', + 'alpha-vs', + 'Alpha VS', + 'Test', + 20_000_000, + 20_000_000, + 4.9, + 9.0 + ), + makePhone( + 'edge-vs-2', + 'beta-vs', + 'Beta VS', + 'Test', + 21_000_000, + 21_000_000, + 4.8, + 8.9 + ), + ]); + assert.deepEqual( + unparseableVsSuffixBattles, + [], + 'two slugs ending in -vs have no parseable versus URL in either order and must be skipped' + ); + + // 3. /deals applies groupCatalogRowsByItem consistently with category pages + // under RATE_ITEM_MODEL ('item' and 'nullable_variant'), collapsing storage + // variants while preserving per-variant cross-platform spreadSavingsVnd, + // Radar badges, and canonical Item links. + for (const mode of ['item', 'nullable_variant'] as const) { + process.env.RATE_ITEM_MODEL = mode; + rpcCalls = []; + const dealsProducts = await loadDealsCatalogProducts(); + assert.equal(rpcCalls.length, 1); + assert.deepEqual(rpcCalls[0]?.body, { + p_sort: 'rating_desc', + p_limit: 100, + p_offset: 0, + }); + assert.equal( + dealsProducts.length, + 3, + `[${mode}] /deals must group duplicate storage variants into one Item row` + ); + + const rankedDeals = rankProductsForDealFinder(dealsProducts, { + categoryFilter: 'all', + persona: 'default', + maxBudgetVnd: 65_000_000, + searchQuery: '', + }); + assert.equal(rankedDeals.length, 3); + assert.deepEqual( + rankedDeals.map((d) => ({ + name: d.name, + slug: d.slug, + href: d.productHref, + minPrice: d.minPrice, + spreadSavingsVnd: d.spreadSavingsVnd, + merchantCount: d.merchantCount, + })), + [ + { + name: 'Apple iPhone 16 Pro Max', + slug: 'apple-iphone-16-pro-max-256gb', + href: '/smartphones/apple-iphone-16-pro-max', + minPrice: 29_990_000, + spreadSavingsVnd: 2_000_000, + merchantCount: 8, + }, + { + name: 'Samsung Galaxy S25 Ultra', + slug: 'samsung-galaxy-s25-ultra-256gb', + href: '/smartphones/samsung-galaxy-s25-ultra', + minPrice: 27_990_000, + spreadSavingsVnd: 6_000_000, + merchantCount: 6, + }, + { + name: 'Xiaomi 15 Ultra', + slug: 'xiaomi-15-ultra', + href: '/smartphones/xiaomi-15-ultra', + minPrice: 24_990_000, + spreadSavingsVnd: 1_000_000, + merchantCount: 3, + }, + ] + ); + + // Uniform-price storage variants (0 cross-store spread on each size) must + // report spreadSavingsVnd = 0 rather than 512GB price minus 256GB price. + const uniformSizeDeals = await loadDealsCatalogProducts(async () => [ + makePhone( + V_IPHONE_256, + 'apple-iphone-16-pro-max-256gb', + 'Apple iPhone 16 Pro Max 256GB', + 'Apple', + 29_990_000, + 29_990_000, + 4.9, + 9.4, + 256 + ), + makePhone( + V_IPHONE_512, + 'apple-iphone-16-pro-max-512gb', + 'Apple iPhone 16 Pro Max 512GB', + 'Apple', + 34_990_000, + 34_990_000, + 4.9, + 9.4, + 512 + ), + ]); + const uniformRanked = rankProductsForDealFinder(uniformSizeDeals, { + categoryFilter: 'all', + persona: 'default', + maxBudgetVnd: 65_000_000, + searchQuery: '', + }); + assert.equal(uniformRanked.length, 1); + assert.equal( + uniformRanked[0]?.spreadSavingsVnd, + 0, + `[${mode}] storage capacity delta must not be reported as cross-platform savings` + ); + + // Variant-keyed Deal Radar signal still attaches to the Item-grouped deal card. + const radarIndex = buildRadarIndex({ + generatedAt: '2026-11-21T00:00:00Z', + categoryFilter: 'all', + totalClustersAnalyzed: 3, + totalOffersAnalyzed: 10, + totalMerchantsAnalyzed: 8, + genuineDeals: [ + { + id: 'offer-1', + verdict: 'genuine_all_time_low', + verdictBadgeVi: 'Đáy giá đa sàn', + productSlug: 'apple-iphone-16-pro-max-256gb', + productName: 'Apple iPhone 16 Pro Max 256GB', + productHref: '/smartphones/apple-iphone-16-pro-max', + imageUrl: null, + brand: 'Apple', + categorySlug: 'smartphones', + platform: 'shopee', + merchantId: 'm-1', + merchantName: 'Apple Flagship Store', + isOfficialMall: true, + merchantRiskScore: 5, + priceCurrent: 29_990_000, + priceOriginal: 32_990_000, + marketMedianPrice: 33_490_000, + marketMinPrice: 29_990_000, + marketMaxPrice: 36_990_000, + savingsVsMedianVnd: 3_500_000, + savingsVsMedianPct: 10.5, + marketOfferCount: 4, + claimedDiscountPct: 9.1, + fakeMarkupPct: 0, + jreviewsScore10: 9.4, + offerUrl: 'https://shopee.vn/item', + explanationVi: 'Thấp hơn trung vị 10.5%', + }, + ], + fakeSaleTraps: [], + priceSpreadLeaders: [], + }); + assert.equal( + getRadarSignal(radarIndex, rankedDeals[0]!.slug).verdict, + 'genuine_deal' + ); + } + + // /deals fallback when Item endpoint errors: returns ungrouped rows cleanly, + // while a primary catalog read failure still rejects so ISR keeps the last good page. + process.env.RATE_ITEM_MODEL = 'item'; + itemsEndpointFailing = true; + const fallbackDeals = await loadDealsCatalogProducts(async () => [ + makePhone( + '99999999-9999-4999-8999-999999999991', + 'fallback-phone-128gb', + 'Fallback Phone 128GB', + 'Apple', + 19_990_000, + 19_990_000, + 4.6, + 8.8, + 128 + ), + ]); + assert.equal(fallbackDeals.length, 1); + assert.equal(fallbackDeals[0]?.slug, 'fallback-phone-128gb'); + itemsEndpointFailing = false; + + await assert.rejects( + () => + loadDealsCatalogProducts(async () => { + throw new Error('supabase catalog down'); + }), + /supabase catalog down/ + ); + + delete process.env.RATE_ITEM_MODEL; + + // 4. Cached catalog helper usage on / and /contact-us: default helper wiring, + // RPC parameter parity with getCachedMasterProducts, canonical cache key + // serialization, and rate:products revalidation tag participation. + const homeCapturedArgs: unknown[] = []; + await loadHomeMasterProducts(async (params) => { + homeCapturedArgs.push(params); + return []; + }); + assert.deepEqual(homeCapturedArgs, [{ categorySlug: 'all', limit: 24 }]); + + const contactCapturedArgs: unknown[] = []; + await loadContactHeroProducts(async (params) => { + contactCapturedArgs.push(params); + return []; + }); + assert.deepEqual(contactCapturedArgs, [{ limit: 6 }]); + + rpcCalls = []; + const homeDefaultRows = await loadHomeMasterProducts(); + const homeDirectCachedRows = await getCachedMasterProducts({ + categorySlug: 'all', + limit: 24, + }); + assert.equal(rpcCalls.length, 2); + assert.deepEqual(rpcCalls[0]?.body, { + p_sort: 'default', + p_limit: 24, + p_offset: 0, + }); + assert.deepEqual(rpcCalls[0]?.body, rpcCalls[1]?.body); + assert.deepEqual(homeDefaultRows, homeDirectCachedRows); + assert.equal( + canonicalizeCacheParams({ categorySlug: 'all', limit: 24 }), + '{"categorySlug":"all","limit":24}' + ); + + rpcCalls = []; + const contactCachedRows = await loadContactHeroProducts(); + const contactDirectCachedRows = await getCachedMasterProducts({ limit: 6 }); + assert.equal(rpcCalls.length, 2); + assert.deepEqual(rpcCalls[0]?.body, { + p_sort: 'default', + p_limit: 6, + p_offset: 0, + }); + assert.deepEqual(rpcCalls[0]?.body, rpcCalls[1]?.body); + assert.deepEqual(contactCachedRows, contactDirectCachedRows); + assert.equal(canonicalizeCacheParams({ limit: 6 }), '{"limit":6}'); + + const contactUncachedControl = await getMasterProducts({ limit: 6 }); + assert.deepEqual( + toHeroProductSuggestions(contactCachedRows), + toHeroProductSuggestions(contactUncachedControl), + 'switching /contact-us to getCachedMasterProducts({ limit: 6 }) preserves hero suggestions 100%' + ); + + const revalidation = resolveRevalidationTargets({ + slugs: ['apple-iphone-16-pro-max-256gb'], + }); + assert.ok( + revalidation.tags.includes(RATE_CACHE_TAGS.PRODUCTS), + 'product slug revalidation must emit rate:products so cached / and /contact-us reads invalidate' + ); + + // 5. Source wiring contracts on /, /deals, and /contact-us. + const homeSrc = readFileSync( + join(testDir, '../../app/[locale]/page.tsx'), + 'utf8' + ); + const dealsSrc = readFileSync( + join(testDir, '../../app/[locale]/deals/page.tsx'), + 'utf8' + ); + const contactSrc = readFileSync( + join(testDir, '../../app/[locale]/contact-us/page.tsx'), + 'utf8' + ); + + assert.ok( + !/\bgetMasterProducts\b/.test(homeSrc) && + homeSrc.includes('getCachedMasterProducts') && + homeSrc.includes('groupCatalogRowsByItem') && + homeSrc.includes('loadHomeMasterProducts()') && + homeSrc.includes('resolveHomeGroupedCatalog(masterProducts)') && + !/buildPopular1v1BattlesForCategory\(\s*masterProducts\b/.test(homeSrc), + 'app/[locale]/page.tsx must read via getCachedMasterProducts and group rows by Item before building 1v1 battles' + ); + + assert.ok( + !/\bgetMasterProducts\b/.test(dealsSrc) && + dealsSrc.includes('getCachedMasterProducts') && + dealsSrc.includes('groupCatalogRowsByItem') && + dealsSrc.includes('loadDealsCatalogProducts()'), + 'app/[locale]/deals/page.tsx must read via getCachedMasterProducts and group rows via groupCatalogRowsByItem' + ); + + assert.ok( + !/\bgetMasterProducts\b/.test(contactSrc) && + contactSrc.includes('getCachedMasterProducts') && + contactSrc.includes('loadContactHeroProducts()'), + 'app/[locale]/contact-us/page.tsx must read catalog rows via getCachedMasterProducts' + ); + + console.warn( + ' ✅ homepage battles, Deals Item grouping & cached catalog reads suite passed' + ); +} + +main().catch((err: unknown) => { + console.error(err); + process.exit(1); +}); From 500aa46c2b102b77a74b07bbf3639333956033ce Mon Sep 17 00:00:00 2001 From: JOY <5027251+JOY@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:19:00 +0700 Subject: [PATCH 05/24] fix(web): serve dotted merchant, product, and versus slugs (#1817) * fix(web): serve dotted merchant, product, and versus slugs Route single-segment dotted dynamic paths under /merchants/:id, /compare/:versus, and /:category/:slug (with or without /vi, /en, /cs) through proxy.ts with per-request CSP nonces instead of rewriting them to /vi/__not-found or applying the static non-page CSP header. Closes #1807 * fix(web): align proxy matcher case-sensitivity and slug tokens Match mixed-case prefixes in proxy.ts config.matcher to stay in exact lockstep with routes-manifest.json caseSensitive: false, and restrict dotted dynamic page segments to valid slug characters so malformed or percent-encoded dotted probes fall through to /vi/__not-found. Refs #1807 * chore(web): merge origin/dev into fix/1807-dotted-slugs --- apps/web/e2e/dotted-paths.spec.ts | 116 ++++++++- apps/web/next.config.mjs | 16 +- apps/web/package.json | 2 +- apps/web/src/lib/__tests__/csp.test.ts | 46 +++- apps/web/src/lib/__tests__/proxy.test.ts | 311 +++++++++++++++++++++++ apps/web/src/proxy.ts | 13 +- 6 files changed, 483 insertions(+), 21 deletions(-) create mode 100644 apps/web/src/lib/__tests__/proxy.test.ts diff --git a/apps/web/e2e/dotted-paths.spec.ts b/apps/web/e2e/dotted-paths.spec.ts index d6409880..79f0f0ea 100644 --- a/apps/web/e2e/dotted-paths.spec.ts +++ b/apps/web/e2e/dotted-paths.spec.ts @@ -1,14 +1,18 @@ import { expect, test } from '@playwright/test'; -// Unknown single-segment paths with a dot are a plain 404, not a server error +// Unknown static-asset paths with a dot are a plain 404, not a server error // (DOS/Rate#732). Browsers and crawlers ask for several of them on their own. const MISSING = [ '/foo.png', + '/nonexistent.png', '/a.b', '/ads.txt', '/manifest.json', '/site.webmanifest', '/browserconfig.xml', + '/unknown/asset.js', + '/compare/100%25.png', + '/smartphones/foo..bar', ]; for (const path of MISSING) { @@ -54,3 +58,113 @@ for (const locale of ['vi', 'en'] as const) { await expect(page.locator('h1')).toHaveText('Không tìm thấy trang'); }); } + +// Dotted dynamic page routes (/merchants/:id, /compare/:versus, and +// /:category/:slug) pass through proxy.ts and render their target route (or its +// localized route-level not-found state when absent) with page CSP nonces +// instead of being rewritten to /vi/__not-found (DOS/Rate#1807). +const DOTTED_DYNAMIC_PAGES = [ + '/merchants/anker.vn', + '/merchants/viettel.store', + '/smartphones/nokia-8.1', + '/compare/nokia-7.2-vs-nokia-8.1', +]; + +for (const path of DOTTED_DYNAMIC_PAGES) { + test(`${path} routes through proxy with page CSP headers`, async ({ + request, + }) => { + const res = await request.get(path, { maxRedirects: 0 }); + expect(res.status(), path).toBe(404); + expect(res.headers()['content-security-policy'], path).toBeUndefined(); + const reportOnly = + res.headers()['content-security-policy-report-only'] ?? ''; + expect(reportOnly, path).toMatch(/script-src [^;]*'nonce-[A-Za-z0-9+/=]+'/); + }); +} + +test('unsorted dotted versus slug reaches VersusComparePage and 308s to canonical order', async ({ + request, + baseURL, +}) => { + const res = await request.get('/compare/nokia-8.1-vs-nokia-7.2', { + maxRedirects: 0, + }); + expect(res.status()).toBe(308); + const targets = new Set( + res + .headersArray() + .filter(({ name }) => name.toLowerCase() === 'location') + .map(({ value }) => new URL(value, baseURL).pathname) + ); + expect([...targets]).toEqual(['/compare/nokia-7.2-vs-nokia-8.1']); + expect(res.headers()['content-security-policy']).toBeUndefined(); +}); + +for (const [prefixedPath, cleanPath, locale, expectedH1, expectedTitle] of [ + [ + '/en/merchants/anker.vn', + '/merchants/anker.vn', + 'en', + 'Page not found', + 'Merchant not found | Rate.vn', + ], + [ + '/EN/merchants/anker.vn', + '/merchants/anker.vn', + 'en', + 'Page not found', + 'Merchant not found | Rate.vn', + ], + [ + '/cs/merchants/anker.vn', + '/merchants/anker.vn', + 'cs', + 'Stránka nenalezena', + 'Obchod nenalezen | Rate.vn', + ], + [ + '/en/smartphones/nokia-8.1', + '/smartphones/nokia-8.1', + 'en', + 'Page not found', + 'Product not found | Rate.vn', + ], + [ + '/cs/compare/nokia-7.2-vs-nokia-8.1', + '/compare/nokia-7.2-vs-nokia-8.1', + 'cs', + 'Stránka nenalezena', + 'Stránka nenalezena | Rate.vn', + ], +] as const) { + test(`${prefixedPath} strips locale via proxy and renders localized route state`, async ({ + page, + request, + baseURL, + }) => { + const redirectRes = await request.get(prefixedPath, { maxRedirects: 0 }); + expect(redirectRes.status()).toBe(308); + const targets = new Set( + redirectRes + .headersArray() + .filter(({ name }) => name.toLowerCase() === 'location') + .map(({ value }) => new URL(value, baseURL).pathname) + ); + expect([...targets]).toEqual([cleanPath]); + expect(redirectRes.headers()['set-cookie'] ?? '').toContain( + `NEXT_LOCALE=${locale}` + ); + expect(redirectRes.headers()['content-security-policy']).toBeUndefined(); + + const pageRes = await page.goto(prefixedPath); + expect(pageRes?.status()).toBe(404); + expect(new URL(page.url()).pathname).toBe(cleanPath); + await expect(page.locator('html')).toHaveAttribute('lang', locale); + await expect(page.locator('h1')).toHaveText(expectedH1); + await expect(page).toHaveTitle(expectedTitle); + await expect( + page.locator('meta[http-equiv="Content-Security-Policy"]') + ).toHaveAttribute('content', /script-src 'self' 'unsafe-inline'/); + }); +} diff --git a/apps/web/next.config.mjs b/apps/web/next.config.mjs index cbb7341c..b654cb91 100644 --- a/apps/web/next.config.mjs +++ b/apps/web/next.config.mjs @@ -157,9 +157,11 @@ const nextConfig = { // Every path the proxy does not serve (its matcher in src/proxy.ts): // pages get it in a tag (PAGE_META_CSP). Vercel copies config // headers onto the request, and an enforcing policy there hides the - // report-only one Next.js reads the nonce from (DOS/Rate#1345). + // report-only one Next.js reads the nonce from (DOS/Rate#1345). Dotted + // dynamic page slugs stay with the proxy (DOS/Rate#1807). { - source: "/:path((?:_next|_vercel|api|monitoring|sitemap|.*\\.).*)", + source: + "/:path((?:_next|_vercel|api|monitoring|sitemap|(?!(?:(?:cs|en|vi)/)?(?:merchants|compare|smartphones|laptops|tablets|smartwatches|earbuds)/[a-zA-Z0-9][a-zA-Z0-9_-]*(?:[.][a-zA-Z0-9_-]+)*/?$)[^.]*\\.).*)", headers: [ { key: "Content-Security-Policy", @@ -199,11 +201,13 @@ const nextConfig = { // Sent to a path no route matches, it gets app/not-found.tsx rendered // on the server with status 404 instead. These rewrites run after // public files and static routes (robots.txt, icon.png, sitemap.xml), - // so those still load; Next and Vercel assets and the API are left - // alone. Only a dot in the last segment counts: "/a.b/smartphones" - // still lands on [locale] and gets the shell (status 404). + // so those still load; Next and Vercel assets, the API and dotted + // dynamic page slugs (/merchants/:id, /compare/:versus, /:category/:slug, + // DOS/Rate#1807) are left alone. Only a dot in the last segment counts: + // "/a.b/smartphones" still lands on [locale] and gets the shell (status 404). { - source: "/:path((?!_next/|_vercel/|api/)(?:.*/)?[^/]*[.][^/]*)", + source: + "/:path((?!_next/|_vercel/|api/|(?:(?:cs|en|vi)/)?(?:merchants|compare|smartphones|laptops|tablets|smartwatches|earbuds)/[a-zA-Z0-9][a-zA-Z0-9_-]*(?:[.][a-zA-Z0-9_-]+)*/?$)(?:[^/]*/)*[^/.]*[.][^/]*)", destination: "/vi/__not-found", }, ] diff --git a/apps/web/package.json b/apps/web/package.json index 4cb25eab..830346b2 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -8,7 +8,7 @@ "lint": "eslint . --max-warnings 0", "preview": "next build && next start", "start": "next start", - "test": "pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/server/rate/__tests__/core-rate-architecture.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/server/rate/__tests__/item-model.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/server/rate/__tests__/item-directory.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/server/rate/__tests__/compare-selection.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/app/api/v1/rate/__tests__/write-paths.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/app/api/v1/rate/__tests__/spec-scope.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/app/api/v1/rate/__tests__/versus-parity.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/app/api/v1/rate/__tests__/item-fields.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/logger.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/review-display.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/review-form-v2.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/review-value-display.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/concurrency.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/merchant-profile-reads.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/merchant-review-risk.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/item-directory.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/home-deals-item-grouping.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/watchlist-sizes.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/item-compare.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-variant-links.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/compare-params.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/catalog-listing.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/discount-verdict-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-detail-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-competitors.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/analyzer-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/sold-count.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/content-lang.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/image-url.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/client-messages.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/seo.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/offer-plausibility.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-market-stats.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/sitemap-lastmod.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/leaderboard-badge-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/leaderboard-criteria.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/trust-depth-v2.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/rating-score.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/rating-display.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/focus-trap.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/display-truth.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/spec-template.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-spec-rows.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/sale-state.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/csp.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/device-images.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/render-timing.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/model-match.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/developers-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/plural-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/locale-format.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/paged-reads.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/server-timing.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/hero-products.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/components/elementary/__tests__/locale-switcher-a11y.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/components/compare/__tests__/quick-toggle-a11y.test.ts", + "test": "pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/server/rate/__tests__/core-rate-architecture.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/server/rate/__tests__/item-model.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/server/rate/__tests__/item-directory.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/server/rate/__tests__/compare-selection.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/app/api/v1/rate/__tests__/write-paths.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/app/api/v1/rate/__tests__/spec-scope.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/app/api/v1/rate/__tests__/versus-parity.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/app/api/v1/rate/__tests__/item-fields.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/logger.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/review-display.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/review-form-v2.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/review-value-display.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/concurrency.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/merchant-profile-reads.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/merchant-review-risk.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/item-directory.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/home-deals-item-grouping.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/watchlist-sizes.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/item-compare.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-variant-links.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/compare-params.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/catalog-listing.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/discount-verdict-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-detail-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-competitors.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/analyzer-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/sold-count.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/content-lang.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/image-url.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/client-messages.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/seo.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/offer-plausibility.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-market-stats.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/sitemap-lastmod.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/leaderboard-badge-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/leaderboard-criteria.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/trust-depth-v2.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/rating-score.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/rating-display.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/focus-trap.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/display-truth.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/spec-template.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/product-spec-rows.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/sale-state.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/csp.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/proxy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/device-images.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/render-timing.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/model-match.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/developers-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/plural-copy.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/locale-format.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/paged-reads.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/server-timing.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/lib/__tests__/hero-products.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/components/elementary/__tests__/locale-switcher-a11y.test.ts && pnpm --filter @repo/crawlers exec tsx --tsconfig ../web/tsconfig.test.json ../web/src/components/compare/__tests__/quick-toggle-a11y.test.ts", "typecheck": "tsc --noEmit" }, "browserslist": [ diff --git a/apps/web/src/lib/__tests__/csp.test.ts b/apps/web/src/lib/__tests__/csp.test.ts index ea419aed..00b72342 100644 --- a/apps/web/src/lib/__tests__/csp.test.ts +++ b/apps/web/src/lib/__tests__/csp.test.ts @@ -1,5 +1,8 @@ +import { AsyncLocalStorage } from 'node:async_hooks'; import assert from 'node:assert/strict'; +Object.assign(globalThis, { AsyncLocalStorage }); + // The report-only Content Security Policy (DOS/Rate#1311): a nonce per request on // both the request (for Next.js) and the response, a script-src with no inline // allowance, and a report endpoint that only logs. @@ -192,7 +195,7 @@ async function main() { 'utf8' ); const rule = nextConfig.match( - /source: ("[^"]+"),\s*headers: \[\s*\{\s*key: "Content-Security-Policy",\s*value:\s*("[^"]+")/ + /source:\s*("[^"]+"),\s*headers: \[\s*\{\s*key: "Content-Security-Policy",\s*value:\s*("[^"]+")/ ); assert.ok(rule, 'next.config.mjs has one Content-Security-Policy rule'); assert.equal(JSON.parse(rule[2]!), STATIC_CSP, 'the same static policy'); @@ -206,33 +209,58 @@ async function main() { sensitive: false, delimiter: '/', }) as RegExp; + const { getMiddlewareMatchers } = (await import( + 'next/dist/build/analysis/get-page-static-info' as string + )) as { + getMiddlewareMatchers: ( + matchers: string[], + options: { i18n?: undefined } + ) => Array<{ regexp: string }>; + }; const { config: proxyConfig } = await import('../../proxy'); - const proxied = proxyConfig.matcher.map( - (m) => - pathToRegexp(m, [], { - strict: true, - sensitive: false, - delimiter: '/', - }) as RegExp - ); + // Next.js evaluates middleware matchers without /i (getMiddlewareRouteMatcher), + // whereas routes-manifest.json headers run with caseSensitive: false (#1807). + const proxied = getMiddlewareMatchers(proxyConfig.matcher, { + i18n: undefined, + }).map((m) => new RegExp(m.regexp)); for (const p of [ '/', '/smartphones', '/smartphones/iphone-16-pro-max-256gb', + '/smartphones/nokia-8.1', + '/en/smartphones/nokia-8.1', + '/Vi/smartphones/nokia-8.1', + '/merchants/anker.vn', + '/merchants/viettel.store', + '/en/merchants/anker.vn', + '/EN/merchants/anker.vn', + '/cs/merchants/anker.vn', + '/Merchants/anker.vn', + '/compare/nokia-7.2-vs-nokia-8.1', + '/en/compare/nokia-7.2-vs-nokia-8.1', + '/CS/compare/nokia-7.2-vs-nokia-8.1', '/inspect', '/compare', '/api/health', + '/API/health', '/_next/static/chunks/a.js', + '/_NEXT/static/chunks/a.js', + '/_VERCEL/insights/script.js', '/robots.txt', '/robots-txt', '/favicon.ico', '/apple-touch-icon.png', '/apisomething', + '/APIsomething', '/smartphones/', '/en/smartphones', '/sitemap.xml', + '/Sitemap', '/monitoring', + '/Monitoring', '/img/logo.png', + '/unknown/asset.js', + '/en/foo.png', ]) { const byProxy = proxied.some((re) => re.test(p)); assert.notEqual( diff --git a/apps/web/src/lib/__tests__/proxy.test.ts b/apps/web/src/lib/__tests__/proxy.test.ts new file mode 100644 index 00000000..93846cb5 --- /dev/null +++ b/apps/web/src/lib/__tests__/proxy.test.ts @@ -0,0 +1,311 @@ +import { AsyncLocalStorage } from 'node:async_hooks'; +import assert from 'node:assert/strict'; +import fs from 'node:fs'; +import path from 'node:path'; + +// Next's build-time matcher compiler reads AsyncLocalStorage from globalThis +// (normally populated by next/dist/server/node-environment). +Object.assign(globalThis, { AsyncLocalStorage }); + +// Dotted dynamic page routes (/merchants/:id, /compare/:versus, and +// /:category/:slug, with or without /vi|/en|/cs prefixes) must pass through +// src/proxy.ts for locale routing and per-request CSP nonces instead of +// skipping the proxy, getting the static non-page CSP header, or being +// rewritten to /vi/__not-found (DOS/Rate#1807). Unknown static asset paths +// (/foo.png, /ads.txt, /unknown/asset.js) keep their #732/#1463 404 rewrite. + +process.env.NEXT_PUBLIC_SUPABASE_URL = 'https://abc.supabase.co'; + +async function main() { + const { NextRequest } = await import('next/server'); + const { pathToRegexp } = await import( + 'next/dist/compiled/path-to-regexp' as string + ); + const { getMiddlewareMatchers } = (await import( + 'next/dist/build/analysis/get-page-static-info' as string + )) as { + getMiddlewareMatchers: ( + matchers: string[], + options: { i18n?: undefined } + ) => Array<{ regexp: string; originalSource: string }>; + }; + + const { CSP_HEADER } = await import('../csp'); + const { config: proxyConfig, localeProxy } = await import('../../proxy'); + + const nextConfigSrc = fs.readFileSync( + path.join(__dirname, '../../../next.config.mjs'), + 'utf8' + ); + const cspRule = nextConfigSrc.match( + /source:\s*("[^"]+"),\s*headers: \[\s*\{\s*key: "Content-Security-Policy"/ + ); + assert.ok(cspRule, 'next.config.mjs defines the static CSP source'); + const notFoundRewriteRule = nextConfigSrc.match( + /source:\s*("[^"]+"),\s*destination:\s*"\/vi\/__not-found"/ + ); + assert.ok( + notFoundRewriteRule, + 'next.config.mjs defines the dotted-path /vi/__not-found rewrite' + ); + + // Next.js compiles routes-manifest.json custom routes with caseSensitive: false + // (/i), whereas functions-config-manifest.json middleware matchers execute via + // new RegExp(matcher.regexp) with no /i flag. + const compileCustomRoute = (source: string) => + pathToRegexp(source, [], { + strict: true, + sensitive: false, + delimiter: '/', + }) as RegExp; + + const middlewareRegexes = getMiddlewareMatchers(proxyConfig.matcher, { + i18n: undefined, + }).map((m) => new RegExp(m.regexp)); + const staticCspRegex = compileCustomRoute(JSON.parse(cspRule[1]!)); + const notFoundRewriteRegex = compileCustomRoute( + JSON.parse(notFoundRewriteRule[1]!) + ); + + const byMiddleware = (p: string) => + middlewareRegexes.some((re) => re.test(p)); + + const dottedDynamicPagePaths = [ + '/merchants/anker.vn', + '/merchants/Anker.VN', + '/merchants/viettel.store', + '/merchants/didongthongminhchinhhang.vn', + '/merchants/samsung.official.vn', + '/merchants/shop_1.vn', + '/merchants/anker.vn/', + '/Merchants/anker.vn', + '/vi/merchants/anker.vn', + '/en/merchants/anker.vn', + '/EN/merchants/anker.vn', + '/cs/merchants/anker.vn', + '/CS/Merchants/anker.vn', + '/smartphones/nokia-8.1', + '/laptops/thinkpad-x1-carbon-11.2', + '/tablets/ipad-pro-12.9', + '/smartwatches/watch-gt4-46.0mm', + '/earbuds/airpods-pro-bt5.3', + '/vi/smartphones/nokia-8.1', + '/Vi/smartphones/nokia-8.1', + '/en/laptops/thinkpad-x1-carbon-11.2', + '/cs/tablets/ipad-pro-12.9', + '/en/smartwatches/watch-gt4-46.0mm', + '/cs/earbuds/airpods-pro-bt5.3', + '/compare/nokia-7.2-vs-nokia-8.1', + '/vi/compare/nokia-7.2-vs-nokia-8.1', + '/en/compare/nokia-7.2-vs-nokia-8.1', + '/cs/compare/nokia-7.2-vs-nokia-8.1', + '/CS/compare/nokia-7.2-vs-nokia-8.1', + ]; + + for (const p of dottedDynamicPagePaths) { + assert.equal( + byMiddleware(p), + true, + `${p} must match compiled Next.js middleware matcher` + ); + assert.equal( + staticCspRegex.test(p), + false, + `${p} must not get the static non-page CSP header` + ); + assert.equal( + notFoundRewriteRegex.test(p), + false, + `${p} must not be rewritten to /vi/__not-found` + ); + } + + const staticAssetFourOhFourPaths = [ + '/foo.png', + '/nonexistent.png', + '/ads.txt', + '/manifest.json', + '/site.webmanifest', + '/browserconfig.xml', + '/a.b', + '/unknown/asset.js', + '/sitemap_index.xml', + '/en/foo.png', + '/EN/foo.png', + '/vi/ads.txt', + '/cs/unknown/asset.js', + '/merchants/.', + '/merchants/..', + '/merchants/.env', + '/merchants/anker.', + '/merchants/foo..bar', + '/merchants/-bad.vn', + '/smartphones/.git', + '/smartphones/foo..bar', + '/smartphones/nokia.', + '/compare/..hidden', + '/compare/100%25.png', + '/compare/nokia-8.1%25-vs-nokia-7.2', + '/merchants/anker.vn/logo.png', + '/smartphones/nokia-8.1/photo.jpg', + '/compare/nokia-7.2-vs-nokia-8.1/extra.json', + ]; + + for (const p of staticAssetFourOhFourPaths) { + assert.equal( + byMiddleware(p), + false, + `${p} must skip compiled Next.js middleware matcher` + ); + assert.equal( + staticCspRegex.test(p), + true, + `${p} must carry the static CSP header` + ); + assert.equal( + notFoundRewriteRegex.test(p), + true, + `${p} must rewrite to /vi/__not-found when no public/static file matches` + ); + } + + const reservedNonPagePaths = [ + '/_next/static/chunks/main.js', + '/_NEXT/static/chunks/main.js', + '/_vercel/insights/script.js', + '/_VERCEL/insights/script.js', + '/api/health', + '/API/health', + '/api/v1/rate/openapi.json', + '/api/v1/rate/merchants/anker.vn', + '/API/v1/rate/merchants/anker.vn', + '/api/v1/rate/products/nokia-8.1', + '/Monitoring', + '/Sitemap', + '/APIsomething', + '/a.b/smartphones', + '/merchants/a.b/extra', + ]; + + for (const p of reservedNonPagePaths) { + assert.equal(byMiddleware(p), false, `${p} must skip proxy.ts`); + assert.equal( + staticCspRegex.test(p), + true, + `${p} must carry the static CSP header` + ); + assert.equal( + notFoundRewriteRegex.test(p), + false, + `${p} must not be rewritten to /vi/__not-found` + ); + } + + // Runtime localeProxy behavior on unprefixed dotted dynamic routes. + for (const [p, cookieLocale, expectedLocale] of [ + ['/merchants/anker.vn', undefined, 'vi'], + ['/merchants/anker.vn', 'en', 'en'], + ['/merchants/viettel.store', 'cs', 'cs'], + ['/smartphones/nokia-8.1', 'en', 'en'], + ['/compare/nokia-7.2-vs-nokia-8.1', 'cs', 'cs'], + ] as const) { + const headers: Record = { + host: 'rate.vn', + 'x-forwarded-proto': 'https', + 'content-security-policy': "script-src 'nonce-attacker'", + }; + if (cookieLocale) headers.cookie = `NEXT_LOCALE=${cookieLocale}`; + + const res = localeProxy( + new NextRequest(`https://rate.vn${p}`, { headers }) + ); + assert.equal(res.status, 200, `${p} renders without redirect`); + assert.equal( + res.headers.get('x-middleware-rewrite'), + `https://rate.vn/${expectedLocale}${p}`, + `${p} rewrites to /${expectedLocale}${p}` + ); + const reportOnly = res.headers.get(CSP_HEADER); + assert.ok(reportOnly, `${p} carries report-only CSP header`); + assert.match( + reportOnly, + /script-src 'self' 'nonce-[A-Za-z0-9+/=]+' 'sha256-[A-Za-z0-9+/=]+' 'strict-dynamic'/ + ); + assert.equal( + res.headers.get(`x-middleware-request-${CSP_HEADER.toLowerCase()}`), + reportOnly, + `${p} forwards the same report-only CSP nonce to Next.js` + ); + assert.equal( + res.headers.get('content-security-policy'), + null, + `${p} sets no enforcing CSP header on the response` + ); + assert.equal( + res.headers.get('x-middleware-request-content-security-policy'), + null, + `${p} strips any client-supplied enforcing CSP header` + ); + } + + // Runtime localeProxy behavior on locale-prefixed dotted dynamic routes. + for (const [prefixed, expectedPath, expectedCookie] of [ + ['/vi/merchants/anker.vn', '/merchants/anker.vn', 'vi'], + ['/en/merchants/anker.vn', '/merchants/anker.vn', 'en'], + ['/cs/merchants/anker.vn', '/merchants/anker.vn', 'cs'], + ['/EN/merchants/anker.vn', '/merchants/anker.vn', 'en'], + [ + '/en/smartphones/nokia-8.1?tab=specs', + '/smartphones/nokia-8.1?tab=specs', + 'en', + ], + [ + '/cs/compare/nokia-7.2-vs-nokia-8.1', + '/compare/nokia-7.2-vs-nokia-8.1', + 'cs', + ], + ] as const) { + const res = localeProxy( + new NextRequest(`https://rate.vn${prefixed}`, { + headers: { host: 'rate.vn', 'x-forwarded-proto': 'https' }, + }) + ); + assert.equal(res.status, 308, `${prefixed} 308-redirects to strip locale`); + assert.equal( + res.headers.get('location'), + `https://rate.vn${expectedPath}`, + `${prefixed} redirects to ${expectedPath}` + ); + assert.equal( + res.cookies.get('NEXT_LOCALE')?.value, + expectedCookie, + `${prefixed} persists NEXT_LOCALE=${expectedCookie}` + ); + } + + // Linear-time guard against ReDoS across all compiled matchers. + for (const input of [ + `/${'a'.repeat(20_000)}`, + `/merchants/${'a.'.repeat(10_000)}/c`, + `/${'a.'.repeat(10_000)}/c`, + `/merchants/${'.segments/'.repeat(2_000)}tail`, + `/${'a'.repeat(10_000)}\nb.png`, + ]) { + const start = performance.now(); + byMiddleware(input); + staticCspRegex.test(input); + notFoundRewriteRegex.test(input); + const elapsedMs = performance.now() - start; + assert.ok( + elapsedMs < 100, + `route regexes must run in linear time (${elapsedMs.toFixed(2)}ms on ${input.length} chars)` + ); + } + + console.warn(' ✅ proxy dotted-slug routing & CSP suite passed'); +} + +main().catch((err) => { + console.error(err); + process.exit(1); +}); diff --git a/apps/web/src/proxy.ts b/apps/web/src/proxy.ts index b8b90be9..4b4997d8 100644 --- a/apps/web/src/proxy.ts +++ b/apps/web/src/proxy.ts @@ -63,9 +63,9 @@ export function localeProxy(req: NextRequest): NextResponse { // Strip any explicit /vi, /en, /cs URL prefix so URLs never contain a locale slug const pathname = req.nextUrl.pathname; - const localePrefixMatch = pathname.match(/^\/(vi|en|cs)(\/.*)?$/); + const localePrefixMatch = pathname.match(/^\/(vi|en|cs)(\/.*)?$/i); if (localePrefixMatch) { - const matchedLocale = localePrefixMatch[1]!; + const matchedLocale = localePrefixMatch[1]!.toLowerCase(); // One leading slash: "/en//host" must not become a protocol-relative Location. const cleanPath = `/${(localePrefixMatch[2] ?? '').replace(/^\/+/, '')}`; const redirectUrl = req.nextUrl.clone(); @@ -115,10 +115,15 @@ export async function proxy(req: NextRequest): Promise { return response; } +// Dotted slugs under dynamic page routes (/merchants/:id, /compare/:versus, and +// /:category/:slug, with or without a /vi|/en|/cs prefix) are pages rather than +// static files, so they still run through the proxy for locale routing and CSP +// nonces (DOS/Rate#1807); next.config.mjs mirrors this in headers() and rewrites(). +// Next.js compiles config.matcher without the /i flag while routes-manifest.json +// sets caseSensitive: false, so literal prefixes here match both cases explicitly. export const config = { matcher: [ '/', - '/(cs|en|vi)/:path*', - '/((?!_next|_vercel|api|monitoring|sitemap|.*\\..*).*)', + '/((?!_[nN][eE][xX][tT]|_[vV][eE][rR][cC][eE][lL]|[aA][pP][iI]|[mM][oO][nN][iI][tT][oO][rR][iI][nN][gG]|[sS][iI][tT][eE][mM][aA][pP]|(?!(?:(?:[cC][sS]|[eE][nN]|[vV][iI])/)?(?:[mM][eE][rR][cC][hH][aA][nN][tT][sS]|[cC][oO][mM][pP][aA][rR][eE]|[sS][mM][aA][rR][tT][pP][hH][oO][nN][eE][sS]|[lL][aA][pP][tT][oO][pP][sS]|[tT][aA][bB][lL][eE][tT][sS]|[sS][mM][aA][rR][tT][wW][aA][tT][cC][hH][eE][sS]|[eE][aA][rR][bB][uU][dD][sS])/[a-zA-Z0-9][a-zA-Z0-9_-]*(?:[.][a-zA-Z0-9_-]+)*/?$)[^.]*\\..*).*)', ], }; From 60265518973b54790429b83bbf7d71f5253afa29 Mon Sep 17 00:00:00 2001 From: JOY <5027251+JOY@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:40:50 +0700 Subject: [PATCH 06/24] fix(web): suppress discount verdicts on unavailable or bait-price offers (#1818) * fix(web): suppress discount verdicts on unavailable or bait-price offers Guard HeroSearchInspector discountAnalysis against unavailable and bait offers, exclude unavailable/bait offer prices in DeepInspect, and require orig > current in Markdown price lines. Refs #1810. * fix(web): harden 1-2 offer bait, unavailable verdict, and orig guards Suppress top-level DeepInspect verdicts on unavailable/unpriced offers, guard Offers Table and CSV price_original with orig > current, catch sub-floor 1-2 offer bait and stale min_price, and speed up offer sort. Refs #1810. --- .../components/home/HeroSearchInspector.tsx | 298 +++++- .../inspect/DeepInspectWorkspace.tsx | 519 +++++++++- .../__tests__/inspect-verdict-guards.test.ts | 953 ++++++++++++++++++ .../lib/__tests__/offer-plausibility.test.ts | 4 + 4 files changed, 1706 insertions(+), 68 deletions(-) create mode 100644 apps/web/src/lib/__tests__/inspect-verdict-guards.test.ts diff --git a/apps/web/src/components/home/HeroSearchInspector.tsx b/apps/web/src/components/home/HeroSearchInspector.tsx index c170e2fd..69ff73e6 100644 --- a/apps/web/src/components/home/HeroSearchInspector.tsx +++ b/apps/web/src/components/home/HeroSearchInspector.tsx @@ -12,11 +12,28 @@ import { discountVerdictCopy } from '@/lib/discount-verdict-copy'; import { Link, useRouter } from '@/lib/navigation'; import { startNavigationProgress } from '@/components/elementary/NavigationProgress'; import type { KeySpecs } from '@repo/shared-data'; +import { + IMPLAUSIBLY_LOW_RATIO, + MINIMUM_DEVICE_PRICE_FLOOR_VND, + MINIMUM_EARBUDS_PRICE_FLOOR_VND, + implausiblyLowOfferIds, + isImplausiblyLowPrice, + isOfferImplausiblyLowAgainstInStock, + minimumListingPriceVnd, + normalizeOfferId, + plausibleMinPrice, + plausibleOffers, +} from '@repo/shared-data/rules'; import type { HeroProductSuggestion } from '@/lib/hero-products'; +import { + isOfferUnavailable, + type OfferAvailability, +} from '@/lib/product-market-stats'; import { MAX_VALID_VND_PRICE, getMasterProductCategorySlug, getMasterProductHref, + type OfferDiscountAnalysis, type UrlInspectionResult, } from '@/lib/rate-domain'; import { @@ -234,6 +251,259 @@ export function buildHeroCompareItem( }; } +function resolveHeroListingPriceFloorVnd(params: { + masterProduct?: { + name?: string | null; + slug?: string | null; + category_id?: string | null; + metadata?: Record | null; + specifications?: Record | null; + } | null; + offerTitle?: string | null; + offerUrl?: string | null; +}): number { + const { masterProduct, offerTitle, offerUrl } = params; + if (!masterProduct) { + return MINIMUM_EARBUDS_PRICE_FLOOR_VND; + } + const categorySlug = getMasterProductCategorySlug( + masterProduct as Parameters[0] + ); + if (categorySlug === 'earbuds') { + return MINIMUM_EARBUDS_PRICE_FLOOR_VND; + } + const texts = [ + masterProduct.name ?? '', + masterProduct.slug ?? '', + offerTitle ?? '', + offerUrl ?? '', + ].filter(Boolean); + return texts.length > 0 + ? minimumListingPriceVnd(texts) + : MINIMUM_DEVICE_PRICE_FLOOR_VND; +} + +export function resolveHeroInspectBestPriceVnd( + inspectResult: + | { + masterProduct?: { + min_price?: unknown; + name?: string | null; + slug?: string | null; + category_id?: string | null; + metadata?: Record | null; + specifications?: Record | null; + } | null; + allOffers?: + | readonly { + id: string; + price_current: number; + availability?: OfferAvailability | null; + implausiblyLow?: boolean | null; + title?: string | null; + url?: string | null; + }[] + | null; + } + | null + | undefined +): number { + const minFloor = resolveHeroListingPriceFloorVnd({ + masterProduct: inspectResult?.masterProduct, + }); + const rawMasterMin = sanitizeHeroInspectorPriceVnd( + inspectResult?.masterProduct?.min_price + ); + const safeOffers = Array.isArray(inspectResult?.allOffers) + ? inspectResult.allOffers + : []; + if (safeOffers.length === 0) { + return rawMasterMin >= minFloor ? rawMasterMin : 0; + } + const pricedInStockOffers = safeOffers.filter((o) => { + if (isOfferUnavailable(o) || o.implausiblyLow === true) return false; + const price = sanitizeHeroInspectorPriceVnd(o.price_current); + return price >= minFloor; + }); + if (pricedInStockOffers.length === 0) { + return 0; + } + return plausibleMinPrice(0, pricedInStockOffers); +} + +export function resolveHeroInspectDiscountAnalysis( + inspectResult: + | { + masterProduct?: { + min_price?: unknown; + name?: string | null; + slug?: string | null; + category_id?: string | null; + metadata?: Record | null; + specifications?: Record | null; + } | null; + matchedOffer?: { + id?: string | null; + price_current?: unknown; + availability?: OfferAvailability | null; + implausiblyLow?: boolean | null; + title?: string | null; + url?: string | null; + } | null; + allOffers?: + | readonly { + id: string; + price_current: number; + availability?: OfferAvailability | null; + implausiblyLow?: boolean | null; + title?: string | null; + url?: string | null; + }[] + | null; + discountAnalysis?: OfferDiscountAnalysis | null; + } + | null + | undefined +): OfferDiscountAnalysis | null { + const analysis = inspectResult?.discountAnalysis ?? null; + if (!analysis) return null; + + const matchedOffer = inspectResult?.matchedOffer ?? null; + const minFloor = resolveHeroListingPriceFloorVnd({ + masterProduct: inspectResult?.masterProduct, + offerTitle: matchedOffer?.title, + offerUrl: matchedOffer?.url, + }); + const safeAnalysisPrice = sanitizeHeroInspectorPriceVnd( + analysis.price_current + ); + if (safeAnalysisPrice < minFloor) { + return null; + } + + const safeOffers = Array.isArray(inspectResult?.allOffers) + ? inspectResult.allOffers + : []; + const inStockOffers = safeOffers.filter((o) => !isOfferUnavailable(o)); + const plausibilityOffers = + inStockOffers.length > 0 ? inStockOffers : safeOffers; + const implausibleOfferIds = implausiblyLowOfferIds(plausibilityOffers); + + const safeBestPrice = resolveHeroInspectBestPriceVnd(inspectResult); + if ( + safeBestPrice >= MINIMUM_EARBUDS_PRICE_FLOOR_VND && + safeAnalysisPrice < safeBestPrice * IMPLAUSIBLY_LOW_RATIO + ) { + return null; + } + if ( + isOfferImplausiblyLowAgainstInStock( + { + id: typeof analysis.product_id === 'string' ? analysis.product_id : '', + price_current: safeAnalysisPrice, + }, + plausibilityOffers, + implausibleOfferIds + ) + ) { + return null; + } + + if (!matchedOffer) { + const pricedPlausibleInStock = plausibleOffers(inStockOffers).filter( + (o) => + o.implausiblyLow !== true && + sanitizeHeroInspectorPriceVnd(o.price_current) >= minFloor + ); + if (safeOffers.length > 0 && pricedPlausibleInStock.length === 0) { + return null; + } + const analysisCanonicalId = normalizeOfferId(analysis.product_id); + const linkedOffer = safeOffers.find( + (o) => + o.id === analysis.product_id || + (analysisCanonicalId !== '' && + normalizeOfferId(o.id) === analysisCanonicalId) + ); + if ( + linkedOffer && + (isOfferUnavailable(linkedOffer) || + linkedOffer.implausiblyLow === true || + sanitizeHeroInspectorPriceVnd(linkedOffer.price_current) < minFloor || + isOfferImplausiblyLowAgainstInStock( + linkedOffer, + plausibilityOffers, + implausibleOfferIds + )) + ) { + return null; + } + return analysis; + } + + if (isOfferUnavailable(matchedOffer)) { + return null; + } + const safeOfferPrice = sanitizeHeroInspectorPriceVnd( + matchedOffer.price_current + ); + if (safeOfferPrice < minFloor) { + return null; + } + if (matchedOffer.implausiblyLow === true) { + return null; + } + if ( + safeBestPrice >= MINIMUM_EARBUDS_PRICE_FLOOR_VND && + safeOfferPrice < safeBestPrice * IMPLAUSIBLY_LOW_RATIO + ) { + return null; + } + + const candidate = + typeof matchedOffer.price_current === 'number' + ? (matchedOffer as { + id?: string | null; + price_current: number; + availability?: string | null; + }) + : { + id: typeof matchedOffer.id === 'string' ? matchedOffer.id : '', + price_current: safeOfferPrice, + availability: matchedOffer.availability ?? null, + }; + if ( + isOfferImplausiblyLowAgainstInStock( + candidate, + plausibilityOffers, + implausibleOfferIds + ) + ) { + return null; + } + + const matchedCanonicalId = normalizeOfferId(matchedOffer.id); + const peerReferenceOffers = plausibleOffers( + plausibilityOffers.filter((o) => { + if (o === matchedOffer) return false; + if ( + (o.id === matchedOffer.id || + (matchedCanonicalId !== '' && + normalizeOfferId(o.id) === matchedCanonicalId)) && + sanitizeHeroInspectorPriceVnd(o.price_current) === safeOfferPrice + ) { + return false; + } + return true; + }) + ); + if (isImplausiblyLowPrice(safeOfferPrice, peerReferenceOffers)) { + return null; + } + + return analysis; +} + export default function HeroSearchInspector({ initialProducts, initialMerchants, @@ -268,6 +538,8 @@ export default function HeroSearchInspector({ const [inspectError, setInspectError] = useState(null); const [inspectResult, setInspectResult] = useState(initialInspectResult); + const heroDiscountAnalysis = + resolveHeroInspectDiscountAnalysis(inspectResult); const containerRef = useRef(null); @@ -899,22 +1171,25 @@ export default function HeroSearchInspector({ {inspectResult.parsedUrl.platform} )} - {inspectResult.discountAnalysis && ( + {heroDiscountAnalysis && ( { discountVerdictCopy( tVerdict, - inspectResult.discountAnalysis, + heroDiscountAnalysis, locale ).label } @@ -943,9 +1218,8 @@ export default function HeroSearchInspector({ {(() => { - const safeBestPrice = sanitizeHeroInspectorPriceVnd( - inspectResult.masterProduct.min_price - ); + const safeBestPrice = + resolveHeroInspectBestPriceVnd(inspectResult); const safeLowestEver = sanitizeHeroInspectorPriceVnd( inspectResult.priceHistory?.lowestEverPrice @@ -977,12 +1251,12 @@ export default function HeroSearchInspector({ ); })()} - {inspectResult.discountAnalysis && ( + {heroDiscountAnalysis && (

{ discountVerdictCopy( tVerdict, - inspectResult.discountAnalysis, + heroDiscountAnalysis, locale ).explanation } diff --git a/apps/web/src/components/inspect/DeepInspectWorkspace.tsx b/apps/web/src/components/inspect/DeepInspectWorkspace.tsx index fd4b416f..76cc0867 100644 --- a/apps/web/src/components/inspect/DeepInspectWorkspace.tsx +++ b/apps/web/src/components/inspect/DeepInspectWorkspace.tsx @@ -64,9 +64,13 @@ import ContentText from '@/components/elementary/ContentText'; import { contentLang } from '@/lib/content-lang'; import { SAMPLE_AUDIT_URLS } from '@/lib/inspect-samples'; import { - implausiblyLowOfferIds, + IMPLAUSIBLY_LOW_RATIO, + MINIMUM_EARBUDS_PRICE_FLOOR_VND, + implausiblyLowIndexes, + isImplausiblyLowPrice, isOfferImplausiblyLowAgainstInStock, normalizeOfferId, + plausibleMinPrice, plausibleOffers, } from '@repo/shared-data/rules'; import { @@ -241,29 +245,97 @@ export function orderInspectOffers< } { const safeOffers = Array.isArray(offers) ? offers : []; const inStockOffers = safeOffers.filter((o) => !isOfferUnavailable(o)); - const plausibilityOffers = - inStockOffers.length > 0 ? inStockOffers : safeOffers; - const implausibleOfferIds = implausiblyLowOfferIds(plausibilityOffers); - const listedOffers = [...safeOffers].sort((a, b) => { - const unavailableA = isOfferUnavailable(a) ? 1 : 0; - const unavailableB = isOfferUnavailable(b) ? 1 : 0; - if (unavailableA !== unavailableB) return unavailableA - unavailableB; - const implausibleA = isInspectOfferBait( - a, - implausibleOfferIds, - plausibilityOffers - ) - ? 1 - : 0; - const implausibleB = isInspectOfferBait( - b, - implausibleOfferIds, - plausibilityOffers - ) - ? 1 - : 0; - return implausibleA - implausibleB; - }); + const hasInStock = inStockOffers.length > 0; + const plausibilityOffers = hasInStock ? inStockOffers : safeOffers; + const flaggedIndexes = implausiblyLowIndexes( + plausibilityOffers.map((o) => o?.price_current) + ); + const implausibleOfferIds = new Set(); + for (const idx of flaggedIndexes) { + const cleanId = normalizeOfferId(plausibilityOffers[idx]?.id); + if (cleanId) implausibleOfferIds.add(cleanId); + } + + let plausibleCutoff = 0; + if (hasInStock && flaggedIndexes.size === 0 && inStockOffers.length >= 2) { + const validPrices: number[] = []; + for (let i = 0; i < inStockOffers.length; i++) { + const p = inStockOffers[i]!.price_current; + if ( + typeof p === 'number' && + Number.isFinite(p) && + p > 0 && + p <= MAX_VALID_VND_PRICE + ) { + validPrices.push(p); + } else { + const sp = sanitizeInspectPriceVnd(p); + if (sp > 0) validPrices.push(sp); + } + } + if (validPrices.length >= 2) { + validPrices.sort((a, b) => a - b); + plausibleCutoff = + validPrices[Math.floor((validPrices.length - 1) / 2)]! * + IMPLAUSIBLY_LOW_RATIO; + } + } + + const inStockPlausible: T[] = []; + const inStockBait: T[] = []; + const unavailablePlausible: T[] = []; + const unavailableBait: T[] = []; + let inStockCursor = 0; + for (let i = 0; i < safeOffers.length; i++) { + const offer = safeOffers[i]!; + const unavailable = isOfferUnavailable(offer); + const rawPrice = offer.price_current; + const safePrice = + typeof rawPrice === 'number' + ? Number.isFinite(rawPrice) && + rawPrice > 0 && + rawPrice <= MAX_VALID_VND_PRICE + ? Math.round(rawPrice) + : 0 + : sanitizeInspectPriceVnd(rawPrice); + const subFloorOrExplicit = + (offer as { implausiblyLow?: boolean | null }).implausiblyLow === true || + (safePrice > 0 && safePrice < MINIMUM_EARBUDS_PRICE_FLOOR_VND); + + if (!unavailable) { + const idx = inStockCursor++; + if (subFloorOrExplicit || flaggedIndexes.has(idx)) { + inStockBait.push(offer); + } else { + inStockPlausible.push(offer); + } + } else if (!hasInStock) { + if (subFloorOrExplicit || flaggedIndexes.has(i)) { + unavailableBait.push(offer); + } else { + unavailablePlausible.push(offer); + } + } else if (subFloorOrExplicit) { + unavailableBait.push(offer); + } else if ( + flaggedIndexes.size === 0 && + !(plausibleCutoff > 0 && safePrice > 0 && safePrice < plausibleCutoff) + ) { + unavailablePlausible.push(offer); + } else if ( + isInspectOfferBait(offer, implausibleOfferIds, plausibilityOffers) + ) { + unavailableBait.push(offer); + } else { + unavailablePlausible.push(offer); + } + } + const listedOffers = [ + ...inStockPlausible, + ...inStockBait, + ...unavailablePlausible, + ...unavailableBait, + ]; return { inStockOffers, plausibilityOffers, @@ -299,11 +371,131 @@ export function inspectMarkdownPriceVerdict( return `**${copy.label}** (${copy.explanation})`; } +export function sanitizeInspectOriginalPriceVnd( + priceOriginal: unknown, + priceCurrent: unknown +): number { + const safeCurrent = sanitizeInspectPriceVnd(priceCurrent); + const safeOrig = sanitizeInspectPriceVnd(priceOriginal); + return safeCurrent > 0 && safeOrig > safeCurrent ? safeOrig : 0; +} + +export function resolveInspectMasterMinPriceVnd(params: { + matchedOffer?: { + id?: string | null; + price_current?: unknown; + availability?: OfferAvailability | null; + implausiblyLow?: boolean | null; + } | null; + allOffers?: + | readonly { + id: string; + price_current: number; + availability?: OfferAvailability | null; + implausiblyLow?: boolean | null; + }[] + | null; + inStockOffers?: + | readonly { + id?: string | null; + price_current: number; + availability?: OfferAvailability | null; + implausiblyLow?: boolean | null; + }[] + | null; + offerImplausiblyLow?: boolean; + master?: { + min_price?: unknown; + } | null; +}): number { + const { matchedOffer, allOffers, master } = params; + const rawMasterMin = sanitizeInspectPriceVnd(master?.min_price); + const safeOfferCurrent = sanitizeInspectPriceVnd(matchedOffer?.price_current); + const hasAllOffers = Array.isArray(allOffers) && allOffers.length > 0; + const hasExplicitInStock = + Array.isArray(params.inStockOffers) && params.inStockOffers.length > 0; + + if (!hasAllOffers && !hasExplicitInStock) { + if (rawMasterMin > 0 && rawMasterMin < MINIMUM_EARBUDS_PRICE_FLOOR_VND) { + return 0; + } + const explicitBait = + params.offerImplausiblyLow === true || + matchedOffer?.implausiblyLow === true || + (safeOfferCurrent > 0 && + safeOfferCurrent < MINIMUM_EARBUDS_PRICE_FLOOR_VND); + if ( + explicitBait && + safeOfferCurrent > 0 && + rawMasterMin === safeOfferCurrent + ) { + return 0; + } + return rawMasterMin; + } + + const candidateOffers = hasExplicitInStock + ? params.inStockOffers! + : allOffers!; + const matchedCanonicalId = normalizeOfferId(matchedOffer?.id); + const excludeMatchedBait = + params.offerImplausiblyLow === true || + matchedOffer?.implausiblyLow === true || + (safeOfferCurrent > 0 && + safeOfferCurrent < MINIMUM_EARBUDS_PRICE_FLOOR_VND); + + const pricedNonBaitInStock = candidateOffers.filter((o) => { + if (!o || typeof o !== 'object' || Array.isArray(o)) return false; + if (isOfferUnavailable(o)) return false; + if (o.implausiblyLow === true) return false; + const price = sanitizeInspectPriceVnd(o.price_current); + if (price < MINIMUM_EARBUDS_PRICE_FLOOR_VND) return false; + if (excludeMatchedBait && matchedOffer) { + if (o === matchedOffer) return false; + if ( + price === safeOfferCurrent && + ((typeof o.id === 'string' && o.id === matchedOffer.id) || + (matchedCanonicalId !== '' && + normalizeOfferId(o.id) === matchedCanonicalId)) + ) { + return false; + } + } + return true; + }); + + if (pricedNonBaitInStock.length === 0) { + return 0; + } + return plausibleMinPrice(0, pricedNonBaitInStock); +} + export function resolveInspectPriceVerdictPrices(params: { matchedOffer?: { + id?: string | null; price_current?: unknown; price_original?: unknown; + availability?: OfferAvailability | null; + implausiblyLow?: boolean | null; } | null; + allOffers?: + | readonly { + id: string; + price_current: number; + availability?: OfferAvailability | null; + implausiblyLow?: boolean | null; + }[] + | null; + inStockOffers?: + | readonly { + id?: string | null; + price_current: number; + availability?: OfferAvailability | null; + implausiblyLow?: boolean | null; + }[] + | null; + offerUnavailable?: boolean; + offerImplausiblyLow?: boolean; master?: { min_price?: unknown; max_price?: unknown; @@ -318,21 +510,142 @@ export function resolveInspectPriceVerdictPrices(params: { historicalLowVnd: number; historicalHighVnd: number; } { - const { matchedOffer, master, priceHistory } = params; + const { matchedOffer, allOffers, master, priceHistory } = params; + const safeOfferCurrent = sanitizeInspectPriceVnd(matchedOffer?.price_current); + const rawMasterMin = sanitizeInspectPriceVnd(master?.min_price); + const hasAllOffers = Array.isArray(allOffers) && allOffers.length > 0; + let cachedOrdered: ReturnType | null = null; + const getOrdered = () => { + if (!hasAllOffers) return null; + if (!cachedOrdered) { + cachedOrdered = orderInspectOffers(allOffers); + } + return cachedOrdered; + }; + + const unavailable = Boolean( + params.offerUnavailable || + (matchedOffer && isOfferUnavailable(matchedOffer)) + ); + const bait = (() => { + if ( + params.offerImplausiblyLow === true || + matchedOffer?.implausiblyLow === true + ) { + return true; + } + if ( + safeOfferCurrent > 0 && + safeOfferCurrent < MINIMUM_EARBUDS_PRICE_FLOOR_VND + ) { + return true; + } + if ( + rawMasterMin >= MINIMUM_EARBUDS_PRICE_FLOOR_VND && + safeOfferCurrent > 0 && + safeOfferCurrent < rawMasterMin * IMPLAUSIBLY_LOW_RATIO + ) { + return true; + } + if (params.offerImplausiblyLow === false) { + return false; + } + if (!matchedOffer || safeOfferCurrent <= 0) { + return false; + } + const candidate = + typeof matchedOffer.id === 'string' && + typeof matchedOffer.price_current === 'number' + ? (matchedOffer as { + id: string; + price_current: number; + availability?: OfferAvailability | null; + }) + : { + id: typeof matchedOffer.id === 'string' ? matchedOffer.id : '', + price_current: safeOfferCurrent, + availability: matchedOffer.availability ?? null, + }; + const matchedCanonicalId = normalizeOfferId(matchedOffer.id); + const isPeerOffer = (o: { id?: string | null; price_current: number }) => { + if (o === matchedOffer) return false; + if ( + ((typeof o.id === 'string' && o.id === matchedOffer.id) || + (matchedCanonicalId !== '' && + normalizeOfferId(o.id) === matchedCanonicalId)) && + sanitizeInspectPriceVnd(o.price_current) === safeOfferCurrent + ) { + return false; + } + return true; + }; + + const ordered = getOrdered(); + if (ordered) { + if ( + isInspectOfferBait( + candidate, + ordered.implausibleOfferIds, + ordered.plausibilityOffers + ) + ) { + return true; + } + return isImplausiblyLowPrice( + safeOfferCurrent, + plausibleOffers(ordered.plausibilityOffers.filter(isPeerOffer)) + ); + } + if ( + Array.isArray(params.inStockOffers) && + params.inStockOffers.length > 0 + ) { + if ( + isOfferImplausiblyLowAgainstInStock(candidate, params.inStockOffers) + ) { + return true; + } + return isImplausiblyLowPrice( + safeOfferCurrent, + plausibleOffers(params.inStockOffers.filter(isPeerOffer)) + ); + } + return false; + })(); + + const safeFallbackMin = resolveInspectMasterMinPriceVnd({ + matchedOffer, + allOffers, + inStockOffers: + params.inStockOffers ?? + (hasAllOffers ? (getOrdered()?.inStockOffers ?? null) : null), + offerImplausiblyLow: bait, + master, + }); + + const suppressOfferPrices = unavailable || bait; + const currentPriceVnd = + (suppressOfferPrices ? 0 : safeOfferCurrent) || safeFallbackMin; + const claimedOriginalVnd = suppressOfferPrices + ? 0 + : sanitizeInspectOriginalPriceVnd( + matchedOffer?.price_original, + currentPriceVnd + ); + return { - currentPriceVnd: - sanitizeInspectPriceVnd(matchedOffer?.price_current) || - sanitizeInspectPriceVnd(master?.min_price), - claimedOriginalVnd: sanitizeInspectPriceVnd(matchedOffer?.price_original), + currentPriceVnd, + claimedOriginalVnd, historicalLowVnd: - sanitizeInspectPriceVnd(priceHistory?.lowestEverPrice) || - sanitizeInspectPriceVnd(master?.min_price), + sanitizeInspectPriceVnd(priceHistory?.lowestEverPrice) || safeFallbackMin, historicalHighVnd: sanitizeInspectPriceVnd(priceHistory?.highestEverPrice) || sanitizeInspectPriceVnd(master?.max_price), }; } +export const resolveInspectVerdictPrices = resolveInspectPriceVerdictPrices; + export function buildInspectMarkdownPriceLines(params: { currentListedPrice: unknown; claimedOrigPrice: unknown; @@ -352,7 +665,7 @@ export function buildInspectMarkdownPriceLines(params: { const out: string[] = []; if (current > 0) { const origSuffix = - orig > 0 + orig > current ? ` | ${params.labels.claimedOriginalLabel}: ${orig.toLocaleString('vi-VN')} ₫` : ''; out.push( @@ -382,12 +695,27 @@ export function isInspectOfferBait( id: string; price_current: number; availability?: OfferAvailability | null; + implausiblyLow?: boolean | null; } | null | undefined, implausibleOfferIds: ReadonlySet, inStockOffers: readonly { id?: string | null; price_current: number }[] ): boolean { + if (!offer) return false; + if (offer.implausiblyLow === true) return true; + const rawPrice = offer.price_current; + const safePrice = + typeof rawPrice === 'number' + ? Number.isFinite(rawPrice) && + rawPrice > 0 && + rawPrice <= MAX_VALID_VND_PRICE + ? Math.round(rawPrice) + : 0 + : sanitizeInspectPriceVnd(rawPrice); + if (safePrice > 0 && safePrice < MINIMUM_EARBUDS_PRICE_FLOOR_VND) { + return true; + } return isOfferImplausiblyLowAgainstInStock( offer, inStockOffers, @@ -568,24 +896,87 @@ export default function DeepInspectWorkspace({ implausibleOfferIds, listedOffers, } = useMemo(() => orderInspectOffers(result?.allOffers), [result?.allOffers]); - // A sold-out listing is never the fallback match (DOS/Rate#1177). + // A sold-out, unpriced, or bait listing is never the fallback match (DOS/Rate#1177, #1810). const matchedOffer = - result?.matchedOffer ?? plausibleOffers(inStockOffers)[0] ?? null; + result?.matchedOffer ?? + plausibleOffers( + inStockOffers.filter( + (o) => + sanitizeInspectPriceVnd(o.price_current) >= + MINIMUM_EARBUDS_PRICE_FLOOR_VND && + !isInspectOfferBait(o, implausibleOfferIds, plausibilityOffers) + ) + )[0] ?? + null; + const apiVerdictImplausiblyLow = Boolean( + ( + result as { + priceVerdict?: { implausiblyLow?: boolean | null } | null; + } | null + )?.priceVerdict?.implausiblyLow === true + ); + const rawMasterMinForBait = sanitizeInspectPriceVnd(master?.min_price); + const safeMatchedOfferPrice = sanitizeInspectPriceVnd( + matchedOffer?.price_current + ); // Judged among in-stock listings, as the product page's offers table does (#797, #1141). - const offerImplausiblyLow = isInspectOfferBait( - matchedOffer, - implausibleOfferIds, - plausibilityOffers + const offerImplausiblyLow = Boolean( + matchedOffer && + (apiVerdictImplausiblyLow || + isInspectOfferBait( + matchedOffer, + implausibleOfferIds, + plausibilityOffers + ) || + (rawMasterMinForBait >= MINIMUM_EARBUDS_PRICE_FLOOR_VND && + safeMatchedOfferPrice > 0 && + safeMatchedOfferPrice < rawMasterMinForBait * IMPLAUSIBLY_LOW_RATIO)) + ); + const hasAllOffersList = Boolean( + Array.isArray(result?.allOffers) && result.allOffers.length > 0 + ); + const allOffersUnavailable = Boolean( + hasAllOffersList && + !inStockOffers.some((o) => sanitizeInspectPriceVnd(o.price_current) > 0) ); const matchedOfferUnavailable = Boolean( - matchedOffer && isOfferUnavailable(matchedOffer) + (matchedOffer && isOfferUnavailable(matchedOffer)) || + (!matchedOffer && allOffersUnavailable) ); - const discount = offerImplausiblyLow - ? null - : filterInspectOfferDiscount( - result?.discountAnalysis, - matchedOfferUnavailable - ); + const discount = (() => { + const rawDiscount = result?.discountAnalysis ?? null; + if (!rawDiscount || offerImplausiblyLow || matchedOfferUnavailable) { + return null; + } + if ( + matchedOffer && + safeMatchedOfferPrice < MINIMUM_EARBUDS_PRICE_FLOOR_VND + ) { + return null; + } + const safeDiscountPrice = sanitizeInspectPriceVnd( + rawDiscount.price_current + ); + if (safeDiscountPrice < MINIMUM_EARBUDS_PRICE_FLOOR_VND) { + return null; + } + if ( + isInspectOfferBait( + { + id: + typeof rawDiscount.product_id === 'string' + ? rawDiscount.product_id + : '', + price_current: safeDiscountPrice, + }, + implausibleOfferIds, + plausibilityOffers + ) + ) { + return null; + } + return filterInspectOfferDiscount(rawDiscount, matchedOfferUnavailable); + })(); const priceHistory = result?.priceHistory ?? null; const reviewSummary = result?.reviewSummary ?? null; const merchant = result?.merchantProfile ?? null; @@ -597,14 +988,22 @@ export default function DeepInspectWorkspace({ ); const verdictPrices = resolveInspectPriceVerdictPrices({ matchedOffer, + allOffers: result?.allOffers, + inStockOffers, + offerUnavailable: matchedOfferUnavailable, + offerImplausiblyLow, master, priceHistory, }); + const safeMasterMinPriceVnd = resolveInspectMasterMinPriceVnd({ + matchedOffer, + allOffers: result?.allOffers, + inStockOffers, + offerImplausiblyLow, + master, + }); const watchableMinPriceVnd = - sanitizeInspectPriceVnd(master?.min_price) || - (!offerImplausiblyLow && !matchedOfferUnavailable - ? sanitizeInspectPriceVnd(matchedOffer?.price_current) - : 0); + safeMasterMinPriceVnd || verdictPrices.currentPriceVnd; const rawMerchantTrust = parseInspectFiniteNumber(merchant?.trust_score); const safeMerchantTrust = rawMerchantTrust == null @@ -748,7 +1147,7 @@ export default function DeepInspectWorkspace({ ).toUpperCase(), o.title || master?.name || cleanQuery, sanitizeInspectPriceVnd(o.price_current), - sanitizeInspectPriceVnd(o.price_original), + sanitizeInspectOriginalPriceVnd(o.price_original, o.price_current), histLow, histHigh, matchedPriceSpread?.spreadPct ?? 0, @@ -769,9 +1168,12 @@ export default function DeepInspectWorkspace({ ? byOfferMap[canonicalOfferId] : undefined; const rawAnalysis = - directByOffer ?? - canonicalByOffer ?? - (o.id === matchedOffer?.id ? discount : null); + sanitizeInspectPriceVnd(o.price_current) >= + MINIMUM_EARBUDS_PRICE_FLOOR_VND + ? (directByOffer ?? + canonicalByOffer ?? + (o.id === matchedOffer?.id ? result?.discountAnalysis : null)) + : null; const a = filterInspectOfferDiscount( rawAnalysis, isOfferUnavailable(o) @@ -1391,14 +1793,19 @@ export default function DeepInspectWorkspace({ - 0 + sanitizeInspectOriginalPriceVnd( + offer.price_original, + offer.price_current + ) > 0 ? 'text-decoration-line-through' : '' }`} > {formatInspectPriceVndOrDash( - offer.price_original, + sanitizeInspectOriginalPriceVnd( + offer.price_original, + offer.price_current + ), locale )} diff --git a/apps/web/src/lib/__tests__/inspect-verdict-guards.test.ts b/apps/web/src/lib/__tests__/inspect-verdict-guards.test.ts new file mode 100644 index 00000000..68883e22 --- /dev/null +++ b/apps/web/src/lib/__tests__/inspect-verdict-guards.test.ts @@ -0,0 +1,953 @@ +import assert from 'node:assert/strict'; +import React from 'react'; +import { renderToStaticMarkup } from 'react-dom/server'; +import { NextIntlClientProvider, createTranslator } from 'next-intl'; +import { + AppRouterContext, + type AppRouterInstance, +} from 'next/dist/shared/lib/app-router-context.shared-runtime'; +import csMessages from '../../../locales/cs.json'; +import enMessages from '../../../locales/en.json'; +import viMessages from '../../../locales/vi.json'; +import { pickClientMessages } from '../client-messages'; +import { discountVerdictCopy } from '../discount-verdict-copy'; +import { formatPriceVnd } from '../locale-format'; +import { + MAX_VALID_VND_PRICE, + parseEcommerceUrlOrQuery, + type MerchantOffer, + type OfferDiscountAnalysis, + type UrlInspectionResult, +} from '../rate-domain'; + +const LOCALES = ['vi', 'en', 'cs'] as const; + +const mockRouter: AppRouterInstance = { + back: () => {}, + forward: () => {}, + refresh: () => {}, + push: () => {}, + replace: () => {}, + prefetch: () => {}, + bfcacheId: 'test', +}; + +const IntlProvider = NextIntlClientProvider as React.ComponentType<{ + locale: string; + messages: Record; + timeZone: string; + children?: React.ReactNode; +}>; + +function messagesForLocale(locale: (typeof LOCALES)[number]) { + return locale === 'vi' + ? viMessages + : locale === 'en' + ? enMessages + : csMessages; +} + +function makeOffer( + id: string, + priceCurrent: number, + availability: + 'in_stock' | 'out_of_stock' | 'discontinued' | null = 'in_stock', + priceOriginal: number | null = null +): MerchantOffer { + return { + id, + platform: 'shopee', + title: `Offer ${id}`, + price_current: priceCurrent, + price_original: priceOriginal, + stock: availability === 'in_stock' ? 5 : 0, + rating_star: 4.9, + rating_count: 18, + url: `https://shopee.vn/item-i.1810.${id.trim() || '0'}`, + availability, + merchant: { + id: `merchant-${id.trim() || '0'}`, + name: `Shop ${id.trim() || '0'}`, + platform: 'shopee', + is_official: true, + rating_star: 4.9, + follower_count: 1000, + location: 'VN', + risk_score: 6, + dosafe_risk_score: 6, + review_risk_score: 6, + review_risk_breakdown: null, + }, + } as unknown as MerchantOffer; +} + +function makeDiscountAnalysis( + verdict: OfferDiscountAnalysis['verdict'], + priceCurrent = 19_990_000, + priceOriginal: number | null = 24_990_000 +): OfferDiscountAnalysis { + return { + product_id: 'offer-1810', + merchant_name: 'Shop 1810', + platform: 'shopee', + price_current: priceCurrent, + price_original: priceOriginal, + claimed_discount_pct: 20, + real_discount_vs_median_pct: verdict === 'genuine_deal' ? 12 : 0, + historical_min_price: 19_500_000, + historical_max_price: 22_000_000, + historical_median_price: 21_000_000, + observation_count: 6, + verdict, + badge_label: verdict, + explanation: verdict, + }; +} + +export async function runInspectVerdictGuardsSuite() { + const nodeModule = await import('node:module'); + const extMap = ( + nodeModule.default as unknown as { + _extensions?: Record void>; + } + )._extensions; + if (extMap && !extMap['.css']) { + extMap['.css'] = (mod) => { + mod.exports = {}; + }; + } + + const { + default: HeroSearchInspector, + resolveHeroInspectBestPriceVnd, + resolveHeroInspectDiscountAnalysis, + } = await import('../../components/home/HeroSearchInspector'); + const { + default: DeepInspectWorkspace, + buildInspectMarkdownPriceLines, + isInspectOfferBait, + resolveInspectMasterMinPriceVnd, + resolveInspectPriceVerdictPrices, + resolveInspectVerdictPrices, + sanitizeInspectOriginalPriceVnd, + } = await import('../../components/inspect/DeepInspectWorkspace'); + + assert.equal( + resolveInspectVerdictPrices, + resolveInspectPriceVerdictPrices, + 'resolveInspectVerdictPrices aliases resolveInspectPriceVerdictPrices' + ); + + const inStockPeerA = makeOffer('peer-a', 20_000_000, 'in_stock', 22_000_000); + const inStockPeerB = makeOffer('peer-b', 21_500_000, 'in_stock', null); + const bait1000Offer = makeOffer('bait-1k', 1_000, 'in_stock', 25_000_000); + const paddedBaitOffer = makeOffer( + ' bait-padded\u0000 ', + 1_000, + 'in_stock', + 25_000_000 + ); + const uncanonicalBaitOffer = makeOffer('000', 1_000, 'in_stock', 25_000_000); + const soldOutOffer = makeOffer( + 'sold-out-1', + 15_000_000, + 'out_of_stock', + 25_000_000 + ); + const discontinuedOffer = makeOffer( + 'disc-1', + 16_000_000, + 'discontinued', + 26_000_000 + ); + + // 1. HeroSearchInspector pure guard: suppresses genuine_deal, fake_discount, + // suspicious_discount, and normal_price on unavailable or bait offers. + for (const verdict of [ + 'genuine_deal', + 'fake_discount', + 'suspicious_discount', + 'normal_price', + ] as const) { + const analysis = makeDiscountAnalysis(verdict); + + assert.equal( + resolveHeroInspectDiscountAnalysis({ + matchedOffer: soldOutOffer, + allOffers: [soldOutOffer, inStockPeerA, inStockPeerB], + discountAnalysis: analysis, + }), + null, + `sold-out offer must suppress ${verdict}` + ); + + assert.equal( + resolveHeroInspectDiscountAnalysis({ + matchedOffer: discontinuedOffer, + allOffers: [discontinuedOffer, inStockPeerA, inStockPeerB], + discountAnalysis: analysis, + }), + null, + `discontinued offer must suppress ${verdict}` + ); + + assert.equal( + resolveHeroInspectDiscountAnalysis({ + matchedOffer: bait1000Offer, + allOffers: [bait1000Offer, inStockPeerA, inStockPeerB], + discountAnalysis: makeDiscountAnalysis(verdict, 1_000, 25_000_000), + }), + null, + `1,000 VND bait offer must suppress ${verdict}` + ); + + assert.equal( + resolveHeroInspectDiscountAnalysis({ + matchedOffer: paddedBaitOffer, + allOffers: [paddedBaitOffer, inStockPeerA, inStockPeerB], + discountAnalysis: makeDiscountAnalysis(verdict, 1_000, 25_000_000), + }), + null, + `padded-ID 1,000 VND bait offer must suppress ${verdict}` + ); + + assert.equal( + resolveHeroInspectDiscountAnalysis({ + matchedOffer: uncanonicalBaitOffer, + allOffers: [uncanonicalBaitOffer, inStockPeerA, inStockPeerB], + discountAnalysis: makeDiscountAnalysis(verdict, 1_000, 25_000_000), + }), + null, + `uncanonicalizable-ID 1,000 VND bait offer must suppress ${verdict}` + ); + + assert.equal( + resolveHeroInspectDiscountAnalysis({ + matchedOffer: { + id: 'explicit-bait', + price_current: 1_000, + availability: 'in_stock', + implausiblyLow: true, + }, + allOffers: [], + discountAnalysis: makeDiscountAnalysis(verdict, 1_000, 25_000_000), + }), + null, + `explicit implausiblyLow matchedOffer must suppress ${verdict}` + ); + + assert.equal( + resolveHeroInspectDiscountAnalysis({ + matchedOffer: null, + allOffers: [soldOutOffer, discontinuedOffer], + discountAnalysis: analysis, + }), + null, + `all-unavailable cluster with null matchedOffer must suppress ${verdict}` + ); + + for (const badPrice of [ + 0, + -500, + Number.NaN, + MAX_VALID_VND_PRICE + 1, + ] as const) { + assert.equal( + resolveHeroInspectDiscountAnalysis({ + matchedOffer: { + id: 'unpriced', + price_current: badPrice, + availability: 'in_stock', + }, + allOffers: [inStockPeerA, inStockPeerB], + discountAnalysis: analysis, + }), + null, + `invalid offer price ${String(badPrice)} must suppress ${verdict}` + ); + } + + // Plausible in-stock offer keeps its verdict, even alongside high discontinued MSRPs. + const inflatedDiscontinuedA = makeOffer( + 'old-50m', + 50_000_000, + 'discontinued' + ); + const inflatedDiscontinuedB = makeOffer( + 'old-52m', + 52_000_000, + 'out_of_stock' + ); + assert.equal( + resolveHeroInspectDiscountAnalysis({ + matchedOffer: inStockPeerA, + allOffers: [ + inflatedDiscontinuedA, + inflatedDiscontinuedB, + inStockPeerA, + inStockPeerB, + ], + discountAnalysis: analysis, + }), + analysis, + `plausible in-stock offer keeps ${verdict}` + ); + + // Round 1 coverage: 1-offer and 2-offer 1,000 VND bait clusters, malformed + // analysis.price_current, null matchedOffer with bait/unavailable linked ID, + // unpriced in-stock peers, and ID-collision overrides. + assert.equal( + resolveHeroInspectDiscountAnalysis({ + matchedOffer: bait1000Offer, + allOffers: [bait1000Offer], + discountAnalysis: makeDiscountAnalysis(verdict, 1_000, 25_000_000), + }), + null, + `1-offer 1,000 VND bait cluster must suppress ${verdict}` + ); + assert.equal( + resolveHeroInspectDiscountAnalysis({ + matchedOffer: bait1000Offer, + allOffers: [bait1000Offer, inStockPeerA], + discountAnalysis: makeDiscountAnalysis(verdict, 1_000, 25_000_000), + }), + null, + `2-offer 1,000 VND bait cluster must suppress ${verdict}` + ); + assert.equal( + resolveHeroInspectDiscountAnalysis({ + matchedOffer: inStockPeerA, + allOffers: [inStockPeerA, inStockPeerB], + discountAnalysis: makeDiscountAnalysis(verdict, 0, 25_000_000), + }), + null, + `zero analysis.price_current must suppress ${verdict}` + ); + assert.equal( + resolveHeroInspectDiscountAnalysis({ + matchedOffer: inStockPeerA, + allOffers: [inStockPeerA, inStockPeerB], + discountAnalysis: makeDiscountAnalysis(verdict, 1_000, 25_000_000), + }), + null, + `bait analysis.price_current must suppress ${verdict}` + ); + assert.equal( + resolveHeroInspectDiscountAnalysis({ + matchedOffer: null, + allOffers: [soldOutOffer, inStockPeerA, inStockPeerB], + discountAnalysis: { + ...analysis, + product_id: soldOutOffer.id, + }, + }), + null, + `null matchedOffer linked to unavailable product_id must suppress ${verdict}` + ); + assert.equal( + resolveHeroInspectDiscountAnalysis({ + matchedOffer: null, + allOffers: [makeOffer('unpriced-in-stock', 0, 'in_stock')], + discountAnalysis: analysis, + }), + null, + `unpriced 0 VND in-stock cluster must suppress ${verdict}` + ); + assert.equal( + resolveHeroInspectDiscountAnalysis({ + matchedOffer: { + id: 'peer-a', + price_current: 500_000, + availability: 'in_stock', + }, + allOffers: [inStockPeerA, inStockPeerB], + discountAnalysis: makeDiscountAnalysis(verdict, 500_000, 25_000_000), + }), + null, + `ID-colliding overridden bait offer must suppress ${verdict}` + ); + } + + assert.equal( + resolveHeroInspectBestPriceVnd({ + masterProduct: { min_price: 15_000_000, slug: 'iphone-16' }, + allOffers: [soldOutOffer, makeOffer('unpriced-0', 0, 'in_stock')], + }), + 0, + 'HeroSearchInspector best price suppresses stale min_price when no priced in-stock offer exists' + ); + assert.equal( + resolveHeroInspectBestPriceVnd({ + masterProduct: { min_price: 1_000, slug: 'iphone-16' }, + allOffers: [bait1000Offer, inStockPeerA], + }), + 20_000_000, + 'HeroSearchInspector best price excludes 1,000 VND bait in 2-offer cluster' + ); + + // 2. HeroSearchInspector SSR HTML across vi/en/cs & home/contact scopes. + const baseMaster = { + id: 'mp-1810', + slug: 'iphone-16-1810', + name: 'iPhone 16 128GB', + brand: 'Apple', + model: 'iPhone 16', + model_key: 'apple_iphone_16_128gb', + min_price: 20_000_000, + max_price: 21_500_000, + merchant_count: 2, + status: 'published', + images: [], + metadata: { category_slug: 'smartphones' }, + } as unknown as NonNullable; + + const renderHeroInspect = ( + locale: (typeof LOCALES)[number], + scope: 'home' | 'contact', + inspectResult: UrlInspectionResult + ): string => + renderToStaticMarkup( + React.createElement( + AppRouterContext.Provider, + { value: mockRouter }, + React.createElement( + IntlProvider, + { + locale, + messages: pickClientMessages( + messagesForLocale(locale) as unknown as Record, + scope + ), + timeZone: 'Asia/Ho_Chi_Minh', + }, + React.createElement(HeroSearchInspector, { + initialProducts: [], + initialMerchants: [], + initialMode: 'inspect', + initialInspectResult: inspectResult, + }) + ) + ) + ); + + for (const locale of LOCALES) { + const fullMsgs = messagesForLocale(locale); + const tVerdict = createTranslator({ + locale, + messages: fullMsgs, + namespace: 'priceVerdict', + }); + + for (const scope of ['home', 'contact'] as const) { + for (const verdict of ['genuine_deal', 'fake_discount'] as const) { + const analysis = makeDiscountAnalysis(verdict, 19_990_000, 24_990_000); + const expectedCopy = discountVerdictCopy(tVerdict, analysis, locale); + + for (const suppressedOffer of [ + soldOutOffer, + discontinuedOffer, + bait1000Offer, + ]) { + const suppressedHtml = renderHeroInspect(locale, scope, { + query: suppressedOffer.url, + matchedType: 'product_listing', + parsedUrl: parseEcommerceUrlOrQuery(suppressedOffer.url), + masterProduct: baseMaster, + matchedOffer: suppressedOffer, + allOffers: [suppressedOffer, inStockPeerA, inStockPeerB], + discountAnalysis: analysis, + priceHistory: null, + reviewSummary: null, + merchantProfile: null, + suggestions: [], + }); + assert.equal( + suppressedHtml.includes(expectedCopy.label), + false, + `${locale}/${scope}: ${suppressedOffer.id} must not render ${verdict} badge label` + ); + assert.equal( + suppressedHtml.includes(expectedCopy.explanation), + false, + `${locale}/${scope}: ${suppressedOffer.id} must not render ${verdict} explanation` + ); + } + + const validHtml = renderHeroInspect(locale, scope, { + query: inStockPeerA.url, + matchedType: 'product_listing', + parsedUrl: parseEcommerceUrlOrQuery(inStockPeerA.url), + masterProduct: baseMaster, + matchedOffer: inStockPeerA, + allOffers: [bait1000Offer, inStockPeerA, inStockPeerB], + discountAnalysis: analysis, + priceHistory: null, + reviewSummary: null, + merchantProfile: null, + suggestions: [], + }); + assert.ok( + validHtml.includes(expectedCopy.label) && + validHtml.includes(expectedCopy.explanation), + `${locale}/${scope}: plausible in-stock offer renders ${verdict} badge and explanation` + ); + } + } + } + + // 3. DeepInspectWorkspace resolveInspectVerdictPrices & buildInspectMarkdownPriceLines. + const mdLabels = { + currentPriceLabel: 'Current price', + claimedOriginalLabel: 'Original price', + historicalLowLabel: 'Historical low', + historicalHighLabel: 'Historical high', + }; + + // Unavailable offers exclude matchedOffer.price_current & price_original. + for (const unavailableOffer of [soldOutOffer, discontinuedOffer]) { + assert.deepEqual( + resolveInspectVerdictPrices({ + matchedOffer: unavailableOffer, + allOffers: [unavailableOffer, inStockPeerA, inStockPeerB], + master: { min_price: 20_000_000, max_price: 21_500_000 }, + priceHistory: { + lowestEverPrice: 19_500_000, + highestEverPrice: 22_000_000, + }, + }), + { + currentPriceVnd: 20_000_000, + claimedOriginalVnd: 0, + historicalLowVnd: 19_500_000, + historicalHighVnd: 22_000_000, + }, + `${unavailableOffer.id} falls back to master min_price and drops claimedOriginalVnd` + ); + + // All offers unavailable -> currentPriceVnd is 0 even if master.min_price is stale. + const allSoldOutVerdictPrices = resolveInspectVerdictPrices({ + matchedOffer: unavailableOffer, + allOffers: [soldOutOffer, discontinuedOffer], + master: { min_price: 15_000_000, max_price: 16_000_000 }, + priceHistory: { + lowestEverPrice: 15_000_000, + highestEverPrice: 16_000_000, + }, + }); + assert.deepEqual(allSoldOutVerdictPrices, { + currentPriceVnd: 0, + claimedOriginalVnd: 0, + historicalLowVnd: 15_000_000, + historicalHighVnd: 16_000_000, + }); + assert.deepEqual( + buildInspectMarkdownPriceLines({ + currentListedPrice: allSoldOutVerdictPrices.currentPriceVnd, + claimedOrigPrice: allSoldOutVerdictPrices.claimedOriginalVnd, + histLow: allSoldOutVerdictPrices.historicalLowVnd, + histHigh: allSoldOutVerdictPrices.historicalHighVnd, + labels: mdLabels, + }), + ['- **Historical low / Historical high:** 15.000.000 ₫ - 16.000.000 ₫'] + ); + } + + // 1,000 VND bait offer never leaks into VerdictPrices or Markdown lines, + // even when master.min_price also holds a stale 1,000 VND bait value. + for (const staleMasterMin of [20_000_000, 1_000]) { + const baitPrices = resolveInspectVerdictPrices({ + matchedOffer: bait1000Offer, + allOffers: [bait1000Offer, inStockPeerA, inStockPeerB], + master: { min_price: staleMasterMin, max_price: 21_500_000 }, + priceHistory: { lowestEverPrice: null, highestEverPrice: 22_000_000 }, + }); + assert.deepEqual(baitPrices, { + currentPriceVnd: 20_000_000, + claimedOriginalVnd: 0, + historicalLowVnd: 20_000_000, + historicalHighVnd: 22_000_000, + }); + const baitMdLines = buildInspectMarkdownPriceLines({ + currentListedPrice: baitPrices.currentPriceVnd, + claimedOrigPrice: baitPrices.claimedOriginalVnd, + histLow: baitPrices.historicalLowVnd, + histHigh: baitPrices.historicalHighVnd, + labels: mdLabels, + }); + assert.deepEqual(baitMdLines, [ + '- **Current price:** 20.000.000 ₫', + '- **Historical low / Historical high:** 20.000.000 ₫ - 22.000.000 ₫', + ]); + assert.equal( + baitMdLines.join('\n').includes('1.000'), + false, + '1,000 VND bait price must never leak into Markdown lines' + ); + assert.equal( + baitMdLines.join('\n').includes('25.000.000'), + false, + 'bait offer claimed original price must never leak into Markdown lines' + ); + } + + // Direct inStockOffers parameter (without allOffers) also detects 1,000 VND bait. + assert.deepEqual( + resolveInspectVerdictPrices({ + matchedOffer: { + id: 'bait-direct', + price_current: 1_000, + price_original: 25_000_000, + }, + inStockOffers: [inStockPeerA, inStockPeerB], + master: { min_price: 20_000_000, max_price: 21_500_000 }, + priceHistory: null, + }), + { + currentPriceVnd: 20_000_000, + claimedOriginalVnd: 0, + historicalLowVnd: 20_000_000, + historicalHighVnd: 21_500_000, + } + ); + + // orig <= current is omitted in both resolveInspectVerdictPrices and buildInspectMarkdownPriceLines. + for (const nonHigherOrig of [0, 15_000_000, 19_990_000]) { + assert.equal( + resolveInspectVerdictPrices({ + matchedOffer: { + id: 'plausible-1', + price_current: 19_990_000, + price_original: nonHigherOrig, + availability: 'in_stock', + }, + allOffers: [inStockPeerA, inStockPeerB], + master: { min_price: 19_990_000, max_price: 21_500_000 }, + priceHistory: null, + }).claimedOriginalVnd, + 0, + `resolveInspectVerdictPrices omits claimedOriginalVnd when orig (${nonHigherOrig}) <= current` + ); + + assert.deepEqual( + buildInspectMarkdownPriceLines({ + currentListedPrice: 19_990_000, + claimedOrigPrice: nonHigherOrig, + histLow: 0, + histHigh: 0, + labels: mdLabels, + }), + ['- **Current price:** 19.990.000 ₫'], + `buildInspectMarkdownPriceLines omits claimedOriginalLabel when orig (${nonHigherOrig}) <= current` + ); + } + + // Strictly higher orig > current is retained. + assert.equal( + resolveInspectVerdictPrices({ + matchedOffer: { + id: 'plausible-disc', + price_current: 19_990_000, + price_original: 19_990_001, + availability: 'in_stock', + }, + master: { min_price: 19_990_000, max_price: 21_500_000 }, + priceHistory: null, + }).claimedOriginalVnd, + 19_990_001 + ); + assert.deepEqual( + buildInspectMarkdownPriceLines({ + currentListedPrice: 19_990_000, + claimedOrigPrice: 24_990_000, + histLow: 0, + histHigh: 0, + labels: mdLabels, + }), + ['- **Current price:** 19.990.000 ₫ | Original price: 24.990.000 ₫'] + ); + + // 4. DeepInspectWorkspace SSR render across locales: Verdict Card never leaks + // 1,000 VND bait price or bait/unavailable strikethroughs into summary tiles. + for (const locale of LOCALES) { + const inspectMsgs = messagesForLocale(locale).inspect; + const renderInspect = (inspection: UrlInspectionResult) => + renderToStaticMarkup( + React.createElement( + AppRouterContext.Provider, + { value: mockRouter }, + React.createElement( + IntlProvider, + { + locale, + messages: pickClientMessages( + messagesForLocale(locale) as unknown as Record, + 'inspect' + ), + timeZone: 'Asia/Ho_Chi_Minh', + }, + React.createElement(DeepInspectWorkspace, { + initialQuery: inspection.query, + initialInspection: inspection, + }) + ) + ) + ); + + const baitHtml = renderInspect({ + query: bait1000Offer.url, + matchedType: 'product_listing', + parsedUrl: parseEcommerceUrlOrQuery(bait1000Offer.url), + masterProduct: baseMaster, + matchedOffer: bait1000Offer, + allOffers: [bait1000Offer, inStockPeerA, inStockPeerB], + discountAnalysis: makeDiscountAnalysis('genuine_deal', 1_000, 25_000_000), + priceHistory: { + points: [], + byOffer: {}, + lowestEverPrice: 19_500_000, + lowestEverDate: null, + lowestEverMerchant: null, + highestEverPrice: 21_500_000, + currentMinPrice: 20_000_000, + fakeDiscountCount: 0, + genuineDealCount: 0, + }, + reviewSummary: null, + merchantProfile: null, + suggestions: [], + }); + + const [verdictCardHtml] = baitHtml.split('${watchlistMsgs.btnWatch}<`), + false, + `${locale}: all-sold-out product with stale master.min_price hides PriceWatchButton` + ); + } + + // 5. Additional pure helper checks for Round 1 edge cases. + assert.equal(sanitizeInspectOriginalPriceVnd(20_000_000, 20_000_000), 0); + assert.equal(sanitizeInspectOriginalPriceVnd(18_000_000, 20_000_000), 0); + assert.equal(sanitizeInspectOriginalPriceVnd(25_000_000, 0), 0); + assert.equal( + sanitizeInspectOriginalPriceVnd(25_000_000, 20_000_000), + 25_000_000 + ); + assert.equal(isInspectOfferBait(bait1000Offer, new Set(), []), true); + assert.equal( + resolveInspectMasterMinPriceVnd({ + matchedOffer: bait1000Offer, + allOffers: [bait1000Offer, inStockPeerA], + master: { min_price: 1_000 }, + }), + 20_000_000 + ); + assert.equal( + resolveInspectMasterMinPriceVnd({ + matchedOffer: bait1000Offer, + allOffers: [bait1000Offer], + master: { min_price: 1_000 }, + }), + 0 + ); + assert.deepEqual( + resolveInspectVerdictPrices({ + matchedOffer: { + id: 'peer-a', + price_current: 500_000, + price_original: 25_000_000, + }, + allOffers: [inStockPeerA, inStockPeerB], + master: { min_price: 20_000_000, max_price: 21_500_000 }, + priceHistory: null, + }), + { + currentPriceVnd: 20_000_000, + claimedOriginalVnd: 0, + historicalLowVnd: 20_000_000, + historicalHighVnd: 21_500_000, + } + ); +} + +if (typeof require !== 'undefined' && require.main === module) { + runInspectVerdictGuardsSuite() + .then(() => { + console.warn('✅ Inspect verdict guards suite passed'); + }) + .catch((err: unknown) => { + console.error(err); + process.exit(1); + }); +} diff --git a/apps/web/src/lib/__tests__/offer-plausibility.test.ts b/apps/web/src/lib/__tests__/offer-plausibility.test.ts index 41e36bc3..d2684a3d 100644 --- a/apps/web/src/lib/__tests__/offer-plausibility.test.ts +++ b/apps/web/src/lib/__tests__/offer-plausibility.test.ts @@ -1187,6 +1187,10 @@ async function main() { ) ); + const { runInspectVerdictGuardsSuite } = + await import('./inspect-verdict-guards.test'); + await runInspectVerdictGuardsSuite(); + console.warn('✅ Offer plausibility suite passed'); } From c159704dd331b80ff60aa6953ca1b67ba3add2b4 Mon Sep 17 00:00:00 2001 From: JOY <5027251+JOY@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:53:35 +0700 Subject: [PATCH 07/24] fix(shared-data): guard prototype keys and lone surrogates in routes (#1823) * fix(shared-data): guard prototype keys and lone surrogates (#1813) * fix(shared-data): harden spec-template malformed runtime inputs (#1813) --- .../src/__tests__/shared-data.test.ts | 193 ++++++++++++++++++ packages/shared-data/src/constants/routes.ts | 22 +- .../shared-data/src/rules/spec-template.ts | 43 +++- 3 files changed, 249 insertions(+), 9 deletions(-) diff --git a/packages/shared-data/src/__tests__/shared-data.test.ts b/packages/shared-data/src/__tests__/shared-data.test.ts index 0d38302f..b655e6b5 100644 --- a/packages/shared-data/src/__tests__/shared-data.test.ts +++ b/packages/shared-data/src/__tests__/shared-data.test.ts @@ -102,6 +102,104 @@ function testRouteConstants(): void { '/api/v1/rate/merchants/' ); assert.equal(RATE_API_V1_ENDPOINTS.REVIEW_VOTE('..'), '/api/reviews//vote'); + // Lone UTF-16 surrogates and astral pairs straddling maxLen must never throw URIError (#1813). + assert.equal( + RATE_API_V1_ENDPOINTS.PRODUCT_DETAIL('iphone-\ud83d-16'), + '/api/v1/rate/products/iphone--16' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.PRODUCT_DETAIL('\ud800'), + '/api/v1/rate/products/' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.PRODUCT_DETAIL('\udc00\ud800'), + '/api/v1/rate/products/' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.PRODUCT_DETAIL('\ud83d\u0000\ude00'), + '/api/v1/rate/products/' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.MERCHANT_DETAIL('shop-\ude00'), + '/api/v1/rate/merchants/shop-' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.REVIEW_VOTE('\udc00rev\ud800'), + '/api/reviews/rev/vote' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.PRODUCT_DETAIL(`${'a'.repeat(179)}😀`), + `/api/v1/rate/products/${'a'.repeat(179)}` + ); + assert.equal( + RATE_API_V1_ENDPOINTS.PRODUCT_DETAIL(`${'a'.repeat(178)}😀`), + `/api/v1/rate/products/${'a'.repeat(178)}${encodeURIComponent('😀')}` + ); + assert.equal( + RATE_API_V1_ENDPOINTS.MERCHANT_DETAIL(`${'a'.repeat(127)}😀`), + `/api/v1/rate/merchants/${'a'.repeat(127)}` + ); + assert.equal( + RATE_API_V1_ENDPOINTS.MERCHANT_DETAIL(`${'a'.repeat(126)}😀`), + `/api/v1/rate/merchants/${'a'.repeat(126)}${encodeURIComponent('😀')}` + ); + assert.equal( + RATE_API_V1_ENDPOINTS.REVIEW_VOTE(`${'a'.repeat(127)}😀`), + `/api/reviews/${'a'.repeat(127)}/vote` + ); + assert.equal( + RATE_API_V1_ENDPOINTS.REVIEW_VOTE(`${'a'.repeat(126)}😀`), + `/api/reviews/${'a'.repeat(126)}${encodeURIComponent('😀')}/vote` + ); + // Post-truncation dot segments (. and ..) must still be rejected (#1813). + assert.equal( + RATE_API_V1_ENDPOINTS.PRODUCT_DETAIL(`.${' '.repeat(179)}x`), + '/api/v1/rate/products/' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.PRODUCT_DETAIL(`..${' '.repeat(178)}x`), + '/api/v1/rate/products/' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.PRODUCT_DETAIL(`..${' '.repeat(177)}😀`), + '/api/v1/rate/products/' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.MERCHANT_DETAIL(`.${' '.repeat(127)}x`), + '/api/v1/rate/merchants/' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.MERCHANT_DETAIL(`..${' '.repeat(126)}x`), + '/api/v1/rate/merchants/' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.REVIEW_VOTE(`..${' '.repeat(126)}x`), + '/api/reviews//vote' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.REVIEW_VOTE(`.${' '.repeat(126)}😀`), + '/api/reviews//vote' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.PRODUCT_DETAIL(`..${'\u00a0'.repeat(178)}x`), + '/api/v1/rate/products/' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.REVIEW_VOTE(`.${'\ufeff'.repeat(127)}x`), + '/api/reviews//vote' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.PRODUCT_DETAIL(null as never), + '/api/v1/rate/products/' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.MERCHANT_DETAIL(undefined as never), + '/api/v1/rate/merchants/' + ); + assert.equal( + RATE_API_V1_ENDPOINTS.REVIEW_VOTE(123 as never), + '/api/reviews//vote' + ); assert.equal(RATE_API_V1_ENDPOINTS.HEALTH, '/api/health'); assert.equal(ROUTES.API.LISTINGS, '/api/v1/rate/products'); assert.equal(STATUS_CODES.OK, 200); @@ -1168,6 +1266,101 @@ function testCategorySpecTemplates(): void { rules.specFieldApplies(watchTemplate, 'camera', 'main_camera'), false ); + + // Prototype keys fall back to PHONE_TEMPLATE without throwing (#1813). + const phoneTemplate = rules.getCategorySpecTemplate('smartphones'); + for (const protoSlug of [ + '__proto__', + 'constructor', + 'toString', + 'valueOf', + 'hasOwnProperty', + ' __PROTO__ ', + 'Constructor', + ]) { + assert.equal(rules.getCategorySpecTemplate(protoSlug), phoneTemplate); + assert.equal( + rules.applyCategorySpecTemplate(protoSlug, phoneShaped), + phoneShaped + ); + assert.equal( + rules.applyCategorySpecTemplate(protoSlug, phoneShaped, { + forStorage: true, + }), + phoneShaped + ); + assert.equal( + rules.specFieldApplies( + rules.getCategorySpecTemplate(protoSlug), + 'camera', + 'main_camera' + ), + true + ); + } + assert.equal( + rules.specFieldApplies( + watchTemplate, + '__proto__' as rules.SpecGroupKey, + 'toString' + ), + false + ); + assert.equal( + rules.specFieldApplies( + watchTemplate, + 'constructor' as rules.SpecGroupKey, + 'name' + ), + false + ); + const protoSpecsPayload = JSON.parse( + '{"__proto__":{"cellular":{"technology":"5G"}},"multimedia":{"audio_features":"ANC"}}' + ) as Record; + const protoBuds = rules.applyCategorySpecTemplate('earbuds', { + specifications: protoSpecsPayload, + }); + assert.equal( + Object.getPrototypeOf(protoBuds.specifications), + Object.prototype + ); + assert.equal( + Object.prototype.hasOwnProperty.call(protoBuds.specifications, 'cellular'), + false + ); + const shadowedSpecs = { + hasOwnProperty: 'not-a-fn', + cellular: { technology: 'LTE / 5G' }, + }; + const shadowedWatch = rules.applyCategorySpecTemplate('smartwatches', { + specifications: shadowedSpecs, + }); + assert.deepEqual( + (shadowedWatch.specifications as { cellular: unknown }).cellular, + { technology: 'LTE' } + ); + const nestedProtoHardware = JSON.parse( + '{"hardware":{"__proto__":{"gpu":"x"},"chipset":"H2"}}' + ) as Record; + const nestedProtoBuds = rules.applyCategorySpecTemplate('earbuds', { + specifications: nestedProtoHardware, + }); + const nestedHw = ( + nestedProtoBuds.specifications as { hardware: Record } + ).hardware; + assert.equal(Object.getPrototypeOf(nestedHw), Object.prototype); + assert.equal(nestedHw.gpu, undefined); + assert.equal(nestedHw.chipset, 'H2'); + assert.equal(rules.applyCategorySpecTemplate('earbuds', null as never), null); + assert.equal(rules.applyCategorySpecTemplate('earbuds', 123 as never), 123); + assert.deepEqual( + rules.applyCategorySpecTemplate('earbuds', phoneShaped, null as never), + buds + ); + assert.equal( + rules.specFieldApplies(null as never, 'camera', 'main_camera'), + false + ); } function assertRoundTrip(modelKey: string): void { diff --git a/packages/shared-data/src/constants/routes.ts b/packages/shared-data/src/constants/routes.ts index 2ebd9807..d6685478 100644 --- a/packages/shared-data/src/constants/routes.ts +++ b/packages/shared-data/src/constants/routes.ts @@ -24,12 +24,28 @@ export const RATE_ROUTES = { CONTACT_US: '/contact-us', } as const; +// Unpaired UTF-16 surrogates cause encodeURIComponent to throw URIError. +const LONE_SURROGATE_RE = + /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?= 0xd800 && lastCode <= 0xdbff + ? sliced.slice(0, -1) + : sliced; +} + function sanitizePathSegment(raw: unknown, maxLen: number): string { if (typeof raw !== 'string') return ''; - const cleaned = raw + const stripped = raw + .replace(LONE_SURROGATE_RE, '') .replace(/\p{Cc}+/gu, '') - .trim() - .slice(0, maxLen); + .trim(); + const cleaned = sliceUtf16Safe(stripped, maxLen).trim(); if (!cleaned || cleaned === '.' || cleaned === '..') return ''; return cleaned; } diff --git a/packages/shared-data/src/rules/spec-template.ts b/packages/shared-data/src/rules/spec-template.ts index b55b1537..80210833 100644 --- a/packages/shared-data/src/rules/spec-template.ts +++ b/packages/shared-data/src/rules/spec-template.ts @@ -145,20 +145,38 @@ const SPEC_TEMPLATES: Readonly> = { const normalizeSlug = (categorySlug: unknown): string => typeof categorySlug === 'string' ? categorySlug.trim().toLowerCase() : ''; +const ownValue = ( + record: Readonly> | null | undefined, + key: string +): T | undefined => + record && key && Object.prototype.hasOwnProperty.call(record, key) + ? record[key] + : undefined; + /** The template for a category slug; unknown slugs keep the full phone template. */ export function getCategorySpecTemplate( categorySlug: string | null | undefined ): CategorySpecTemplate { - return SPEC_TEMPLATES[normalizeSlug(categorySlug)] ?? PHONE_TEMPLATE; + return ( + ownValue(SPEC_TEMPLATES, normalizeSlug(categorySlug)) ?? PHONE_TEMPLATE + ); } +const ownList = ( + record: Partial> | null | undefined, + group: SpecGroupKey +): readonly string[] => { + const list = ownValue(record, group); + return Array.isArray(list) ? list : []; +}; + const hiddenIn = ( template: CategorySpecTemplate, group: SpecGroupKey, forStorage: boolean ): readonly string[] => [ - ...(template.hiddenFields[group] ?? []), - ...(forStorage ? [] : (template.displayHiddenFields[group] ?? [])), + ...ownList(template.hiddenFields, group), + ...(forStorage ? [] : ownList(template.displayHiddenFields, group)), ]; /** Whether the page shows a field of a group for the category. */ @@ -168,6 +186,8 @@ export function specFieldApplies( field: string ): boolean { return ( + Boolean(template) && + Array.isArray(template.groups) && template.groups.includes(group) && !hiddenIn(template, group, false).includes(field) ); @@ -245,8 +265,10 @@ const withoutFiveGText = ( export function applyCategorySpecTemplate( categorySlug: string | null | undefined, product: T, - { forStorage = false }: { forStorage?: boolean } = {} + options?: { forStorage?: boolean } | null ): T { + if (!isRecord(product)) return product; + const forStorage = options?.forStorage === true; const template = getCategorySpecTemplate(categorySlug); if (template === PHONE_TEMPLATE) return product; const out: T = { ...product }; @@ -255,7 +277,16 @@ export function applyCategorySpecTemplate( const specs: Record = {}; for (const [group, value] of Object.entries(product.specifications)) { if (!(ALL_GROUPS as readonly string[]).includes(group)) { - specs[group] = value; + if (group === '__proto__') { + Object.defineProperty(specs, group, { + value, + enumerable: true, + configurable: true, + writable: true, + }); + } else { + specs[group] = value; + } continue; } if (!template.groups.includes(group as SpecGroupKey)) continue; @@ -270,7 +301,7 @@ export function applyCategorySpecTemplate( } // The page hides 5G for the category, so it also leaves it out of the // free-text network fields. - const shownCellular = specs.cellular; + const shownCellular = ownValue(specs, 'cellular'); if (!forStorage && !template.has5g && isRecord(shownCellular)) { specs.cellular = withoutFiveGText(shownCellular); } From cc3c01b0bdd13534ab063b7421874ec35573d310 Mon Sep 17 00:00:00 2001 From: JOY <5027251+JOY@users.noreply.github.com> Date: Sun, 22 Nov 2026 02:01:01 +0700 Subject: [PATCH 08/24] fix(extension): prevent surrogate URIError, sync v2 consent, and harden radar UI (#1824) * fix(extension): prevent surrogate URIError, sync v2 consent, and harden radar UI Closes #1812 * fix(extension): serialize settings writes and harden consent/radar edge cases --- .../src/background/service-worker.ts | 31 +- .../src/extractors/ecommerce-detector.ts | 14 +- apps/extension/src/security-guards.ts | 38 +- apps/extension/src/sidepanel/panel-view.ts | 24 +- apps/extension/src/sidepanel/sidepanel.ts | 577 ++++++-- apps/extension/src/surrogate-safe.ts | 68 + apps/extension/src/test-extension-pipeline.ts | 1227 +++++++++++++++++ 7 files changed, 1833 insertions(+), 146 deletions(-) create mode 100644 apps/extension/src/surrogate-safe.ts diff --git a/apps/extension/src/background/service-worker.ts b/apps/extension/src/background/service-worker.ts index bed9ff0a..029c94ec 100644 --- a/apps/extension/src/background/service-worker.ts +++ b/apps/extension/src/background/service-worker.ts @@ -24,6 +24,8 @@ import { ONBOARDING_PAGE_PATH, sanitizeExtensionProductSignal, selectFreshestSessionSignal, + sliceCodePoints, + stripLoneSurrogates, } from '../security-guards.js'; import { LookupCooldown } from '../rate-limit.js'; import { @@ -217,6 +219,7 @@ let fullLookupRequestCounter = 0; const latestFullLookupByTab = new Map(); let latestSelectionCounter = 0; let latestPageSelectionCounter = 0; +let settingsUpdateChain: Promise = Promise.resolve(); let pendingQuickQueryReceipt: { updatedAt: number; apiBaseUrl: string; @@ -865,10 +868,12 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { try { const cleanedVariant = typeof message.selectedVariant === 'string' - ? message.selectedVariant - .replace(/\p{Cc}+/gu, ' ') - .trim() - .slice(0, 200) + ? sliceCodePoints( + stripLoneSurrogates(message.selectedVariant) + .replace(/\p{Cc}+/gu, ' ') + .trim(), + 200 + ) : ''; const overrideVariant = cleanedVariant || null; const [activeTab] = await chrome.tabs.query({ @@ -1061,10 +1066,12 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { try { const rawQuery = typeof message.query === 'string' - ? message.query - .replace(/\p{Cc}+/gu, ' ') - .trim() - .slice(0, 500) + ? sliceCodePoints( + stripLoneSurrogates(message.query) + .replace(/\p{Cc}+/gu, ' ') + .trim(), + 500 + ) : ''; const settings = await getSettings(); if (!rawQuery) { @@ -1190,7 +1197,7 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { }); return false; } - (async () => { + const runSettingsUpdate = async (): Promise => { try { const nextSettings: Partial = {}; let serverChanged = false; @@ -1232,7 +1239,11 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { error: err instanceof Error ? err.message : 'Settings update failed', }); } - })(); + }; + settingsUpdateChain = settingsUpdateChain.then( + runSettingsUpdate, + runSettingsUpdate + ); return true; } diff --git a/apps/extension/src/extractors/ecommerce-detector.ts b/apps/extension/src/extractors/ecommerce-detector.ts index 30af4123..fc06bf6c 100644 --- a/apps/extension/src/extractors/ecommerce-detector.ts +++ b/apps/extension/src/extractors/ecommerce-detector.ts @@ -3,6 +3,7 @@ import type { ExtractedStockState, SupportedEcommercePlatform, } from '../types.js'; +import { sliceCodePoints, stripLoneSurrogates } from '../surrogate-safe.js'; /** * Strip all tracking query strings (sp_atk, xptdk, utm_*, fbclid, etc.) and hashes. @@ -785,7 +786,7 @@ export function extractRatingFromJsonLd(aggRaw: unknown): { export function cleanExtractedProductTitle(rawTitle: string): string { if (typeof rawTitle !== 'string') return ''; - const withoutPlatformSuffix = rawTitle + const withoutPlatformSuffix = stripLoneSurrogates(rawTitle) .replace(/\p{Cc}+/gu, ' ') .replace( /\s*(?:\||[-–—])\s*(?:Shopee(?:\s*Việt\s*Nam|\s*VN)?|Lazada(?:\s*Việt\s*Nam|\s*VN)?|Tiki(?:\.vn)?|TikTok\s*Shop|CellphoneS(?:\.com\.vn)?|Thế\s*Giới\s*Di\s*Động|Thegioididong(?:\.com)?|TopZone(?:\.vn)?|Điện\s*Máy\s*Xanh|FPT\s*Shop|Fptshop(?:\.com\.vn)?|Hoàng\s*Hà\s*Mobile|HoangHaMobile(?:\.com)?|Di\s*Động\s*Việt|Didongviet(?:\.vn)?|Phong\s*Vũ|Phongvu(?:\.vn)?).*$/i, @@ -795,7 +796,7 @@ export function cleanExtractedProductTitle(rawTitle: string): string { .trim(); if (!withoutPlatformSuffix.includes('|')) { - return withoutPlatformSuffix.slice(0, 500); + return sliceCodePoints(withoutPlatformSuffix, 500); } const pipeSegments = withoutPlatformSuffix @@ -812,7 +813,10 @@ export function cleanExtractedProductTitle(rawTitle: string): string { } } - return keptSegments.join(' ').replace(/\s+/g, ' ').trim().slice(0, 500); + return sliceCodePoints( + keptSegments.join(' ').replace(/\s+/g, ' ').trim(), + 500 + ); } const CONCRETE_WAI_ARIA_ROLES = new Set([ @@ -944,7 +948,7 @@ export function extractSelectedVariantFromDom(doc: Document): string | null { return; } } - const txt = (node.textContent || '') + const txt = stripLoneSurrogates(node.textContent || '') .replace(/\p{Cc}+/gu, ' ') .replace(/\s+/g, ' ') .trim(); @@ -959,7 +963,7 @@ export function extractSelectedVariantFromDom(doc: Document): string | null { } if (pickedLabels.length > 0) { - return pickedLabels.join(' - ').slice(0, 200); + return sliceCodePoints(pickedLabels.join(' - '), 200) || null; } return null; } diff --git a/apps/extension/src/security-guards.ts b/apps/extension/src/security-guards.ts index e1cb1865..999c7863 100644 --- a/apps/extension/src/security-guards.ts +++ b/apps/extension/src/security-guards.ts @@ -7,6 +7,17 @@ import type { SupportedEcommercePlatform, } from './types.js'; import { t, type MessageKey } from './i18n.js'; +import { + encodeSurrogateSafeUriComponent, + sliceCodePoints, + stripLoneSurrogates, +} from './surrogate-safe.js'; + +export { + encodeSurrogateSafeUriComponent, + sliceCodePoints, + stripLoneSurrogates, +}; export const DEFAULT_API_BASE_URL = 'https://rate.vn'; @@ -351,19 +362,23 @@ export function sanitizeExtensionProductSignal( const trimmedTitle = typeof safeSignal.title === 'string' - ? safeSignal.title - .replace(/\p{Cc}+/gu, ' ') - .replace(/\s+/g, ' ') - .trim() - .slice(0, 500) + ? sliceCodePoints( + stripLoneSurrogates(safeSignal.title) + .replace(/\p{Cc}+/gu, ' ') + .replace(/\s+/g, ' ') + .trim(), + 500 + ) : ''; const trimmedVariant = typeof safeSignal.selectedVariant === 'string' - ? safeSignal.selectedVariant - .replace(/\p{Cc}+/gu, ' ') - .replace(/\s+/g, ' ') - .trim() - .slice(0, 200) || null + ? sliceCodePoints( + stripLoneSurrogates(safeSignal.selectedVariant) + .replace(/\p{Cc}+/gu, ' ') + .replace(/\s+/g, ' ') + .trim(), + 200 + ) || null : null; return { @@ -482,8 +497,7 @@ export function buildExtensionObservationV1( // v1 allows 120 characters; a longer label is cut, not a reason to // drop the observation. variant_label: signal.selectedVariant - ? Array.from(signal.selectedVariant).slice(0, 120).join('').trim() || - null + ? sliceCodePoints(signal.selectedVariant, 120).trim() || null : null, price_vnd: signal.price, list_price_vnd: signal.originalPrice, diff --git a/apps/extension/src/sidepanel/panel-view.ts b/apps/extension/src/sidepanel/panel-view.ts index 7884fd7e..2089efa7 100644 --- a/apps/extension/src/sidepanel/panel-view.ts +++ b/apps/extension/src/sidepanel/panel-view.ts @@ -20,6 +20,11 @@ import { resolveSafeApiRelativeUrl, } from '../security-guards.js'; import { contentTextHtml, escapeHtml, t, type MessageKey } from '../i18n.js'; +import { + encodeSurrogateSafeUriComponent, + sliceCodePoints, + stripLoneSurrogates, +} from '../surrogate-safe.js'; export { escapeHtml }; @@ -357,10 +362,12 @@ export function renderNotFoundHtml( apiBaseUrl: string, accessory: boolean ): string { - const query = String(signal.title ?? '').trim(); + const query = sliceCodePoints(String(signal.title ?? '').trim(), 500).trim(); const searchUrl = resolveSafeApiRelativeUrl( apiBaseUrl, - query ? `/inspect?q=${encodeURIComponent(query)}` : '/inspect', + query + ? `/inspect?q=${encodeSurrogateSafeUriComponent(query, 500)}` + : '/inspect', '/inspect' ); return ` @@ -404,7 +411,7 @@ export function renderLookupErrorHtml( productTitle?: string | null, retryAfterSeconds?: number | null ): string { - const title = String(productTitle ?? '').trim(); + const title = stripLoneSurrogates(String(productTitle ?? '')).trim(); // A rate limit says how long to wait when the server said so (DOS/Rate#1466). const wait = kind === 'rate_limited' && @@ -432,6 +439,7 @@ export function renderLookupErrorHtml( export interface PanelActionLink { href: string; label: string; + icon?: string; primary?: boolean; } @@ -445,10 +453,12 @@ export function renderActionRowsHtml(links: PanelActionLink[]): string { for (let i = 0; i < links.length; i += 2) { const cells = links .slice(i, i + 2) - .map( - (l) => - `${escapeHtml(l.label)}` - ) + .map((l) => { + const iconHtml = l.icon + ? `` + : ''; + return `${iconHtml}${escapeHtml(l.label)}`; + }) .join('\n '); rows.push( `

0 ? ' style="margin-top: 6px;"' : ''}> diff --git a/apps/extension/src/sidepanel/sidepanel.ts b/apps/extension/src/sidepanel/sidepanel.ts index 1b254c2e..998f73b0 100644 --- a/apps/extension/src/sidepanel/sidepanel.ts +++ b/apps/extension/src/sidepanel/sidepanel.ts @@ -8,9 +8,15 @@ import { escapeSafeExternalHref, formatSafeExtensionVnd, formatSafeNumber, + normalizeAllowedApiBaseUrl, resolveSafeApiRelativeUrl, sanitizeVerdictClassSuffix, } from '../security-guards.js'; +import { + encodeSurrogateSafeUriComponent, + sliceCodePoints, + stripLoneSurrogates, +} from '../surrogate-safe.js'; import { applyDocumentMessages, contentLangAttr, @@ -42,6 +48,13 @@ function formatVnd(val: number | null | undefined): string { return formatSafeExtensionVnd(val); } +function cleanUrlSegment(raw: unknown, maxCodePoints: number): string { + if (typeof raw !== 'string') return ''; + const cleaned = stripLoneSurrogates(raw).trim(); + if (!cleaned) return ''; + return sliceCodePoints(cleaned, maxCodePoints).trim(); +} + interface DealRadarFeedItem { id: string; verdict: string; @@ -81,10 +94,13 @@ async function fetchAndRenderDealRadar( ): Promise { const slot = document.getElementById('deal-radar-slot'); if (!slot) return; + const generationAtStart = panelLoadGeneration; + slot.innerHTML = ''; try { - const catParam = categorySlug - ? `&category=${encodeURIComponent(categorySlug)}` + const safeCategory = cleanUrlSegment(categorySlug, 120); + const catParam = safeCategory + ? `&category=${encodeSurrogateSafeUriComponent(safeCategory)}` : ''; const res = await fetch( `${settings.apiBaseUrl}/api/v1/rate/deals?limit=4${catParam}`, @@ -99,15 +115,32 @@ async function fetchAndRenderDealRadar( priceSpreadLeaders?: PriceSpreadFeedItem[]; }; }; - const deals = json?.data?.genuineDeals || []; - const traps = json?.data?.fakeSaleTraps || []; - const spreads = json?.data?.priceSpreadLeaders || []; + if (generationAtStart !== panelLoadGeneration) return; + if (!json || json.ok === false) return; + const deals = ( + Array.isArray(json.data?.genuineDeals) ? json.data.genuineDeals : [] + ).filter((d): d is DealRadarFeedItem => + Boolean(d && typeof d === 'object') + ); + const traps = ( + Array.isArray(json.data?.fakeSaleTraps) ? json.data.fakeSaleTraps : [] + ).filter((d): d is DealRadarFeedItem => + Boolean(d && typeof d === 'object') + ); + const spreads = ( + Array.isArray(json.data?.priceSpreadLeaders) + ? json.data.priceSpreadLeaders + : [] + ).filter((s): s is PriceSpreadFeedItem => + Boolean(s && typeof s === 'object') + ); if (deals.length === 0 && traps.length === 0 && spreads.length === 0) return; - slot.innerHTML = ` -
-
🔥 ${escapeHtml(t('radarDealsTitle'))}
+ const sections: string[] = []; + if (deals.length > 0) { + sections.push(` +
${escapeHtml(t('radarDealsTitle'))}
${deals .slice(0, 4) .map( @@ -116,11 +149,11 @@ async function fetchAndRenderDealRadar(
- ${escapeHtml(d.platform.toUpperCase())} (${contentTextHtml(d.merchantName)}) - ${escapeHtml(t('radarSavings', [formatVnd(d.savingsVsMedianVnd), formatSafeNumber(d.savingsVsMedianPct, { min: 0, max: 100 })]))} + ${escapeHtml(String(d.platform ?? '').toUpperCase())} (${contentTextHtml(stripLoneSurrogates(String(d.merchantName ?? '')))}) - ${escapeHtml(t('radarSavings', [formatVnd(d.savingsVsMedianVnd), formatSafeNumber(d.savingsVsMedianPct, { min: 0, max: 100 })]))}
@@ -130,58 +163,62 @@ async function fetchAndRenderDealRadar( ` ) .join('')} - ${ - spreads.length > 0 - ? ` -
-
📊 ${escapeHtml(t('radarSpreadTitle'))}
- ${spreads - .slice(0, 2) - .map( - (s) => ` -
- ` - ) - .join('')} + `); + } + if (spreads.length > 0) { + sections.push(` +
${escapeHtml(t('radarSpreadTitle'))}
+ ${spreads + .slice(0, 2) + .map( + (s) => ` +
+
+
${contentTextHtml(stripLoneSurrogates(String(s.productName ?? '')))}
+
+ ${escapeHtml(t('radarSpreadVersus', [String(s.cheapestPlatform ?? '').toUpperCase(), formatVnd(s.minPrice), String(s.priciestPlatform ?? '').toUpperCase(), formatVnd(s.maxPrice)]))} +
+
+ + ${escapeHtml(t('radarSpreadAmount', [formatVnd(s.spreadVnd)]))} +
` - : '' - } - ${ - traps.length > 0 - ? ` -
-
⚠️ ${escapeHtml(t('radarTrapsTitle'))}
- ${traps - .slice(0, 2) - .map( - (trap) => ` -
-
-
${contentTextHtml(trap.productName)}
-
- ${escapeHtml(trap.platform.toUpperCase())} - ${escapeHtml(t('radarTrapMarkup', [formatSafeNumber(trap.fakeMarkupPct, { min: 0, max: 10000 }), formatVnd(trap.priceOriginal)]))} -
-
- ${formatVnd(trap.priceCurrent)} -
- ` - ) - .join('')} + ) + .join('')} + `); + } + if (traps.length > 0) { + sections.push(` +
${escapeHtml(t('radarTrapsTitle'))}
+ ${traps + .slice(0, 2) + .map( + (trap) => ` +
+
+
${contentTextHtml(stripLoneSurrogates(String(trap.productName ?? '')))}
+
+ ${escapeHtml(String(trap.platform ?? '').toUpperCase())} - ${escapeHtml(t('radarTrapMarkup', [formatSafeNumber(trap.fakeMarkupPct, { min: 0, max: 10000 }), formatVnd(trap.priceOriginal)]))} +
+
+ ${formatVnd(trap.priceCurrent)}
` - : '' - } + ) + .join('')} + `); + } + + slot.innerHTML = ` +
+ ${sections + .map((section, idx) => + idx === 0 + ? `
${section}
` + : `
${section}
` + ) + .join('')}
`; } catch { @@ -211,6 +248,8 @@ async function fetchAndRenderMerchantTrustDirectory( ): Promise { const slot = document.getElementById('merchant-trust-slot'); if (!slot) return; + const generationAtStart = panelLoadGeneration; + slot.innerHTML = ''; try { const res = await fetch( @@ -222,33 +261,45 @@ async function fetchAndRenderMerchantTrustDirectory( ok?: boolean; data?: MerchantTrustFeedItem[]; }; - const allMerchants = json?.data || []; + if (generationAtStart !== panelLoadGeneration) return; + if (!json || json.ok === false) return; + const allMerchants = (Array.isArray(json.data) ? json.data : []).filter( + (m): m is MerchantTrustFeedItem => Boolean(m && typeof m === 'object') + ); if (allMerchants.length === 0) return; const sorted = [...allMerchants].sort((a, b) => { if (platform) { const aMatch = - a.platform.toLowerCase() === platform.toLowerCase() ? 1 : 0; + String(a.platform ?? '').toLowerCase() === platform.toLowerCase() + ? 1 + : 0; const bMatch = - b.platform.toLowerCase() === platform.toLowerCase() ? 1 : 0; + String(b.platform ?? '').toLowerCase() === platform.toLowerCase() + ? 1 + : 0; if (aMatch !== bMatch) return bMatch - aMatch; } // A store with no trust score sorts last (DOS/Rate#1484). return ( (b.trust_score ?? -1) - (a.trust_score ?? -1) || - b.product_count - a.product_count + (Number(b.product_count) || 0) - (Number(a.product_count) || 0) ); }); slot.innerHTML = `
-
🛡️ ${escapeHtml(t('merchantDirTitle', [formatSafeNumber(allMerchants.length, { min: 0 })]))}
+
${escapeHtml(t('merchantDirTitle', [formatSafeNumber(allMerchants.length, { min: 0 })]))}
${sorted .slice(0, 5) .map((m) => { + const merchantSegment = + cleanUrlSegment(m.slug, 200) || cleanUrlSegment(m.id, 200); const detailHref = resolveSafeApiRelativeUrl( settings.apiBaseUrl, - `/merchants/${encodeURIComponent(m.slug || m.id)}`, + merchantSegment + ? `/merchants/${encodeSurrogateSafeUriComponent(merchantSegment)}` + : '/merchants', '/merchants' ); const trustColor = @@ -260,12 +311,12 @@ async function fetchAndRenderMerchantTrustDirectory(
- ${escapeHtml(m.platform.toUpperCase())} (${escapeHtml(t('merchantProductCount', [formatSafeNumber(m.product_count, { min: 0 })]))}) - ${escapeHtml(t('merchantScamRisk', [riskScoreText(m.dosafe_risk_score)]))} | ${escapeHtml(t('merchantReviewRisk', [riskScoreText(m.review_risk_score)]))} + ${escapeHtml(String(m.platform ?? '').toUpperCase())} (${escapeHtml(t('merchantProductCount', [formatSafeNumber(m.product_count, { min: 0 })]))}) - ${escapeHtml(t('merchantScamRisk', [riskScoreText(m.dosafe_risk_score)]))} | ${escapeHtml(t('merchantReviewRisk', [riskScoreText(m.review_risk_score)]))}
@@ -474,7 +525,7 @@ function renderPanelContent( ${subBadges .map( (sr) => - `🏆 #${formatSafeNumber(sr.rank, { min: 1 })} ${escapeHtml(focusLabel(sr.criterion))} (${formatSafeNumber(sr.score10, { min: 0, max: 10, decimals: 1 })})` + `#${formatSafeNumber(sr.rank, { min: 1 })} ${escapeHtml(focusLabel(sr.criterion))} (${formatSafeNumber(sr.score10, { min: 0, max: 10, decimals: 1 })})` ) .join('')}
` @@ -507,20 +558,25 @@ function renderPanelContent( `/compare?category=${categorySlug}` ) : null; + const rawInspectTarget = + cleanUrlSegment(signal.canonicalUrl, 2048) || + cleanUrlSegment(master?.slug, 200) || + cleanUrlSegment(signal.title, 500); const inspectUrl = resolveSafeApiRelativeUrl( settings.apiBaseUrl, - `/inspect?q=${encodeURIComponent( - signal.canonicalUrl || master?.slug || signal.title - )}`, + rawInspectTarget + ? `/inspect?q=${encodeSurrogateSafeUriComponent(rawInspectTarget)}` + : '/inspect', '/inspect' ); // Alerts are per size: an Item page with no stated size adds nothing until // a size is picked (DOS/Rate#1158). const sizeUnknown = Boolean(lookup.item) && !lookup.selectedVariant; + const watchlistSlug = !sizeUnknown ? cleanUrlSegment(master?.slug, 200) : ''; const watchlistUrl = resolveSafeApiRelativeUrl( settings.apiBaseUrl, - master?.slug && !sizeUnknown - ? `/watchlist?add=${encodeURIComponent(master.slug)}` + watchlistSlug + ? `/watchlist?add=${encodeSurrogateSafeUriComponent(watchlistSlug)}` : '/watchlist', '/watchlist' ); @@ -532,12 +588,12 @@ function renderPanelContent( ? // Only a 3+ offer floor is the multi-platform floor; fewer offers are // just below the median (DOS/Rate#1145). radarSig.genuineDeal?.verdict === 'genuine_all_time_low' - ? `🔥 ${escapeHtml(t('radarPillDeal', [formatSafeNumber(radarSig.savingsPct, { min: 0, max: 100 })]))}` - : `✅ ${escapeHtml(t('radarPillBelowMedian', [formatSafeNumber(radarSig.savingsPct, { min: 0, max: 100 })]))}` + ? `${escapeHtml(t('radarPillDeal', [formatSafeNumber(radarSig.savingsPct, { min: 0, max: 100 })]))}` + : `${escapeHtml(t('radarPillBelowMedian', [formatSafeNumber(radarSig.savingsPct, { min: 0, max: 100 })]))}` : radarSig.verdict === 'fake_sale_trap' - ? `🚨 ${escapeHtml(t('radarPillTrap', [formatSafeNumber(radarSig.fakeMarkupPct, { min: 0, max: 10000 })]))}` + ? `${escapeHtml(t('radarPillTrap', [formatSafeNumber(radarSig.fakeMarkupPct, { min: 0, max: 10000 })]))}` : Number.isFinite(radarSpreadPct) && radarSpreadPct >= 5 - ? `📊 ${escapeHtml(t('radarPillSpread', [formatSafeNumber(radarSig.spreadPct, { min: 0, max: 10000 })]))}` + ? `${escapeHtml(t('radarPillSpread', [formatSafeNumber(radarSig.spreadPct, { min: 0, max: 10000 })]))}` : '' : ''; @@ -546,10 +602,12 @@ function renderPanelContent( const actionLinks: PanelActionLink[] = [ { href: detailUrl, label: t('linkDetail'), primary: true }, { href: compareUrl, label: t('linkCompare') }, - { href: leaderboardUrl, label: `🏆 ${t('linkLeaderboard')}` }, - ...(versusUrl ? [{ href: versusUrl, label: `⚔️ ${t('linkVersus')}` }] : []), - { href: inspectUrl, label: `🔬 ${t('linkInspect')}` }, - { href: watchlistUrl, label: `🔔 ${t('linkWatch')}` }, + { href: leaderboardUrl, icon: '🏆', label: t('linkLeaderboard') }, + ...(versusUrl + ? [{ href: versusUrl, icon: '⚔️', label: t('linkVersus') }] + : []), + { href: inspectUrl, icon: '🔬', label: t('linkInspect') }, + { href: watchlistUrl, icon: '🔔', label: t('linkWatch') }, ]; root.innerHTML = ` @@ -601,7 +659,36 @@ function renderPanelContent( }); } +const OPT_IN_CONSENT_VERSION = 2; + let panelLoadGeneration = 0; +let consentHydrated = false; +let apiBaseHydrated = false; +let sawRawConsentStorage = false; +let storedOptIn = false; +let storedConsentVersion = 0; +let lastConfirmedOptIn = false; +let consentMutationSeq = 0; +let optInOpCounter = 0; +let lastConfirmedOptInSeq = 0; +let optInSaveInFlight = 0; +let bufferedConsentUpdate: { + hasOptIn: boolean; + optIn: boolean; + hasVersion: boolean; + consentVersion: number; +} | null = null; + +let lastConfirmedApiBaseUrl = DEFAULT_API_BASE_URL; +let lastLoadedPanelApiBaseUrl = DEFAULT_API_BASE_URL; +let apiBaseMutationSeq = 0; +let apiBaseOpCounter = 0; +let lastConfirmedApiBaseSeq = 0; +let apiBaseSaveInFlight = 0; + +function isEffectiveConsentOptIn(): boolean { + return storedOptIn && storedConsentVersion === OPT_IN_CONSENT_VERSION; +} /** * A full lookup the service worker marks stale (a newer tab detection started @@ -628,6 +715,8 @@ async function loadSidePanel( ) as HTMLInputElement | null; if (!root) return; const generation = ++panelLoadGeneration; + const consentSeqAtStart = consentMutationSeq; + const apiSeqAtStart = apiBaseMutationSeq; try { const res = (await chrome.runtime.sendMessage({ @@ -650,16 +739,51 @@ async function loadSidePanel( return; } - const settings: ExtensionSettings = res?.settings || { - apiBaseUrl: DEFAULT_API_BASE_URL, - crowdContributeOptIn: false, + const rawSettings = res?.settings; + const settings: ExtensionSettings = { + apiBaseUrl: normalizeAllowedApiBaseUrl(rawSettings?.apiBaseUrl), + crowdContributeOptIn: rawSettings?.crowdContributeOptIn === true, }; + lastLoadedPanelApiBaseUrl = settings.apiBaseUrl; - if (chkOptIn) { - chkOptIn.checked = Boolean(settings.crowdContributeOptIn); + if ( + rawSettings && + consentMutationSeq === consentSeqAtStart && + optInSaveInFlight === 0 + ) { + if (!sawRawConsentStorage) { + storedOptIn = settings.crowdContributeOptIn; + storedConsentVersion = 0; + lastConfirmedOptIn = settings.crowdContributeOptIn; + if (chkOptIn) { + chkOptIn.checked = settings.crowdContributeOptIn; + } + } else if (consentHydrated) { + lastConfirmedOptIn = isEffectiveConsentOptIn(); + if (chkOptIn) { + chkOptIn.checked = lastConfirmedOptIn; + } + } + } else if ( + !rawSettings && + chkOptIn && + optInSaveInFlight === 0 && + consentHydrated && + sawRawConsentStorage + ) { + chkOptIn.checked = isEffectiveConsentOptIn(); } - if (inpApiBase && !inpApiBase.value) { - inpApiBase.value = settings.apiBaseUrl; + + if (apiBaseMutationSeq === apiSeqAtStart && apiBaseSaveInFlight === 0) { + if ( + typeof rawSettings?.apiBaseUrl === 'string' && + rawSettings.apiBaseUrl.trim() + ) { + lastConfirmedApiBaseUrl = settings.apiBaseUrl; + } + if (inpApiBase && !inpApiBase.value.trim()) { + inpApiBase.value = lastConfirmedApiBaseUrl; + } } if (res?.ok === false) { @@ -739,9 +863,10 @@ async function runQuickQuery(query: string): Promise { }; if (generation !== panelLoadGeneration) return; - const settings: ExtensionSettings = res?.settings || { - apiBaseUrl: DEFAULT_API_BASE_URL, - crowdContributeOptIn: false, + const rawSettings = res?.settings; + const settings: ExtensionSettings = { + apiBaseUrl: normalizeAllowedApiBaseUrl(rawSettings?.apiBaseUrl), + crowdContributeOptIn: rawSettings?.crowdContributeOptIn === true, }; if (res?.ok === false) { @@ -790,6 +915,34 @@ document.addEventListener('DOMContentLoaded', () => { 'inp-api-base' ) as HTMLInputElement | null; + const hasLocalStorageGet = + typeof chrome !== 'undefined' && + typeof chrome.storage?.local?.get === 'function'; + + consentHydrated = !hasLocalStorageGet; + apiBaseHydrated = !hasLocalStorageGet; + sawRawConsentStorage = hasLocalStorageGet; + storedOptIn = false; + storedConsentVersion = 0; + lastConfirmedOptIn = false; + consentMutationSeq = 0; + optInOpCounter = 0; + lastConfirmedOptInSeq = 0; + optInSaveInFlight = 0; + bufferedConsentUpdate = null; + + lastConfirmedApiBaseUrl = inpApiBase?.value.trim() + ? normalizeAllowedApiBaseUrl(inpApiBase.value) + : DEFAULT_API_BASE_URL; + lastLoadedPanelApiBaseUrl = lastConfirmedApiBaseUrl; + apiBaseMutationSeq = 0; + apiBaseOpCounter = 0; + lastConfirmedApiBaseSeq = 0; + apiBaseSaveInFlight = 0; + if (chkOptIn) { + chkOptIn.checked = false; + } + btnRefresh?.addEventListener('click', () => { void loadSidePanel(); }); @@ -801,40 +954,240 @@ document.addEventListener('DOMContentLoaded', () => { } }); - chrome.storage.onChanged?.addListener((changes, areaName) => { - const change = changes.crowdContributeOptIn; - if (areaName === 'local' && change && chkOptIn) { - chkOptIn.checked = change.newValue === true; + const finishStorageHydration = ( + stored: Record | undefined + ) => { + if (!consentHydrated) { + const buffered = bufferedConsentUpdate; + bufferedConsentUpdate = null; + sawRawConsentStorage = true; + const baseOptIn = stored?.crowdContributeOptIn === true; + const baseVersion = + typeof stored?.crowdConsentVersion === 'number' + ? stored.crowdConsentVersion + : 0; + storedOptIn = buffered?.hasOptIn ? buffered.optIn : baseOptIn; + storedConsentVersion = buffered?.hasVersion + ? buffered.consentVersion + : baseVersion; + lastConfirmedOptIn = isEffectiveConsentOptIn(); + consentHydrated = true; + if (optInSaveInFlight === 0 && chkOptIn) { + chkOptIn.checked = lastConfirmedOptIn; + } + } + + if (!apiBaseHydrated) { + apiBaseHydrated = true; + if ( + apiBaseMutationSeq === 0 && + apiBaseSaveInFlight === 0 && + stored && + typeof stored.apiBaseUrl === 'string' && + stored.apiBaseUrl.trim() + ) { + const normalized = normalizeAllowedApiBaseUrl(stored.apiBaseUrl); + lastConfirmedApiBaseUrl = normalized; + if (inpApiBase && !inpApiBase.value.trim()) { + inpApiBase.value = normalized; + } + } + } + }; + + if (hasLocalStorageGet) { + void Promise.resolve( + chrome.storage.local.get([ + 'crowdContributeOptIn', + 'crowdConsentVersion', + 'apiBaseUrl', + ]) + ) + .then((stored) => { + finishStorageHydration(stored); + }) + .catch(() => { + finishStorageHydration(undefined); + }); + } + + chrome.storage?.onChanged?.addListener((changes, areaName) => { + if (areaName === 'session') { + if (changes.lookup_tab_latest) { + void loadSidePanel(); + } + return; + } + if (areaName !== 'local') return; + + if (Object.prototype.hasOwnProperty.call(changes, 'apiBaseUrl')) { + const rawNewApiBase = changes.apiBaseUrl?.newValue; + const nextApiBase = normalizeAllowedApiBaseUrl( + typeof rawNewApiBase === 'string' ? rawNewApiBase : null + ); + apiBaseHydrated = true; + lastConfirmedApiBaseUrl = nextApiBase; + if (apiBaseSaveInFlight === 0) { + apiBaseMutationSeq += 1; + if (inpApiBase) { + inpApiBase.value = nextApiBase; + } + } + } + + const hasOptIn = Object.prototype.hasOwnProperty.call( + changes, + 'crowdContributeOptIn' + ); + const hasVersion = Object.prototype.hasOwnProperty.call( + changes, + 'crowdConsentVersion' + ); + if (!hasOptIn && !hasVersion) return; + + consentMutationSeq += 1; + sawRawConsentStorage = true; + const nextOptInValue = changes.crowdContributeOptIn?.newValue === true; + const nextVersionValue = + typeof changes.crowdConsentVersion?.newValue === 'number' + ? changes.crowdConsentVersion.newValue + : 0; + + if (!consentHydrated) { + if (hasOptIn && hasVersion) { + consentHydrated = true; + bufferedConsentUpdate = null; + storedOptIn = nextOptInValue; + storedConsentVersion = nextVersionValue; + lastConfirmedOptIn = isEffectiveConsentOptIn(); + if (optInSaveInFlight === 0 && chkOptIn) { + chkOptIn.checked = lastConfirmedOptIn; + } + return; + } + bufferedConsentUpdate = { + hasOptIn: hasOptIn || Boolean(bufferedConsentUpdate?.hasOptIn), + optIn: hasOptIn + ? nextOptInValue + : (bufferedConsentUpdate?.optIn ?? false), + hasVersion: hasVersion || Boolean(bufferedConsentUpdate?.hasVersion), + consentVersion: hasVersion + ? nextVersionValue + : (bufferedConsentUpdate?.consentVersion ?? 0), + }; + if ( + (hasOptIn && !nextOptInValue) || + (hasVersion && nextVersionValue !== OPT_IN_CONSENT_VERSION) + ) { + lastConfirmedOptIn = false; + if (optInSaveInFlight === 0 && chkOptIn) { + chkOptIn.checked = false; + } + } + return; + } + + if (hasOptIn) { + storedOptIn = nextOptInValue; + } + if (hasVersion) { + storedConsentVersion = nextVersionValue; + } + lastConfirmedOptIn = isEffectiveConsentOptIn(); + if (optInSaveInFlight === 0 && chkOptIn) { + chkOptIn.checked = lastConfirmedOptIn; } }); chkOptIn?.addEventListener('change', async () => { - await chrome.runtime.sendMessage({ - type: 'RATE_UPDATE_SETTINGS', - crowdContributeOptIn: chkOptIn.checked, - }); + const desired = Boolean(chkOptIn.checked); + consentMutationSeq += 1; + const opSeq = ++optInOpCounter; + optInSaveInFlight += 1; + try { + const res = (await chrome.runtime.sendMessage({ + type: 'RATE_UPDATE_SETTINGS', + crowdContributeOptIn: desired, + })) as { ok?: boolean; settings?: ExtensionSettings } | undefined; + if ( + !res || + res.ok === false || + typeof res.settings?.crowdContributeOptIn !== 'boolean' || + res.settings.crowdContributeOptIn !== desired + ) { + throw new Error('Settings update failed'); + } + if (opSeq >= lastConfirmedOptInSeq) { + lastConfirmedOptInSeq = opSeq; + consentHydrated = true; + bufferedConsentUpdate = null; + sawRawConsentStorage = true; + storedOptIn = desired; + storedConsentVersion = desired ? OPT_IN_CONSENT_VERSION : 0; + lastConfirmedOptIn = desired; + } + } catch { + // Reconciled against latest confirmed state in finally once in-flight saves settle. + } finally { + optInSaveInFlight = Math.max(0, optInSaveInFlight - 1); + if (optInSaveInFlight === 0 && chkOptIn) { + chkOptIn.checked = + sawRawConsentStorage && consentHydrated + ? isEffectiveConsentOptIn() + : lastConfirmedOptIn; + } + } }); inpApiBase?.addEventListener('change', async () => { - if (inpApiBase.value.trim()) { + const rawInput = inpApiBase.value.trim(); + const requestedApiBase = rawInput || DEFAULT_API_BASE_URL; + apiBaseMutationSeq += 1; + const opSeq = ++apiBaseOpCounter; + apiBaseSaveInFlight += 1; + let succeeded = false; + if (!rawInput) { + inpApiBase.value = DEFAULT_API_BASE_URL; + } + try { const updated = (await chrome.runtime.sendMessage({ type: 'RATE_UPDATE_SETTINGS', - apiBaseUrl: inpApiBase.value.trim(), - })) as { settings?: ExtensionSettings } | undefined; - if (updated?.settings?.apiBaseUrl) { - inpApiBase.value = updated.settings.apiBaseUrl; + apiBaseUrl: requestedApiBase, + })) as { ok?: boolean; settings?: ExtensionSettings } | undefined; + if ( + !updated || + updated.ok === false || + typeof updated.settings?.apiBaseUrl !== 'string' || + !updated.settings.apiBaseUrl.trim() + ) { + throw new Error('API base URL update failed'); + } + const savedApiBase = normalizeAllowedApiBaseUrl( + updated.settings.apiBaseUrl + ); + succeeded = true; + apiBaseHydrated = true; + if (opSeq >= lastConfirmedApiBaseSeq) { + lastConfirmedApiBaseSeq = opSeq; + lastConfirmedApiBaseUrl = savedApiBase; + } + } catch { + // Reconciled against lastConfirmedApiBaseUrl in finally once in-flight saves settle. + } finally { + apiBaseSaveInFlight = Math.max(0, apiBaseSaveInFlight - 1); + if (apiBaseSaveInFlight === 0) { + if (inpApiBase) { + inpApiBase.value = lastConfirmedApiBaseUrl; + } + if ( + succeeded || + lastConfirmedApiBaseUrl !== lastLoadedPanelApiBaseUrl + ) { + void loadSidePanel(); + } } - void loadSidePanel(); } }); - if (typeof chrome !== 'undefined' && chrome.storage?.onChanged) { - chrome.storage.onChanged.addListener((changes, areaName) => { - if (areaName === 'session' && changes.lookup_tab_latest) { - void loadSidePanel(); - } - }); - } - void loadSidePanel(); }); diff --git a/apps/extension/src/surrogate-safe.ts b/apps/extension/src/surrogate-safe.ts new file mode 100644 index 00000000..e45f149a --- /dev/null +++ b/apps/extension/src/surrogate-safe.ts @@ -0,0 +1,68 @@ +/** + * Surrogate-safe Unicode helpers for product titles, variant labels, and URL + * query/path components (DOS/Rate#1812). + * + * JavaScript `.slice()` cuts by UTF-16 code units, which can split an astral + * character (emoji, mathematical bold text, supplementary ideographs) into an + * unpaired high surrogate (`0xD800..0xDBFF`) at the boundary and cause + * `encodeURIComponent` to throw `URIError`. + */ + +const LONE_SURROGATE_RE = + /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?= 0xd800 && code <= 0xdbff) { + const next = i + 1 < value.length ? value.charCodeAt(i + 1) : 0; + if (next >= 0xdc00 && next <= 0xdfff) { + out += value.slice(i, i + 2); + i += 1; + count += 1; + } + continue; + } + if (code >= 0xdc00 && code <= 0xdfff) { + continue; + } + out += value[i]!; + count += 1; + } + return out; +} + +/** + * Encodes a URI component after stripping lone surrogates (and optionally + * truncating by Unicode code points), guaranteeing `encodeURIComponent` never + * throws `URIError`. + */ +export function encodeSurrogateSafeUriComponent( + value: unknown, + maxCodePoints?: number +): string { + const safe = + typeof maxCodePoints === 'number' + ? sliceCodePoints(value, maxCodePoints) + : stripLoneSurrogates(value); + return encodeURIComponent(safe); +} diff --git a/apps/extension/src/test-extension-pipeline.ts b/apps/extension/src/test-extension-pipeline.ts index f81b8a1e..88d941b7 100644 --- a/apps/extension/src/test-extension-pipeline.ts +++ b/apps/extension/src/test-extension-pipeline.ts @@ -9701,6 +9701,1233 @@ async function runTests() { } } } + + // DOS/Rate#1812: surrogate-safe code-point truncation, side panel consent version sync, + // settings UI rollback + cleared inpApiBase default restore, and Deal Radar UI/a11y fixes. + { + const { + encodeSurrogateSafeUriComponent, + sliceCodePoints, + stripLoneSurrogates, + } = await import('./surrogate-safe.js'); + const { + buildExtensionObservationV1, + buildLookupParams, + sanitizeExtensionProductSignal, + } = await import('./security-guards.js'); + const { renderActionRowsHtml, renderLookupErrorHtml, renderNotFoundHtml } = + await import('./sidepanel/panel-view.js'); + const { t } = await import('./i18n.js'); + + const hasLoneSurrogate = (s: string) => + /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(? encodeURIComponent(titleStraddling500.slice(0, 500)), + URIError + ); + + const safe500 = sliceCodePoints(titleStraddling500, 500); + assert.equal(safe500, `${'a'.repeat(499)}🔥`); + assert.equal(Array.from(safe500).length, 500); + assert.equal(hasLoneSurrogate(safe500), false); + assert.doesNotThrow(() => encodeURIComponent(safe500)); + assert.equal( + encodeSurrogateSafeUriComponent(titleStraddling500, 500), + `${'a'.repeat(499)}%F0%9F%94%A5` + ); + + const boldMathTitle = `${'𝗶'.repeat(505)} | Shopee VN`; + const cleanedMathTitle = cleanExtractedProductTitle(boldMathTitle); + assert.equal(Array.from(cleanedMathTitle).length, 500); + assert.equal(hasLoneSurrogate(cleanedMathTitle), false); + assert.doesNotThrow(() => encodeURIComponent(cleanedMathTitle)); + + const cleanedStraddleNoPipe = cleanExtractedProductTitle( + `${'x'.repeat(499)}🔥 tail | Lazada VN` + ); + assert.equal(cleanedStraddleNoPipe, `${'x'.repeat(499)}🔥`); + assert.equal(hasLoneSurrogate(cleanedStraddleNoPipe), false); + assert.doesNotThrow(() => encodeURIComponent(cleanedStraddleNoPipe)); + + const cleanedStraddleWithPipe = cleanExtractedProductTitle( + `${'y'.repeat(493)} | 256GB🔥 | Tiki.vn` + ); + // 'y'*493 + ' 256GB' = 499 BMP chars + '🔥' = 500 code points (straddling UTF-16 index 500). + assert.equal(cleanedStraddleWithPipe, `${'y'.repeat(493)} 256GB🔥`); + assert.equal(hasLoneSurrogate(cleanedStraddleWithPipe), false); + assert.doesNotThrow(() => encodeURIComponent(cleanedStraddleWithPipe)); + + const cleanedLoneSurrogateTitle = cleanExtractedProductTitle( + 'Điện thoại \uD83D iPhone 16 Pro \uDC00 | Shopee' + ); + assert.equal(cleanedLoneSurrogateTitle, 'Điện thoại iPhone 16 Pro'); + + // Variant straddling the 200-char limit in extractSelectedVariantFromDom & security-guards. + const variantDomStraddle = { + querySelectorAll: (sel: string) => + sel === 'button.product-variation--selected' + ? [ + { textContent: 'A'.repeat(55) }, + { textContent: 'B'.repeat(55) }, + { textContent: 'C'.repeat(55) }, + { textContent: `${'D'.repeat(25)}🔥X` }, + ] + : [], + } as unknown as Document; + // Joined: 55 + 3 + 55 + 3 + 55 + 3 + 25 = 199 BMP chars + '🔥' at 200th code point! + const extractedVariant = extractSelectedVariantFromDom(variantDomStraddle); + assert.ok(extractedVariant && extractedVariant.endsWith('🔥')); + assert.equal(Array.from(extractedVariant!).length, 200); + assert.equal(hasLoneSurrogate(extractedVariant!), false); + assert.doesNotThrow(() => encodeURIComponent(extractedVariant!)); + + const loneOnlyVariantDom = { + querySelectorAll: (sel: string) => + sel === 'button.product-variation--selected' + ? [{ textContent: '\uD800\uDC00'.slice(0, 1) }] + : [], + } as unknown as Document; + assert.equal(extractSelectedVariantFromDom(loneOnlyVariantDom), null); + + const sanitizedStraddle = sanitizeExtensionProductSignal({ + platform: 'shopee', + canonicalUrl: 'https://shopee.vn/p-i.1.2', + externalItemId: '2', + externalShopId: '1', + title: `${'a'.repeat(499)}🔥 extra`, + selectedVariant: `${'v'.repeat(199)}🔥 extra`, + price: 20_000_000, + originalPrice: null, + ratingStar: null, + ratingCount: null, + }); + assert.equal(sanitizedStraddle.title, `${'a'.repeat(499)}🔥`); + assert.equal(sanitizedStraddle.selectedVariant, `${'v'.repeat(199)}🔥`); + assert.equal(hasLoneSurrogate(sanitizedStraddle.title), false); + assert.equal( + hasLoneSurrogate(sanitizedStraddle.selectedVariant ?? ''), + false + ); + const lookupParams = buildLookupParams(sanitizedStraddle, 'full'); + assert.equal(lookupParams.get('title'), `${'a'.repeat(499)}🔥`); + assert.equal(lookupParams.get('selectedVariant'), `${'v'.repeat(199)}🔥`); + assert.equal(lookupParams.get('title')?.includes('\uFFFD'), false); + + const obsWithLoneVariant = buildExtensionObservationV1( + { + ...sanitizedStraddle, + selectedVariant: '256GB \uD83D Natural 🔥', + }, + new Date('2026-11-21T12:00:00Z'), + '2.1.0' + ); + assert.ok(obsWithLoneVariant); + assert.equal(obsWithLoneVariant.variant_label, '256GB Natural 🔥'); + + // panel-view renderNotFoundHtml & renderLookupErrorHtml never throw URIError on lone surrogates or straddling emoji. + const notFoundStraddleHtml = renderNotFoundHtml( + { title: `${'z'.repeat(499)}🔥\uD83D` }, + 'https://rate.vn', + false + ); + assert.ok( + notFoundStraddleHtml.includes( + `href="https://rate.vn/inspect?q=${'z'.repeat(499)}%F0%9F%94%A5"` + ) + ); + assert.equal(hasLoneSurrogate(notFoundStraddleHtml), false); + + const notFoundLoneOnlyHtml = renderNotFoundHtml( + { title: '\uD83D' }, + 'https://rate.vn', + false + ); + assert.ok(notFoundLoneOnlyHtml.includes('href="https://rate.vn/inspect"')); + assert.equal(hasLoneSurrogate(notFoundLoneOnlyHtml), false); + + const errLoneHtml = renderLookupErrorHtml('network', 'Phone \uD83D 16'); + assert.equal(hasLoneSurrogate(errLoneHtml), false); + + const actionIconRows = renderActionRowsHtml([ + { + href: 'https://rate.vn/inspect?q=test', + icon: '🔬', + label: 'Kiểm tra chi tiết', + }, + ]); + assert.ok( + actionIconRows.includes( + 'Kiểm tra chi tiết' + ) + ); + + // 2. Runtime verification of sidepanel.ts: + // - surrogate-safe inspect/watchlist/radar/merchant URL encoding (no false lookupErrorNetwork) + // - crowdConsentVersion === 2 gating on hydration & storage.onChanged + // - try/catch + UI rollback on chkOptIn and inpApiBase change handlers + // - cleared inpApiBase reset to DEFAULT_API_BASE_URL + // - Deal Radar hiding radarDealsTitle when deals.length === 0, json.ok === false, null platform guards, and aria-hidden emojis. + const g = globalThis as Record; + const savedGlobals = ['chrome', 'document', 'fetch'].map( + (key) => [key, Object.getOwnPropertyDescriptor(g, key)] as const + ); + const unhandled1812: unknown[] = []; + const onUnhandled1812 = (reason: unknown) => { + unhandled1812.push(reason); + }; + process.on('unhandledRejection', onUnhandled1812); + const settle = () => new Promise((r) => setTimeout(r, 15)); + + try { + const root = { + innerHTML: '', + querySelector: () => null, + querySelectorAll: () => [], + }; + const radarSlot = { innerHTML: '' }; + const trustSlot = { innerHTML: '' }; + const chkHandlers: Array<() => unknown> = []; + const apiHandlers: Array<() => unknown> = []; + const submitHandlers: Array<(e: { preventDefault: () => void }) => void> = + []; + const chkOptIn = { + checked: false, + addEventListener: (ev: string, cb: () => unknown) => { + if (ev === 'change') chkHandlers.push(cb); + }, + }; + const inpApiBase = { + value: '', + addEventListener: (ev: string, cb: () => unknown) => { + if (ev === 'change') apiHandlers.push(cb); + }, + }; + const inpQuickQuery = { value: '' }; + const formQuickInspect = { + addEventListener: ( + ev: string, + cb: (e: { preventDefault: () => void }) => void + ) => { + if (ev === 'submit') submitHandlers.push(cb); + }, + }; + + let onDomReady: (() => void) | undefined; + const storageListeners: Array< + (changes: Record, areaName: string) => void + > = []; + const sentMessages: Array> = []; + const fetchedRadarUrls: string[] = []; + + const storedLocalState: Record = { + apiBaseUrl: 'http://localhost:3000', + crowdContributeOptIn: true, + crowdConsentVersion: 1, + }; + let localGetImpl = async () => ({ ...storedLocalState }); + + let dealsJsonPayload: unknown = { + ok: true, + data: { + genuineDeals: [], + priceSpreadLeaders: [ + { + id: 'sp-1', + productSlug: 'iphone-16', + productName: 'iPhone 16\uD83D', + productHref: '/smartphones/iphone-16', + minPrice: 19_990_000, + maxPrice: 22_990_000, + spreadVnd: 3_000_000, + spreadPct: 15, + cheapestPlatform: null, + cheapestOfferUrl: 'https://shopee.vn/p-i.1.2', + priciestPlatform: undefined, + }, + ], + fakeSaleTraps: [ + { + id: 'tr-1', + verdict: 'fake_sale_trap', + verdictBadgeVi: 'Bẫy sale ảo', + productSlug: 'galaxy-s25', + productName: 'Galaxy S25', + categorySlug: 'smartphones', + productHref: '/smartphones/galaxy-s25', + platform: null, + merchantName: 'Trap Shop', + offerUrl: 'https://shopee.vn/t-i.3.4', + priceCurrent: 24_990_000, + priceOriginal: 35_990_000, + savingsVsMedianVnd: 0, + savingsVsMedianPct: 0, + fakeMarkupPct: 44, + explanationVi: 'Nâng khống', + }, + ], + }, + }; + let merchantsJsonPayload: unknown = { + ok: true, + data: [ + { + id: 'm-lone-\uD83D', + platform: null, + name: 'Official Store\uD83D', + slug: 'official-\uD83D-store', + is_official: true, + dosafe_risk_score: 5, + review_risk_score: 8, + risk_score: 8, + trust_score: 92, + product_count: 12, + }, + ], + }; + + const matchedLookupWithAstral: ExtensionLookupData = { + matchStatus: 'exact_listing', + platform: 'shopee', + canonicalUrl: null, + detectedStorageGb: 256, + isAccessoryOrPart: false, + accessoryReason: null, + variantCandidates: [], + masterProduct: { + id: 'mp-1812', + slug: '', + name: 'iPhone 16 Pro 256GB', + brand: 'Apple', + model: 'iPhone 16 Pro', + storage_gb: 256, + ram_gb: 8, + min_price: 25_990_000, + max_price: 28_990_000, + merchant_count: 4, + average_rating: 4.9, + user_rating_weighted: 4.9, + expert_rating: null, + authentic_review_count: 25, + total_reviews: 30, + }, + matchedOffer: null, + topCheaperOffers: [], + allOffers: [], + merchantProfile: null, + reviewSummary: null, + priceHistory: null, + categorySlug: 'smartphones', + compareUrl: '/compare?category=smartphones', + productDetailUrl: '/smartphones/iphone-16-pro', + versusUrl: '/compare?category=smartphones&ids=a,b', + leaderboardStandings: [ + { + criterion: 'combined', + rank: 1, + totalInCategory: 20, + score5: 4.8, + score10: 9.6, + categorySlug: 'smartphones', + }, + { + criterion: 'camera', + rank: 2, + totalInCategory: 20, + score5: 4.7, + score10: 9.4, + categorySlug: 'smartphones', + }, + ], + radarSignal: { + verdict: 'genuine_deal', + savingsPct: 8, + fakeMarkupPct: 0, + spreadPct: 12, + genuineDeal: { verdict: 'genuine_all_time_low' }, + }, + priceAssessment: { + verdict: 'genuine_deal', + badgeLabel: 'Giá tốt', + explanation: 'Thấp hơn trung vị', + observedPrice: 25_990_000, + observedOriginalPrice: 28_990_000, + claimedDiscountPct: 10, + vsMarketMedianPct: -8, + marketMinPrice: 25_990_000, + marketMedianPrice: 28_200_000, + marketMaxPrice: 28_990_000, + historicalMinPrice: 25_990_000, + historicalMaxPrice: 29_990_000, + cheaperOfferCount: 0, + maxSavingsAmount: 0, + }, + }; + + let sendMessageImpl = async ( + msg: Record + ): Promise => { + if (msg.type === 'RATE_SIDEPANEL_FETCH_FULL') { + return { + ok: true, + settings: { + apiBaseUrl: String( + storedLocalState.apiBaseUrl ?? DEFAULT_API_BASE_URL + ), + crowdContributeOptIn: + storedLocalState.crowdContributeOptIn === true && + storedLocalState.crowdConsentVersion === 2, + }, + signal: { + platform: 'shopee', + canonicalUrl: '', + externalItemId: '2', + externalShopId: '1', + // Raw lone surrogate + straddling emoji when canonicalUrl and master.slug are empty! + title: `${'q'.repeat(499)}\uD83D`, + selectedVariant: null, + price: 25_990_000, + originalPrice: null, + ratingStar: null, + ratingCount: null, + }, + data: matchedLookupWithAstral, + }; + } + if (msg.type === 'RATE_SIDEPANEL_QUICK_QUERY') { + return { + ok: true, + settings: { + apiBaseUrl: String( + storedLocalState.apiBaseUrl ?? DEFAULT_API_BASE_URL + ), + crowdContributeOptIn: false, + }, + signal: { + platform: 'shopee', + canonicalUrl: '', + externalItemId: null, + externalShopId: null, + title: `${'m'.repeat(499)}\uD83D`, + selectedVariant: null, + price: null, + originalPrice: null, + ratingStar: null, + ratingCount: null, + }, + data: null, + }; + } + if (msg.type === 'RATE_UPDATE_SETTINGS') { + if (typeof msg.apiBaseUrl === 'string') { + storedLocalState.apiBaseUrl = normalizeAllowedApiBaseUrl( + msg.apiBaseUrl + ); + } + if (typeof msg.crowdContributeOptIn === 'boolean') { + storedLocalState.crowdContributeOptIn = msg.crowdContributeOptIn; + storedLocalState.crowdConsentVersion = msg.crowdContributeOptIn + ? 2 + : null; + } + return { + ok: true, + settings: { + apiBaseUrl: String( + storedLocalState.apiBaseUrl ?? DEFAULT_API_BASE_URL + ), + crowdContributeOptIn: + storedLocalState.crowdContributeOptIn === true && + storedLocalState.crowdConsentVersion === 2, + }, + }; + } + return { ok: false }; + }; + + g.fetch = async (url: unknown) => { + const u = String(url); + if (u.includes('/api/v1/rate/deals')) { + fetchedRadarUrls.push(u); + return { + ok: true, + json: async () => dealsJsonPayload, + }; + } + if (u.includes('/api/v1/rate/merchants')) { + return { + ok: true, + json: async () => merchantsJsonPayload, + }; + } + return { ok: false }; + }; + g.chrome = { + runtime: { + sendMessage: (msg: Record) => { + sentMessages.push(msg); + return sendMessageImpl(msg); + }, + }, + storage: { + local: { get: () => localGetImpl() }, + onChanged: { + addListener: ( + fn: (c: Record, a: string) => void + ) => storageListeners.push(fn), + }, + }, + }; + g.document = { + documentElement: {}, + title: '', + querySelectorAll: () => [], + getElementById: (id: string) => { + switch (id) { + case 'panel-root': + return root; + case 'deal-radar-slot': + return radarSlot; + case 'merchant-trust-slot': + return trustSlot; + case 'chk-crowd-optin': + return chkOptIn; + case 'inp-api-base': + return inpApiBase; + case 'inp-quick-query': + return inpQuickQuery; + case 'form-quick-inspect': + return formQuickInspect; + default: + return null; + } + }, + addEventListener: (ev: string, cb: () => void) => { + if (ev === 'DOMContentLoaded') onDomReady = cb; + }, + }; + + await import( + new URL('./sidepanel/sidepanel.js?case=1812a', import.meta.url).href + ); + onDomReady!(); + await settle(); + + // (a) Product card with lone-surrogate title did NOT throw URIError into lookupErrorNetwork. + assert.equal( + root.innerHTML.includes(t('lookupErrorNetwork')), + false, + 'lone surrogate in signal.title must never trigger false lookupErrorNetwork' + ); + assert.ok( + root.innerHTML.includes( + `href="http://localhost:3000/inspect?q=${'q'.repeat(499)}"` + ), + 'inspectUrl strips lone surrogate cleanly' + ); + // Decorative emojis in product card, leaderboard sub-badge, radar pill, and action links have aria-hidden="true". + for (const expectedIcon of ['🏆', '⚔️', '🔬', '🔔', '🔥']) { + assert.ok( + root.innerHTML.includes( + `` + ), + `icon ${expectedIcon} must be wrapped with aria-hidden="true"` + ); + } + + // (b) Deal Radar with deals.length === 0 hides radarDealsTitle, guards null platforms, and marks emojis aria-hidden. + assert.equal( + radarSlot.innerHTML.includes(t('radarDealsTitle')), + false, + 'radarDealsTitle must be hidden when genuineDeals is empty' + ); + assert.ok(radarSlot.innerHTML.includes(t('radarSpreadTitle'))); + assert.ok(radarSlot.innerHTML.includes(t('radarTrapsTitle'))); + assert.ok( + radarSlot.innerHTML.includes('') + ); + assert.ok( + radarSlot.innerHTML.includes('') + ); + assert.equal( + radarSlot.innerHTML.indexOf('border-top:1px solid #2a2f3a;') > + radarSlot.innerHTML.indexOf(t('radarSpreadTitle')), + true, + 'first rendered radar section must not carry a leading separator border' + ); + assert.match( + radarSlot.innerHTML, + /
\s*
\s*
so .offer-item:last-child prevents double borders' + ); + assert.ok( + trustSlot.innerHTML.includes('') + ); + assert.ok( + trustSlot.innerHTML.includes( + 'href="http://localhost:3000/merchants/official--store"' + ) + ); + + // (c) Deal Radar & Merchant Trust Directory guard json.ok === false, and deals.length > 0 shows radarDealsTitle with aria-hidden 🔥. + dealsJsonPayload = { + ok: false, + data: { + genuineDeals: [ + { + id: 'd-ignored', + productName: 'Should Not Render', + }, + ], + }, + }; + merchantsJsonPayload = { + ok: false, + data: [{ id: 'm-ignored', name: 'Should Not Render' }], + }; + storageListeners.forEach((fn) => + fn({ lookup_tab_latest: { newValue: 2 } }, 'session') + ); + await settle(); + assert.equal( + radarSlot.innerHTML, + '', + 'Deal Radar must render nothing when json.ok === false' + ); + assert.equal( + trustSlot.innerHTML, + '', + 'Merchant directory must render nothing when json.ok === false' + ); + + dealsJsonPayload = { + ok: true, + data: { + genuineDeals: [ + { + id: 'd-1', + verdict: 'genuine_deal', + verdictBadgeVi: 'Giá tốt', + productSlug: 'ipad-air', + productName: 'iPad Air M2', + categorySlug: 'tablets', + productHref: '/tablets/ipad-air', + platform: null, + merchantName: 'Apple Store', + offerUrl: 'https://shopee.vn/ipad-i.1.2', + priceCurrent: 14_990_000, + priceOriginal: 16_990_000, + savingsVsMedianVnd: 1_500_000, + savingsVsMedianPct: 9, + fakeMarkupPct: 0, + explanationVi: 'Tốt', + }, + ], + priceSpreadLeaders: [], + fakeSaleTraps: [], + }, + }; + storageListeners.forEach((fn) => + fn({ lookup_tab_latest: { newValue: 3 } }, 'session') + ); + await settle(); + assert.ok(radarSlot.innerHTML.includes(t('radarDealsTitle'))); + assert.ok( + radarSlot.innerHTML.includes('') + ); + + // Quick query with lone-surrogate not-found signal renders notFoundTitle instead of lookupErrorNetwork. + inpQuickQuery.value = `${'m'.repeat(499)}🔥 tail`; + submitHandlers[0]!({ preventDefault() {} }); + await settle(); + assert.ok(root.innerHTML.includes(t('notFoundTitle'))); + assert.equal(root.innerHTML.includes(t('lookupErrorNetwork')), false); + + // (d) Consent gating on crowdConsentVersion === 2 in hydration and storage.onChanged. + // Stored local had { crowdContributeOptIn: true, crowdConsentVersion: 1 }, so chkOptIn MUST be false! + assert.equal( + chkOptIn.checked, + false, + 'v1 consent must leave chkOptIn unchecked' + ); + const emitLocalChange = (changes: Record) => + storageListeners.forEach((fn) => fn(changes, 'local')); + + // crowdContributeOptIn: true alone while storedConsentVersion === 1 stays false! + emitLocalChange({ crowdContributeOptIn: { newValue: true } }); + assert.equal( + chkOptIn.checked, + false, + 'crowdContributeOptIn without crowdConsentVersion === 2 must stay unchecked' + ); + + // Upgrading crowdConsentVersion to 2 while crowdContributeOptIn is true turns chkOptIn true! + emitLocalChange({ crowdConsentVersion: { newValue: 2 } }); + assert.equal(chkOptIn.checked, true); + + // Downgrading or clearing crowdConsentVersion revokes chkOptIn back to false! + emitLocalChange({ crowdConsentVersion: { newValue: 1 } }); + assert.equal(chkOptIn.checked, false); + emitLocalChange({ + crowdContributeOptIn: { newValue: true }, + crowdConsentVersion: { newValue: 2 }, + }); + assert.equal(chkOptIn.checked, true); + emitLocalChange({ crowdConsentVersion: { newValue: null } }); + assert.equal(chkOptIn.checked, false); + + // (e) chkOptIn change handler rollback on rejected sendMessage AND { ok: false }. + const baseImpl = sendMessageImpl; + // Turning ON fails with rejection -> rolls back to false. + sendMessageImpl = async (msg) => { + if (msg.type === 'RATE_UPDATE_SETTINGS') { + throw new Error('storage quota exceeded'); + } + return baseImpl(msg); + }; + chkOptIn.checked = true; + await chkHandlers[0]!(); + assert.equal( + chkOptIn.checked, + false, + 'chkOptIn must roll back to false when RATE_UPDATE_SETTINGS throws' + ); + + // Turning ON succeeds -> checked === true; then turning OFF fails with { ok: false } -> rolls back to true! + sendMessageImpl = baseImpl; + chkOptIn.checked = true; + await chkHandlers[0]!(); + assert.equal(chkOptIn.checked, true); + + sendMessageImpl = async (msg) => { + if (msg.type === 'RATE_UPDATE_SETTINGS') { + return { ok: false, error: 'write failed' }; + } + return baseImpl(msg); + }; + chkOptIn.checked = false; + await chkHandlers[0]!(); + assert.equal( + chkOptIn.checked, + true, + 'chkOptIn must roll back to true when disabling fails with ok: false' + ); + + // (f) inpApiBase rollback on failure & cleared inpApiBase reset to DEFAULT_API_BASE_URL. + assert.equal(inpApiBase.value, 'http://localhost:3000'); + // Changing to https://www.rate.vn fails -> rolls back to http://localhost:3000. + inpApiBase.value = 'https://www.rate.vn'; + await apiHandlers[0]!(); + assert.equal( + inpApiBase.value, + 'http://localhost:3000', + 'inpApiBase must roll back to previous confirmed URL when RATE_UPDATE_SETTINGS returns ok: false' + ); + + sendMessageImpl = async (msg) => { + if (msg.type === 'RATE_UPDATE_SETTINGS') { + throw new Error('worker unavailable'); + } + return baseImpl(msg); + }; + inpApiBase.value = ''; + await apiHandlers[0]!(); + assert.equal( + inpApiBase.value, + 'http://localhost:3000', + 'clearing inpApiBase when storage update throws must roll back to previous confirmed URL' + ); + + // Now restore working sendMessage and clear inpApiBase -> persists & restores DEFAULT_API_BASE_URL! + sendMessageImpl = baseImpl; + sentMessages.length = 0; + inpApiBase.value = ' '; + await apiHandlers[0]!(); + await settle(); + assert.equal( + inpApiBase.value, + DEFAULT_API_BASE_URL, + 'clearing inpApiBase must restore DEFAULT_API_BASE_URL' + ); + assert.equal(storedLocalState.apiBaseUrl, DEFAULT_API_BASE_URL); + assert.ok( + sentMessages.some( + (m) => + m.type === 'RATE_UPDATE_SETTINGS' && + m.apiBaseUrl === DEFAULT_API_BASE_URL + ), + 'clearing inpApiBase must persist DEFAULT_API_BASE_URL via RATE_UPDATE_SETTINGS' + ); + assert.ok( + sentMessages.some((m) => m.type === 'RATE_SIDEPANEL_FETCH_FULL'), + 'clearing inpApiBase must reload the side panel against DEFAULT_API_BASE_URL' + ); + + // (g) Pre-hydration partial storage.onChanged buffering in sidepanel.ts. + let resolveSlowHydration!: (v: Record) => void; + localGetImpl = () => + new Promise>((resolve) => { + resolveSlowHydration = resolve; + }); + await import( + new URL('./sidepanel/sidepanel.js?case=1812b', import.meta.url).href + ); + onDomReady!(); + const latestStorageListener = storageListeners.at(-1)!; + // Partial opt-in true before hydration finishes must NOT turn chkOptIn true until version === 2 is confirmed! + latestStorageListener( + { crowdContributeOptIn: { newValue: true } }, + 'local' + ); + assert.equal( + chkOptIn.checked, + false, + 'pre-hydration partial opt-in must stay unchecked until crowdConsentVersion === 2 is confirmed' + ); + resolveSlowHydration({ + crowdContributeOptIn: false, + crowdConsentVersion: 2, + apiBaseUrl: DEFAULT_API_BASE_URL, + }); + await settle(); + assert.equal( + chkOptIn.checked, + true, + 'buffered opt-in true + hydrated crowdConsentVersion === 2 must reconcile chkOptIn to true' + ); + + // (h) Whitespace/lone-surrogate-only canonicalUrl, master.slug, and m.slug fall back cleanly without trailing %20. + merchantsJsonPayload = { + ok: true, + data: [ + { + id: 'merchant-fallback-id', + platform: 'shopee', + name: 'Fallback Shop', + slug: ' \uD83D \uDC00 '.slice(0, 3), + is_official: false, + dosafe_risk_score: 10, + review_risk_score: 10, + risk_score: 10, + trust_score: 90, + product_count: 5, + }, + ], + }; + sendMessageImpl = async (msg) => { + if (msg.type === 'RATE_SIDEPANEL_FETCH_FULL') { + return { + ok: true, + settings: { + apiBaseUrl: DEFAULT_API_BASE_URL, + crowdContributeOptIn: false, + }, + signal: { + platform: 'shopee', + canonicalUrl: ' \uD83D ', + externalItemId: '2', + externalShopId: '1', + title: 'Phone 16 \uD83D', + selectedVariant: null, + price: 25_990_000, + originalPrice: null, + ratingStar: null, + ratingCount: null, + }, + data: { + ...matchedLookupWithAstral, + masterProduct: { + ...matchedLookupWithAstral.masterProduct!, + slug: ' \uDC00 ', + }, + }, + }; + } + return baseImpl(msg); + }; + latestStorageListener({ lookup_tab_latest: { newValue: 9 } }, 'session'); + await settle(); + assert.ok( + root.innerHTML.includes('href="https://rate.vn/inspect?q=Phone%2016"'), + 'whitespace/lone-surrogate canonicalUrl and master.slug must fall back to trimmed signal.title without trailing %20' + ); + assert.ok( + root.innerHTML.includes('href="https://rate.vn/watchlist"'), + 'lone-surrogate-only master.slug must fall back to /watchlist without ?add=%20' + ); + assert.ok( + trustSlot.innerHTML.includes( + 'href="https://rate.vn/merchants/merchant-fallback-id"' + ), + 'lone-surrogate-only m.slug must fall back to m.id' + ); + + // (i) Fail-closed v2 consent when local.get rejects or returns {}: unversioned res.settings.crowdContributeOptIn + // and partial storage.onChanged without crowdConsentVersion === 2 must NEVER check chkOptIn. + localGetImpl = async () => { + throw new Error('storage read error'); + }; + sendMessageImpl = async (msg) => { + if (msg.type === 'RATE_SIDEPANEL_FETCH_FULL') { + return { + ok: true, + settings: { + apiBaseUrl: DEFAULT_API_BASE_URL, + crowdContributeOptIn: true, + }, + signal: null, + data: null, + }; + } + return baseImpl(msg); + }; + await import( + new URL('./sidepanel/sidepanel.js?case=1812c', import.meta.url).href + ); + onDomReady!(); + await settle(); + const listener1812c = storageListeners.at(-1)!; + assert.equal( + chkOptIn.checked, + false, + 'rejected storage.local.get must fail closed even if res.settings claims crowdContributeOptIn: true' + ); + listener1812c({ crowdContributeOptIn: { newValue: true } }, 'local'); + assert.equal( + chkOptIn.checked, + false, + 'partial crowdContributeOptIn without crowdConsentVersion === 2 after rejected hydration must remain unchecked' + ); + + // (j) Overlapping inpApiBase and chkOptIn saves reconcile to latest persisted value without mid-flight flicker. + localGetImpl = async () => ({ + apiBaseUrl: DEFAULT_API_BASE_URL, + crowdContributeOptIn: false, + crowdConsentVersion: 0, + }); + sendMessageImpl = baseImpl; + storedLocalState.apiBaseUrl = DEFAULT_API_BASE_URL; + storedLocalState.crowdContributeOptIn = false; + storedLocalState.crowdConsentVersion = 0; + chkHandlers.length = 0; + apiHandlers.length = 0; + await import( + new URL('./sidepanel/sidepanel.js?case=1812d', import.meta.url).href + ); + onDomReady!(); + await settle(); + const listener1812d = storageListeners.at(-1)!; + + // Overlapping inpApiBase saves: Save #1 (http://localhost:3000) succeeds while Save #2 (https://www.rate.vn) fails. + let resolveApiSave1!: (v: unknown) => void; + let rejectApiSave2!: (e: Error) => void; + let apiCallIdx = 0; + sendMessageImpl = (msg) => { + if (msg.type === 'RATE_UPDATE_SETTINGS' && 'apiBaseUrl' in msg) { + apiCallIdx += 1; + if (apiCallIdx === 1) { + return new Promise((r) => { + resolveApiSave1 = r; + }); + } + return new Promise((_, rej) => { + rejectApiSave2 = rej; + }); + } + return baseImpl(msg); + }; + sentMessages.length = 0; + inpApiBase.value = 'http://localhost:3000'; + const pApi1 = apiHandlers[0]!(); + inpApiBase.value = 'https://www.rate.vn'; + const pApi2 = apiHandlers[0]!(); + storedLocalState.apiBaseUrl = 'http://localhost:3000'; + listener1812d( + { apiBaseUrl: { newValue: 'http://localhost:3000' } }, + 'local' + ); + assert.equal( + inpApiBase.value, + 'https://www.rate.vn', + 'in-flight second inpApiBase edit must not flicker when first save fires onChanged' + ); + resolveApiSave1({ + ok: true, + settings: { + apiBaseUrl: 'http://localhost:3000', + crowdContributeOptIn: false, + }, + }); + await pApi1; + assert.equal( + sentMessages.filter((m) => m.type === 'RATE_SIDEPANEL_FETCH_FULL') + .length, + 0, + 'superseded first inpApiBase save must not trigger premature reload while second save is in flight' + ); + rejectApiSave2(new Error('save 2 failed')); + await pApi2; + await settle(); + assert.equal( + inpApiBase.value, + 'http://localhost:3000', + 'failed second inpApiBase save must roll back to Save #1 persisted URL, not pre-Save-#1 URL' + ); + assert.equal( + sentMessages.filter((m) => m.type === 'RATE_SIDEPANEL_FETCH_FULL') + .length, + 1, + 'panel must reload once against Save #1 persisted URL after Save #2 settles' + ); + + // Overlapping chkOptIn saves: Toggle #1 (true) succeeds while Toggle #2 (false) fails -> reconciles to true without mid-flight flicker. + let resolveChk1!: (v: unknown) => void; + let resolveChk2!: (v: unknown) => void; + let chkCallIdx = 0; + sendMessageImpl = (msg) => { + if ( + msg.type === 'RATE_UPDATE_SETTINGS' && + 'crowdContributeOptIn' in msg + ) { + chkCallIdx += 1; + if (chkCallIdx === 1) { + return new Promise((r) => { + resolveChk1 = r; + }); + } + return new Promise((r) => { + resolveChk2 = r; + }); + } + return baseImpl(msg); + }; + chkOptIn.checked = true; + const pChk1 = chkHandlers[0]!(); + chkOptIn.checked = false; + const pChk2 = chkHandlers[0]!(); + listener1812d( + { + crowdContributeOptIn: { newValue: true }, + crowdConsentVersion: { newValue: 2 }, + }, + 'local' + ); + assert.equal( + chkOptIn.checked, + false, + 'chkOptIn must not flicker back to true while Toggle #2 (false) is still in flight' + ); + resolveChk1({ + ok: true, + settings: { + apiBaseUrl: 'http://localhost:3000', + crowdContributeOptIn: true, + }, + }); + await pChk1; + resolveChk2({ ok: false, error: 'toggle 2 failed' }); + await pChk2; + assert.equal( + chkOptIn.checked, + true, + 'failed Toggle #2 must reconcile chkOptIn to Toggle #1 persisted true state' + ); + + assert.equal( + unhandled1812.length, + 0, + 'side panel settings failures must never trigger unhandledRejection' + ); + + // 3. Service worker serialization of concurrent RATE_UPDATE_SETTINGS & astral 200/500 bounds. + { + let swMsgHandler: + | (( + msg: Record, + sender: Record, + sendResponse: (res: unknown) => void + ) => unknown) + | undefined; + const swLocal = new Map([ + ['apiBaseUrl', 'https://rate.vn'], + ['crowdContributeOptIn', false], + ]); + const swSession = new Map(); + let delayCooldownClearOnce = true; + let releaseCooldownClear!: () => void; + let onCooldownPaused: (() => void) | undefined; + + g.chrome = { + runtime: { + id: 'rate-ext-1812-sw', + getURL: (p: string) => `chrome-extension://rate-ext-1812-sw/${p}`, + getManifest: () => ({ version: '2.1.0' }), + onInstalled: { addListener() {} }, + onStartup: { addListener() {} }, + onMessage: { + addListener: (fn: typeof swMsgHandler) => { + swMsgHandler = fn; + }, + }, + }, + sidePanel: { + setPanelBehavior: async () => {}, + open: async () => {}, + }, + storage: { + local: { + get: async (keys?: string | string[]) => { + const list = Array.isArray(keys) ? keys : keys ? [keys] : []; + return Object.fromEntries( + list + .filter((k) => swLocal.has(k)) + .map((k) => [k, swLocal.get(k)]) + ); + }, + set: async (obj: Record) => { + for (const [k, v] of Object.entries(obj)) swLocal.set(k, v); + }, + remove: async (keys: string | string[]) => { + for (const k of Array.isArray(keys) ? keys : [keys]) { + swLocal.delete(k); + } + }, + }, + session: { + get: async (keys?: string | string[]) => { + const list = Array.isArray(keys) ? keys : keys ? [keys] : []; + return Object.fromEntries( + list + .filter((k) => swSession.has(k)) + .map((k) => [k, swSession.get(k)]) + ); + }, + set: async (obj: Record) => { + if ( + delayCooldownClearOnce && + obj.lookup_rate_limited_until === 0 + ) { + delayCooldownClearOnce = false; + const gate = new Promise((r) => { + releaseCooldownClear = r; + }); + onCooldownPaused?.(); + await gate; + } + for (const [k, v] of Object.entries(obj)) swSession.set(k, v); + }, + }, + }, + action: { + setBadgeText: async () => {}, + setBadgeBackgroundColor: async () => {}, + onClicked: { addListener() {} }, + }, + tabs: { + query: async () => [{ id: 1, active: true }], + create: async () => ({ id: 2 }), + }, + }; + g.fetch = async () => + new Response(JSON.stringify({ ok: true, data: null }), { + status: 200, + }); + + await import( + new URL('./background/service-worker.js?case=1812sw', import.meta.url) + .href + ); + const callSw1812 = (msg: Record) => + new Promise>((resolve) => { + swMsgHandler!( + msg, + { + id: 'rate-ext-1812-sw', + tab: undefined, + url: 'chrome-extension://rate-ext-1812-sw/sidepanel/sidepanel.html', + }, + (r) => resolve(r as Record) + ); + }); + + // Concurrent RATE_UPDATE_SETTINGS: Request 1 (localhost) pauses at lookupCooldown.clear() while Request 2 (reset to https://rate.vn) is queued behind it. + const pausedOnClear = new Promise((r) => { + onCooldownPaused = r; + }); + const req1 = callSw1812({ + type: 'RATE_UPDATE_SETTINGS', + apiBaseUrl: 'http://localhost:3000', + crowdContributeOptIn: true, + }); + const req2 = callSw1812({ + type: 'RATE_UPDATE_SETTINGS', + apiBaseUrl: DEFAULT_API_BASE_URL, + crowdContributeOptIn: false, + }); + await pausedOnClear; + releaseCooldownClear(); + const [res1, res2] = await Promise.all([req1, req2]); + assert.equal(res1.ok, true); + assert.equal(res2.ok, true); + assert.equal( + swLocal.get('apiBaseUrl'), + DEFAULT_API_BASE_URL, + 'serialized RATE_UPDATE_SETTINGS must preserve later reset to DEFAULT_API_BASE_URL' + ); + assert.equal( + swLocal.get('crowdContributeOptIn'), + false, + 'serialized RATE_UPDATE_SETTINGS must preserve later opt-out' + ); + assert.equal(swLocal.get('crowdConsentVersion'), null); + + // Service worker quick query (500 limit) & selectedVariant override (200 limit) preserve astral pairs cleanly. + const quickAstralRes = await callSw1812({ + type: 'RATE_SIDEPANEL_QUICK_QUERY', + query: `${'k'.repeat(499)}🔥 extra`, + }); + assert.equal(quickAstralRes.ok, true); + const quickSig = quickAstralRes.signal as { title: string }; + assert.equal(quickSig.title, `${'k'.repeat(499)}🔥`); + assert.equal(hasLoneSurrogate(quickSig.title), false); + + const fullAstralRes = await callSw1812({ + type: 'RATE_SIDEPANEL_FETCH_FULL', + selectedVariant: `${'s'.repeat(199)}🔥 extra`, + }); + assert.equal(fullAstralRes.ok, true); + const fullSig = fullAstralRes.signal as { selectedVariant: string }; + assert.equal(fullSig.selectedVariant, `${'s'.repeat(199)}🔥`); + assert.equal(hasLoneSurrogate(fullSig.selectedVariant), false); + } + + console.log( + ' ✅ [Surrogate-safe URL encoding, consent v2 sync, settings rollback & Deal Radar UI/a11y (#1812)] Verified' + ); + } finally { + process.off('unhandledRejection', onUnhandled1812); + for (const [key, desc] of savedGlobals) { + if (desc) Object.defineProperty(g, key, desc); + else delete g[key]; + } + } + } console.log('🎉 All Rate Browser Extension MV3 v2 tests passed!'); } From 624f1d5380bfb24360ac9c1fba1785dcfa10cf2d Mon Sep 17 00:00:00 2001 From: JOY <5027251+JOY@users.noreply.github.com> Date: Sun, 22 Nov 2026 08:08:16 +0700 Subject: [PATCH 09/24] fix(web): resolve item slugs in reviews api and harden route handlers (#1825) * fix(web): resolve item slugs in reviews api and harden route handlers Refs #1811 * fix(web): drop item-size static params and dedupe leaderboard items Refs #1811 --- .../api/v1/rate/__tests__/item-fields.test.ts | 201 +++++++++++ .../api/v1/rate/__tests__/write-paths.test.ts | 330 +++++++++++++++++- .../app/api/v1/rate/item-size/[slug]/route.ts | 15 +- .../src/app/api/v1/rate/leaderboard/route.ts | 23 +- .../api/v1/rate/reviews/my-values/route.ts | 96 ++--- apps/web/src/app/api/v1/rate/reviews/route.ts | 15 +- .../server/rate/__tests__/item-model.test.ts | 16 +- 7 files changed, 632 insertions(+), 64 deletions(-) diff --git a/apps/web/src/app/api/v1/rate/__tests__/item-fields.test.ts b/apps/web/src/app/api/v1/rate/__tests__/item-fields.test.ts index 083a61be..a22030ba 100644 --- a/apps/web/src/app/api/v1/rate/__tests__/item-fields.test.ts +++ b/apps/web/src/app/api/v1/rate/__tests__/item-fields.test.ts @@ -66,6 +66,9 @@ let schemaMissing = false; let offersSchemaMissing = false; // The Item's published variants: both sizes, one, or none. let variantSet: 'both' | 'one' | 'none' = 'both'; +let itemReviewsEnabled = false; +const LISTING_128 = '66666666-6666-4666-8666-666666666128'; +const LISTING_256 = '66666666-6666-4666-8666-666666666256'; const OTHER = { ...master('55555555-5555-4555-8555-555555555555', 256), item_id: null, @@ -132,8 +135,20 @@ globalThis.fetch = (async (input: RequestInfo | URL) => { : []; if (q.includes('slug=eq.apple-iphone-16-128gb')) return json([M128]); if (q.includes('slug=eq.apple-iphone-16-256gb')) return json([M256]); + if (itemReviewsEnabled && q.includes('slug=eq.apple-iphone-16-unknown')) + return json([ + { + ...M128, + slug: 'apple-iphone-16-unknown', + variant_key: 'unknown', + storage_gb: null, + status: 'archived', + }, + ]); if (q.includes(`slug=eq.${OTHER.slug}`)) return json([OTHER]); if (q.includes(`id=eq.${V128}`)) return json([M128]); + if (itemReviewsEnabled && q.includes(`id=eq.${V256}`)) + return json([M256]); if ( q.includes(`item_id=eq.${ITEM_ID}`) || q.includes(`item_id=in.(${ITEM_ID})`) @@ -142,6 +157,10 @@ globalThis.fetch = (async (input: RequestInfo | URL) => { return json([]); } case '/rest/v1/products': + if (itemReviewsEnabled && q.includes(`cluster_id=eq.${V128}`)) + return json([{ id: LISTING_128 }]); + if (itemReviewsEnabled && q.includes(`cluster_id=eq.${V256}`)) + return json([{ id: LISTING_256 }]); // A listing on the 128GB variant. if (q.includes('111222333') && q.includes('cluster_id=not.is.null')) return json([{ cluster_id: V128, platform_item_id: '111222333' }]); @@ -178,6 +197,44 @@ globalThis.fetch = (async (input: RequestInfo | URL) => { }, ]); return json([]); + case '/rest/v1/reviews': + if (itemReviewsEnabled && q.includes(LISTING_256)) { + return json([ + { + id: 'rev-item-256', + author_name: 'Buyer 256GB', + rating_star: 5, + comment: 'Spacious 256GB storage and crisp main camera.', + created_at: '2026-11-20T10:00:00Z', + reviewer_type: 'user', + helpfulness_weight: 1, + review_flags: { + verdict: 'authentic', + flag_category: 'authentic', + include_in_rating: true, + }, + }, + ]); + } + if (itemReviewsEnabled && q.includes(LISTING_128)) { + return json([ + { + id: 'rev-item-128', + author_name: 'Buyer 128GB', + rating_star: 4, + comment: 'Compact 128GB model with reliable all-day battery.', + created_at: '2026-11-20T09:00:00Z', + reviewer_type: 'user', + helpfulness_weight: 1, + review_flags: { + verdict: 'authentic', + flag_category: 'authentic', + include_in_rating: true, + }, + }, + ]); + } + return json([]); default: return json([]); } @@ -484,12 +541,156 @@ async function testExtensionLookup() { console.warn(' ✅ Extension lookup returns the Item and all its sizes'); } +async function testReviewsAndItemSizeRoutes() { + const { NextRequest } = await import('next/server'); + const reviewsRoute = await import('../reviews/route'); + const itemSizeRoute = await import('../item-size/[slug]/route'); + + itemReviewsEnabled = true; + try { + // 1. Flag off: canonical Item slug returns 404 without querying items; variant slug returns 200. + setFlag(undefined); + calls = []; + let res = await reviewsRoute.GET( + new NextRequest( + 'http://localhost/api/v1/rate/reviews?slug=apple-iphone-16' + ) + ); + assert.equal(res.status, 404); + assert.equal((await res.json()).code, 'NOT_FOUND'); + assert.ok(!calls.some((c) => c.startsWith('/rest/v1/items'))); + + res = await reviewsRoute.GET( + new NextRequest( + 'http://localhost/api/v1/rate/reviews?slug=apple-iphone-16-128gb' + ) + ); + assert.equal(res.status, 200); + let body = await res.json(); + assert.equal(body.masterProductId, V128); + assert.equal(body.data[0]?.id, 'rev-item-128'); + + // 2. Flag on, Item tables missing: falls back to master product lookup. + setFlag('item'); + schemaMissing = true; + res = await reviewsRoute.GET( + new NextRequest( + 'http://localhost/api/v1/rate/reviews?slug=apple-iphone-16-128gb' + ) + ); + assert.equal(res.status, 200); + assert.equal((await res.json()).masterProductId, V128); + schemaMissing = false; + + // 3. Flag on: canonical Item slug resolves to default variant (256GB), ?v= selects variant, case-insensitive. + res = await reviewsRoute.GET( + new NextRequest( + 'http://localhost/api/v1/rate/reviews?slug=apple-iphone-16' + ) + ); + assert.equal(res.status, 200); + body = await res.json(); + assert.equal(body.ok, true); + assert.equal(body.masterProductId, V256); + assert.equal(body.count, 1); + assert.equal(body.data[0]?.id, 'rev-item-256'); + + res = await reviewsRoute.GET( + new NextRequest( + 'http://localhost/api/v1/rate/reviews?slug=APPLE-IPHONE-16' + ) + ); + assert.equal(res.status, 200); + assert.equal((await res.json()).masterProductId, V256); + + res = await reviewsRoute.GET( + new NextRequest( + 'http://localhost/api/v1/rate/reviews?slug=apple-iphone-16&v=128gb' + ) + ); + assert.equal(res.status, 200); + body = await res.json(); + assert.equal(body.masterProductId, V128); + assert.equal(body.data[0]?.id, 'rev-item-128'); + + // Unknown ?v= on an Item slug and retired/archived variant slugs fall back to the default variant. + res = await reviewsRoute.GET( + new NextRequest( + 'http://localhost/api/v1/rate/reviews?slug=apple-iphone-16&v=999gb' + ) + ); + assert.equal(res.status, 200); + assert.equal((await res.json()).masterProductId, V256); + + res = await reviewsRoute.GET( + new NextRequest( + 'http://localhost/api/v1/rate/reviews?slug=apple-iphone-16-unknown' + ) + ); + assert.equal(res.status, 200); + assert.equal((await res.json()).masterProductId, V256); + + res = await reviewsRoute.GET( + new NextRequest( + 'http://localhost/api/v1/rate/reviews?slug=apple-iphone-17' + ) + ); + assert.equal(res.status, 404); + assert.equal((await res.json()).code, 'NOT_FOUND'); + + // 4. item-size/[slug] OPTIONS preflight and GET CORS headers (#1811). + const preflight = await itemSizeRoute.OPTIONS(); + assert.equal(preflight.status, 204); + assert.equal(await preflight.text(), ''); + assert.equal(preflight.headers.get('access-control-allow-origin'), '*'); + assert.equal( + preflight.headers.get('access-control-allow-methods'), + 'GET, OPTIONS' + ); + assert.equal( + preflight.headers.get('access-control-allow-headers'), + 'Content-Type, Authorization, X-Rate-Extension-Version' + ); + + const sizeRes = await itemSizeRoute.GET( + new Request( + 'http://localhost/api/v1/rate/item-size/apple-iphone-16-128gb' + ), + { params: Promise.resolve({ slug: 'apple-iphone-16-128gb' }) } + ); + assert.equal(sizeRes.status, 200); + assert.equal(sizeRes.headers.get('access-control-allow-origin'), '*'); + assert.equal( + (await sizeRes.json()).data.product.slug, + 'apple-iphone-16-128gb' + ); + + setFlag(undefined); + const sizeOffRes = await itemSizeRoute.GET( + new Request( + 'http://localhost/api/v1/rate/item-size/apple-iphone-16-128gb' + ), + { params: Promise.resolve({ slug: 'apple-iphone-16-128gb' }) } + ); + assert.equal(sizeOffRes.status, 404); + assert.equal(sizeOffRes.headers.get('access-control-allow-origin'), '*'); + } finally { + itemReviewsEnabled = false; + schemaMissing = false; + setFlag(undefined); + } + console.warn( + ' ✅ /reviews resolves canonical Item slugs and /item-size/[slug] answers OPTIONS (#1811)' + ); +} + async function main() { console.warn('🧪 API v1 Item fields suite (DOS/Rate#1158)'); await testFlagOffUnchanged(); await testDetailItemFields(); await testGroupItem(); await testExtensionLookup(); + await testReviewsAndItemSizeRoutes(); } main().catch((err) => { diff --git a/apps/web/src/app/api/v1/rate/__tests__/write-paths.test.ts b/apps/web/src/app/api/v1/rate/__tests__/write-paths.test.ts index ff9fa9d9..24609c71 100644 --- a/apps/web/src/app/api/v1/rate/__tests__/write-paths.test.ts +++ b/apps/web/src/app/api/v1/rate/__tests__/write-paths.test.ts @@ -1159,7 +1159,7 @@ async function testReviewsHideFakeAnd413Cors() { } async function testMyReviewValues() { - const { POST } = await import('../reviews/my-values/route'); + const { POST, OPTIONS } = await import('../reviews/my-values/route'); const MINE = 'aaaaaaaa-1111-4222-8333-444444444444'; const OTHER = 'aaaaaaaa-1111-4222-8333-555555555555'; const breakdown = { @@ -1169,14 +1169,85 @@ async function testMyReviewValues() { tier: 'complete', gaps: ['photo', 'not_a_gap', 'verified', 'photo'], }; + const assertMyValuesHeaders = (response: Response) => { + assert.equal(response.headers.get('access-control-allow-origin'), '*'); + assert.equal( + response.headers.get('access-control-allow-methods'), + 'POST, OPTIONS' + ); + assert.equal(response.headers.get('cache-control'), 'private, no-store'); + }; + + const preflight = await OPTIONS(); + assert.equal(preflight.status, 204); + assertMyValuesHeaders(preflight); // Nobody signed in: no call to the database. calls = []; let res = await POST(post('/api/v1/rate/reviews/my-values', { ids: [MINE] })); assert.equal(res.status, 401); + assertMyValuesHeaders(res); + assert.equal(rpcCalls('my_review_values').length, 0); + + // Empty ids still require auth, and skip the RPC when authenticated. + calls = []; + res = await POST(post('/api/v1/rate/reviews/my-values', { ids: [] })); + assert.equal(res.status, 401); + assertMyValuesHeaders(res); + assert.equal(calls.length, 0); + + calls = []; + res = await POST( + post('/api/v1/rate/reviews/my-values', { ids: [] }, GOOD_TOKEN) + ); + assert.equal(res.status, 200); + assertMyValuesHeaders(res); + assert.deepEqual(await res.json(), { ok: true, values: [] }); assert.equal(rpcCalls('my_review_values').length, 0); - // Bad ids are refused before the database. + // Oversized and malformed payloads are rejected before authenticateRateRequest (#1811). + calls = []; + const oversizeRes = await POST( + new Request('http://localhost/api/v1/rate/reviews/my-values', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Content-Length': String(8 * 1024 + 1), + Authorization: `Bearer ${GOOD_TOKEN}`, + }, + body: '{}', + }) + ); + assert.equal(oversizeRes.status, 413); + assert.equal((await oversizeRes.json()).code, 'PAYLOAD_TOO_LARGE'); + assertMyValuesHeaders(oversizeRes); + assert.equal( + calls.length, + 0, + 'oversized payload must not call Supabase auth or RPC' + ); + + calls = []; + const malformedRes = await POST( + new Request('http://localhost/api/v1/rate/reviews/my-values', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${GOOD_TOKEN}`, + }, + body: '{not-json', + }) + ); + assert.equal(malformedRes.status, 400); + assert.equal((await malformedRes.json()).code, 'INVALID_IDS'); + assertMyValuesHeaders(malformedRes); + assert.equal( + calls.length, + 0, + 'malformed JSON must not call Supabase auth or RPC' + ); + + // Bad ids are refused before auth and before the database. for (const ids of [ 'x', [MINE, 'not-a-uuid'], @@ -1190,7 +1261,12 @@ async function testMyReviewValues() { post('/api/v1/rate/reviews/my-values', { ids }, GOOD_TOKEN) ); assert.equal(res.status, 400); - assert.equal(rpcCalls('my_review_values').length, 0); + assertMyValuesHeaders(res); + assert.equal( + calls.length, + 0, + 'invalid ids must be rejected before auth and RPC' + ); } // The writer's call goes out with their own token; the answer keeps only @@ -1219,7 +1295,7 @@ async function testMyReviewValues() { ) ); assert.equal(res.status, 200); - assert.match(res.headers.get('cache-control') ?? '', /no-store/); + assertMyValuesHeaders(res); assert.deepEqual(await res.json(), { ok: true, values: [{ id: MINE, score: 62, gaps: ['photo', 'verified'] }], @@ -1242,11 +1318,38 @@ async function testMyReviewValues() { post('/api/v1/rate/reviews/my-values', { ids: [MINE] }, GOOD_TOKEN) ); assert.equal(res.status, 503); + assertMyValuesHeaders(res); rpcResponder = () => ({ status: 500, json: { code: 'XX000' } }); res = await POST( post('/api/v1/rate/reviews/my-values', { ids: [MINE] }, GOOD_TOKEN) ); assert.equal(res.status, 502); + assertMyValuesHeaders(res); + + // Unexpected runtime failure (e.g. an aborted/errored body stream) inside POST + // preserves MY_VALUES_HEADERS and hides internal details (#1811). + const brokenBody = new ReadableStream({ + pull() { + throw new Error('internal socket ECONNRESET'); + }, + }); + res = await POST( + new Request('http://localhost/api/v1/rate/reviews/my-values', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${GOOD_TOKEN}`, + }, + body: brokenBody, + duplex: 'half', + } as RequestInit) + ); + assert.equal(res.status, 500); + assertMyValuesHeaders(res); + const body500 = await res.json(); + assert.equal(body500.ok, false); + assert.equal(body500.code, 'INTERNAL_ERROR'); + assert.ok(!JSON.stringify(body500).includes('ECONNRESET')); } async function testExtensionObservations() { @@ -2856,6 +2959,224 @@ async function testMerchantCacheRevalidateAndDomainBounds() { ); } +async function testLeaderboardAllCategoryCompareUrl() { + const { NextRequest } = await import('next/server'); + const { GET, OPTIONS } = await import('../leaderboard/route'); + + const optRes = await OPTIONS(); + assert.equal(optRes.status, 204); + assert.equal(optRes.headers.get('access-control-allow-origin'), '*'); + + const CAT_PHONES = '77777777-8888-4777-8666-000000000001'; + const CAT_LAPTOPS = '77777777-8888-4777-8666-000000000002'; + const CAT_TABLETS = '77777777-8888-4777-8666-000000000003'; + const mkMaster = ( + idSuffix: string, + slug: string, + brand: string, + categorySlug: 'smartphones' | 'laptops' | 'tablets', + categoryId: string, + rating: number + ) => ({ + id: `99999999-8888-4777-8666-${idSuffix}`, + category_id: categoryId, + slug, + name: slug, + brand, + model: slug, + status: 'published', + rating_score: rating, + user_rating_weighted: rating, + expert_rating: rating * 2, + authentic_review_count: 10, + total_reviews: 10, + min_price: 20_000_000, + max_price: 22_000_000, + merchant_count: 2, + metadata: { category_slug: categorySlug }, + specifications: { category_slug: categorySlug }, + key_specs: {}, + }); + + const rows = [ + mkMaster( + '000000000101', + 'alpha-phone-1', + 'AlphaBrand1811', + 'smartphones', + CAT_PHONES, + 4.9 + ), + mkMaster( + '000000000102', + 'alpha-laptop-1', + 'AlphaBrand1811', + 'laptops', + CAT_LAPTOPS, + 4.8 + ), + mkMaster( + '000000000103', + 'alpha-phone-2', + 'AlphaBrand1811', + 'smartphones', + CAT_PHONES, + 4.7 + ), + mkMaster( + '000000000104', + 'alpha-phone-3', + 'AlphaBrand1811', + 'smartphones', + CAT_PHONES, + 4.6 + ), + mkMaster( + '000000000201', + 'beta-laptop-1', + 'BetaBrand1811', + 'laptops', + CAT_LAPTOPS, + 4.9 + ), + mkMaster( + '000000000202', + 'beta-phone-1', + 'BetaBrand1811', + 'smartphones', + CAT_PHONES, + 4.8 + ), + mkMaster( + '000000000203', + 'beta-tablet-1', + 'BetaBrand1811', + 'tablets', + CAT_TABLETS, + 4.7 + ), + mkMaster( + '000000000204', + 'beta-laptop-2', + 'BetaBrand1811', + 'laptops', + CAT_LAPTOPS, + 4.6 + ), + { + ...mkMaster( + '000000000301', + 'gamma-phone-256gb', + 'GammaBrand1811', + 'smartphones', + CAT_PHONES, + 4.9 + ), + item_id: '11111111-1111-4111-8111-000000001811', + }, + mkMaster( + '000000000302', + 'gamma-laptop-1', + 'GammaBrand1811', + 'laptops', + CAT_LAPTOPS, + 4.8 + ), + { + ...mkMaster( + '000000000303', + 'gamma-phone-128gb', + 'GammaBrand1811', + 'smartphones', + CAT_PHONES, + 4.7 + ), + item_id: '11111111-1111-4111-8111-000000001811', + }, + ]; + + const savedRpcResponder = rpcResponder; + const savedItemFlag = process.env.RATE_ITEM_MODEL; + try { + rpcResponder = (name, rpcBody) => { + if (name === 'search_master_products') { + const args = (rpcBody ?? {}) as { p_offset?: number }; + return Number(args.p_offset ?? 0) > 0 + ? { status: 200, json: [] } + : { status: 200, json: rows }; + } + return { status: 200, json: [] }; + }; + + // 1. category=all where #1 and #3 are smartphones and #2 is laptops: + // compare_top_3_url filters out cross-category #2 instead of sending a laptop slug to /compare?cat=smartphones. + let res = await GET( + new NextRequest( + 'http://localhost/api/v1/rate/leaderboard?category=all&brand=AlphaBrand1811' + ) + ); + assert.equal(res.status, 200); + let body = await res.json(); + assert.deepEqual( + body.podium_top_3.map((x: { slug: string }) => x.slug), + ['alpha-phone-1', 'alpha-laptop-1', 'alpha-phone-2'] + ); + assert.equal( + body.compare_top_3_url, + '/compare?cat=smartphones&slugs=alpha-phone-1,alpha-phone-2' + ); + assert.equal(body.data.compare_top_3_url, body.compare_top_3_url); + + // 2. category=all where #1, #2, #3 are all distinct categories: + // fewer than 2 podium items match compareCategory, so compare_top_3_url is null. + res = await GET( + new NextRequest( + 'http://localhost/api/v1/rate/leaderboard?category=all&brand=BetaBrand1811' + ) + ); + assert.equal(res.status, 200); + body = await res.json(); + assert.deepEqual( + body.podium_top_3.map((x: { slug: string }) => x.slug), + ['beta-laptop-1', 'beta-phone-1', 'beta-tablet-1'] + ); + assert.equal(body.compare_top_3_url, null); + assert.equal(body.data.compare_top_3_url, null); + + // 3. Single-category leaderboard keeps all 3 same-category podium slugs. + res = await GET( + new NextRequest( + 'http://localhost/api/v1/rate/leaderboard?category=smartphones&brand=AlphaBrand1811' + ) + ); + assert.equal(res.status, 200); + body = await res.json(); + assert.equal( + body.compare_top_3_url, + '/compare?cat=smartphones&slugs=alpha-phone-1,alpha-phone-2,alpha-phone-3' + ); + + // 4. Under RATE_ITEM_MODEL, same-Item podium variants are deduplicated so /compare does not collapse to 1 Item. + process.env.RATE_ITEM_MODEL = 'item'; + res = await GET( + new NextRequest( + 'http://localhost/api/v1/rate/leaderboard?category=all&brand=GammaBrand1811' + ) + ); + assert.equal(res.status, 200); + body = await res.json(); + assert.equal(body.compare_top_3_url, null); + } finally { + rpcResponder = savedRpcResponder; + if (savedItemFlag === undefined) delete process.env.RATE_ITEM_MODEL; + else process.env.RATE_ITEM_MODEL = savedItemFlag; + } + + console.log( + ' ✅ Leaderboard category=all filters compare_top_3_url to compareCategory (#1811)' + ); +} + async function main() { console.log('🧪 Public write-path security suite (#155)'); await testReviews(); @@ -2873,6 +3194,7 @@ async function main() { await testRequestAuthAndRouteBounds(); await testExtensionAndInspectBounds(); await testMerchantCacheRevalidateAndDomainBounds(); + await testLeaderboardAllCategoryCompareUrl(); testSharedDataClients(); console.log('✅ Public write-path security suite passed'); } diff --git a/apps/web/src/app/api/v1/rate/item-size/[slug]/route.ts b/apps/web/src/app/api/v1/rate/item-size/[slug]/route.ts index cdfe07df..9b647d9d 100644 --- a/apps/web/src/app/api/v1/rate/item-size/[slug]/route.ts +++ b/apps/web/src/app/api/v1/rate/item-size/[slug]/route.ts @@ -6,12 +6,15 @@ import { withPublicOfferReviewRisk, } from '@/server/rate/api-handler'; -// Cached like the product pages: the route reads nothing from the request but -// its path, so each size's answer is cached and refreshed every 5 minutes. -export const revalidate = 300; - -export function generateStaticParams(): { slug: string }[] { - return []; +// Cached via `withRateDataCache` inside `loadItemSizePanel` and edge +// `Cache-Control` (5 min fresh, 10 min stale-while-revalidate). Dynamic route +// execution allows `OPTIONS()` preflight without Next.js ISR prerender +// rejecting non-GET handlers (#1285, #1811). +export async function OPTIONS() { + return new NextResponse(null, { + status: 204, + headers: PUBLIC_API_CORS_HEADERS, + }); } type RouteContext = { params: Promise<{ slug: string }> }; diff --git a/apps/web/src/app/api/v1/rate/leaderboard/route.ts b/apps/web/src/app/api/v1/rate/leaderboard/route.ts index 78989b74..d55fb658 100644 --- a/apps/web/src/app/api/v1/rate/leaderboard/route.ts +++ b/apps/web/src/app/api/v1/rate/leaderboard/route.ts @@ -21,6 +21,7 @@ import { parseBoundedIntParam, parseBoundedStringParam, } from '@/server/rate/api-handler'; +import { isItemModelOn } from '@/server/rate/item-model'; export const revalidate = 60; @@ -120,12 +121,30 @@ export async function GET(req: NextRequest) { }; }); - const top3Slugs = items.slice(0, 3).map((x) => x.slug); const compareCategory = category === 'all' ? items[0]?.category || 'smartphones' : category; + const dedupeByItem = isItemModelOn(); + const seenCompareItemIds = new Set(); + const top3Slugs = pageRanked + .slice(0, 3) + .filter((r) => { + if (category === 'all' && r.categorySlug !== compareCategory) { + return false; + } + if (dedupeByItem) { + const rawItemId = (r.product as { item_id?: unknown }).item_id; + const itemId = typeof rawItemId === 'string' ? rawItemId.trim() : ''; + if (itemId) { + if (seenCompareItemIds.has(itemId)) return false; + seenCompareItemIds.add(itemId); + } + } + return Boolean(r.slug); + }) + .map((r) => r.slug); const compareTop3Url = top3Slugs.length >= 2 - ? `/compare?cat=${compareCategory}&slugs=${top3Slugs.join(',')}` + ? `/compare?cat=${compareCategory}&slugs=${top3Slugs.map(encodeURIComponent).join(',')}` : null; const payload = { diff --git a/apps/web/src/app/api/v1/rate/reviews/my-values/route.ts b/apps/web/src/app/api/v1/rate/reviews/my-values/route.ts index e426fc46..678111cf 100644 --- a/apps/web/src/app/api/v1/rate/reviews/my-values/route.ts +++ b/apps/web/src/app/api/v1/rate/reviews/my-values/route.ts @@ -38,56 +38,62 @@ function fail(status: number, code: string, error: string) { * ids are left out of the answer. */ export async function POST(request: Request) { - const auth = await authenticateRateRequest( - request, - 'read your review values', - MY_VALUES_HEADERS - ); - if (!auth.ok) return auth.response; + try { + const body = await readBoundedJsonBody(request, 8 * 1024); + if (body.tooLarge) return payloadTooLargeResponse(MY_VALUES_HEADERS); + const ids = parseReviewIds( + body.value && typeof body.value === 'object' && !Array.isArray(body.value) + ? (body.value as { ids?: unknown }).ids + : undefined + ); + if (!ids) { + return fail(400, 'INVALID_IDS', 'Provide ids: up to 100 review uuids'); + } - const body = await readBoundedJsonBody(request, 8 * 1024); - if (body.tooLarge) return payloadTooLargeResponse(MY_VALUES_HEADERS); - const ids = parseReviewIds( - body.value && typeof body.value === 'object' && !Array.isArray(body.value) - ? (body.value as { ids?: unknown }).ids - : undefined - ); - if (!ids) { - return fail(400, 'INVALID_IDS', 'Provide ids: up to 100 review uuids'); - } - if (ids.length === 0) { - return NextResponse.json( - { ok: true, values: [] }, - { headers: MY_VALUES_HEADERS } + const auth = await authenticateRateRequest( + request, + 'read your review values', + MY_VALUES_HEADERS ); - } + if (!auth.ok) return auth.response; - const { data, error } = await auth.rateClient.rpc('my_review_values', { - p_review_ids: ids, - }); - if (error) { - if (isMissingRpcError(error)) { - return fail( - 503, - 'REVIEW_VALUES_UNAVAILABLE', - 'Review values unavailable' + if (ids.length === 0) { + return NextResponse.json( + { ok: true, values: [] }, + { headers: MY_VALUES_HEADERS } ); } - log.error('my_review_values failed', { error: serializeError(error) }); - return fail(502, 'READ_FAILED', 'Could not read review values'); - } - const asked = new Set(ids); - const values = []; - for (const row of Array.isArray(data) ? data : []) { - const id = - typeof row?.review_id === 'string' ? row.review_id.toLowerCase() : ''; - if (!asked.has(id)) continue; - const value = parseMyReviewValue(row.value_score, row.value_breakdown); - if (value) values.push({ id, ...value }); + const { data, error } = await auth.rateClient.rpc('my_review_values', { + p_review_ids: ids, + }); + if (error) { + if (isMissingRpcError(error)) { + return fail( + 503, + 'REVIEW_VALUES_UNAVAILABLE', + 'Review values unavailable' + ); + } + log.error('my_review_values failed', { error: serializeError(error) }); + return fail(502, 'READ_FAILED', 'Could not read review values'); + } + + const asked = new Set(ids); + const values = []; + for (const row of Array.isArray(data) ? data : []) { + const id = + typeof row?.review_id === 'string' ? row.review_id.toLowerCase() : ''; + if (!asked.has(id)) continue; + const value = parseMyReviewValue(row.value_score, row.value_breakdown); + if (value) values.push({ id, ...value }); + } + return NextResponse.json( + { ok: true, values }, + { headers: MY_VALUES_HEADERS } + ); + } catch (error) { + log.error('POST failed', { error: serializeError(error) }); + return fail(500, 'INTERNAL_ERROR', 'Could not read review values'); } - return NextResponse.json( - { ok: true, values }, - { headers: MY_VALUES_HEADERS } - ); } diff --git a/apps/web/src/app/api/v1/rate/reviews/route.ts b/apps/web/src/app/api/v1/rate/reviews/route.ts index f0b4d284..4a7a6108 100644 --- a/apps/web/src/app/api/v1/rate/reviews/route.ts +++ b/apps/web/src/app/api/v1/rate/reviews/route.ts @@ -37,6 +37,7 @@ import { import { hasRatingCriteria, storedPoint } from '@repo/shared-data/rules'; import { evaluateReviewTrustAssessment } from '@/lib/rate-domain'; import { isFilteredOutReview } from '@/lib/review-display'; +import { resolveApiItemContext } from '@/server/rate/item-api'; import type { AuthenticatedRateRequest } from '@/server/rate/request-auth'; const log = createLogger('api.reviews'); @@ -84,7 +85,19 @@ export async function GET(request: NextRequest) { } resolvedMasterId = parsedQuery.masterProductId; } else { - const product = await getCachedMasterProductBySlug(parsedQuery.slug!); + const requestedSlug = parsedQuery.slug!; + const variantParam = request.nextUrl?.searchParams?.get('v'); + const itemContext = + (await resolveApiItemContext(requestedSlug, variantParam)) ?? + (requestedSlug.toLowerCase() !== requestedSlug + ? await resolveApiItemContext( + requestedSlug.toLowerCase(), + variantParam + ) + : null); + const product = await getCachedMasterProductBySlug( + itemContext ? itemContext.selected.slug : requestedSlug + ); if (!product) { return NextResponse.json( { ok: false, error: 'Product not found', code: 'NOT_FOUND' }, diff --git a/apps/web/src/server/rate/__tests__/item-model.test.ts b/apps/web/src/server/rate/__tests__/item-model.test.ts index e96eb914..693e0a25 100644 --- a/apps/web/src/server/rate/__tests__/item-model.test.ts +++ b/apps/web/src/server/rate/__tests__/item-model.test.ts @@ -651,18 +651,22 @@ async function main() { assert.doesNotMatch(src, /searchParams|\bheaders\(|\bcookies\(/, file); assert.doesNotMatch(src, /export const dynamic\b|force-dynamic/, file); } - // The size route is cached too: it reads only its path (DOS/Rate#1285). - const sizeRoute = path.resolve( - __dirname, - '../../../app/api/v1/rate/item-size/[slug]/route.ts' - ); - for (const file of [...productRouteFiles, sizeRoute]) { + for (const file of productRouteFiles) { const src = fs.readFileSync(file, 'utf8'); assert.match(src, /export const revalidate = 300;/, file); assert.match(src, /export function generateStaticParams\(/, file); } + // The size route reads only its path and caches via Data Cache + Cache-Control, + // without generateStaticParams (which breaks OPTIONS preflight in Next 16 ISR; #1285, #1811). + const sizeRoute = path.resolve( + __dirname, + '../../../app/api/v1/rate/item-size/[slug]/route.ts' + ); { const src = fs.readFileSync(sizeRoute, 'utf8'); + assert.match(src, /export async function OPTIONS\(/); + assert.match(src, /public, s-maxage=300, stale-while-revalidate=600/); + assert.doesNotMatch(src, /export function generateStaticParams\(/); assert.doesNotMatch(src, /searchParams|\bheaders\(|\bcookies\(/); assert.doesNotMatch(src, /export const dynamic\b|force-dynamic/); assert.doesNotMatch(src, /_request\.(url|nextUrl|headers)/); From 36e3099a18e71a34aef8a06e153db589fdee620f Mon Sep 17 00:00:00 2001 From: JOY <5027251+JOY@users.noreply.github.com> Date: Sun, 22 Nov 2026 08:23:26 +0700 Subject: [PATCH 10/24] fix(web): group Item peers on product detail and preserve ImageObject JSON-LD (#1827) * fix(web): group Item peers on product detail and preserve ImageObject JSON-LD - Group category peers by Item via groupCatalogRowsByItem and exclude the current Item before standing and relatedCompetitors rankings. - Emit canonical Item detail hrefs (catalogRowSlug) and guarded 1v1 versus links (versusHref + isParseableVersusPair). - Preserve CC BY-SA ImageObject JSON-LD attribution on ProductGroup and hasVariant in buildItemProductGroupNode. - Add unit tests in product-competitors.test.ts and device-images.test.ts. Closes #1819 * fix(web): address round-1 review findings on compare pins and JSON-LD fallback - Precompute fallbackGroupImages in buildItemProductGroupNode so all priced hasVariant Product nodes inherit ImageObject metadata when default images are empty, and strip null contentUrl/url properties. - Pin shared-slug multi-size Items and partial-load routes, and build /compare links with cat= and variant. fallback hints. - Strip control chars and validate fallback catalogue slugs in resolveCanonicalPeerSlug and withoutCurrentItemPeers. - Emit the trimmed canonical string returned by resolvePeerVersusHref. - Expand unit tests in product-competitors.test.ts and device-images.test.ts. --- .../app/[locale]/smartphones/[slug]/page.tsx | 151 +++- .../src/lib/__tests__/device-images.test.ts | 199 +++++ .../lib/__tests__/product-competitors.test.ts | 748 +++++++++++++++++- apps/web/src/lib/item-page.ts | 538 ++++++++++++- 4 files changed, 1572 insertions(+), 64 deletions(-) diff --git a/apps/web/src/app/[locale]/smartphones/[slug]/page.tsx b/apps/web/src/app/[locale]/smartphones/[slug]/page.tsx index cde8db86..29efe154 100644 --- a/apps/web/src/app/[locale]/smartphones/[slug]/page.tsx +++ b/apps/web/src/app/[locale]/smartphones/[slug]/page.tsx @@ -17,9 +17,13 @@ import { } from '@/server/rate/item-page'; import { buildItemProductGroupNode, + buildPeerCompareHref, + buildPeerItemSlugMap, itemVariantLabel, itemFromStats, - withoutItemSiblings, + resolveCanonicalPeerSlug, + resolvePeerVersusHref, + withoutCurrentItemPeers, } from '@/lib/item-page'; import ItemVariantPicker from '@/components/product/item/ItemVariantPicker'; import ItemVariantPanels from '@/components/product/item/ItemVariantPanels'; @@ -37,6 +41,7 @@ import { getCachedMasterProductReviewSummary, getCachedMasterProductSiblings, getCachedMasterProducts, + groupCatalogRowsByItem, computeProductRadarSignal, } from '@/server/rate'; import { computeBayesianRatingSummary } from '@/lib/rate-api'; @@ -138,7 +143,8 @@ import { formatPriceVnd, } from '@/lib/locale-format'; import { displayedScore5, formatRatingValue } from '@/lib/rating-display'; -import { versusHref } from '@/lib/item-compare'; +import { catalogRowSlug } from '@/lib/item-directory'; +import { isParseableVersusPair, versusHref } from '@/lib/item-compare'; import StarRating from '@/components/elementary/StarRating'; // Cached after the first request, refreshed every 5 minutes or when the @@ -452,20 +458,43 @@ export async function ProductDetailView({ getCachedMasterProductReviewSummary(product.id, reviews) ) ); + const currentItemSlug = + itemRoute?.kind === 'render' ? itemRoute.itemSlug : null; + const currentSiblingSlugs = [ + ...(itemRoute?.kind === 'render' + ? itemRoute.variants.map((v) => v.slug) + : []), + ...itemVariants.map((v) => v.variant.slug), + ...itemVariants.map((v) => v.product.slug), + ]; + const currentSiblingIds = [ + ...(itemRoute?.kind === 'render' + ? itemRoute.variants.map((v) => v.id) + : []), + ...itemVariants.map((v) => v.variant.id), + ...itemVariants.map((v) => v.product.id), + ]; const competitorsRead = peersRead.then(async (storedCategoryPeers) => { - const categoryPeers = storedCategoryPeers.map((peer) => - applyCategorySpecTemplate(categorySlug, peer) + // RATE_ITEM_MODEL on: group category peers into one row per Item and drop + // the current Item's sizes before standing and competitor rankings (#1819). + const groupedCategoryPeers = + await groupCatalogRowsByItem(storedCategoryPeers); + const categoryPeers = withoutCurrentItemPeers( + groupedCategoryPeers.map((peer) => + applyCategorySpecTemplate(categorySlug, peer) + ), + { + productId: product.id, + productSlug: product.slug, + itemSlug: currentItemSlug, + siblingSlugs: currentSiblingSlugs, + siblingIds: currentSiblingIds, + } ); - // An Item's other sizes are the same device, not competitors (#1154). // "From" price: the stored stats every other page shows (in-stock // listings, no bait prices), not the offer list (DOS/Rate#1141). const relatedCompetitors = rankRelatedCompetitors( - itemPage - ? withoutItemSiblings( - categoryPeers, - itemVariants.map((v) => v.variant.slug) - ) - : categoryPeers, + categoryPeers, product.slug, currentMinPrice ); @@ -605,11 +634,14 @@ export async function ProductDetailView({ worstRating: 1, } : null; - const jsonLdImages = usableImageUrls(product.images).map(absoluteUrl); - // The product node names a credited photo's licence (DOS/Rate#1756). - const jsonLdProductImages = usableImageUrls(product.images).map((src) => - deviceImageJsonLd(src, absoluteUrl(src)) - ); + // Both Product and ProductGroup nodes name a credited photo's licence (#1756, #1819). + const toProductJsonLdImages = ( + rawImages: Parameters[0] + ) => + usableImageUrls(rawImages).map((src) => + deviceImageJsonLd(src, absoluteUrl(src)) + ); + const jsonLdProductImages = toProductJsonLdImages(product.images); const itemGroupNode = itemPage && isItemGroup ? buildItemProductGroupNode({ @@ -628,15 +660,17 @@ export async function ProductDetailView({ name: displayName, brand: itemPage.item.brand || product.brand, }), - images: jsonLdImages, + images: jsonLdProductImages, variants: itemVariants.map((v) => { const stats = productMarketStats(v.product); + const variantImages = toProductJsonLdImages(v.product.images); return { variantKey: v.variant.variant_key, label: itemVariantLabel(v.variant), minPrice: stats.minPrice, maxPrice: stats.maxPrice, sellerCount: stats.sellerCount, + ...(variantImages.length > 0 ? { images: variantImages } : {}), }; }), aggregateRating, @@ -928,6 +962,33 @@ export async function ProductDetailView({
) : null; + const peerItemSlugMap = buildPeerItemSlugMap( + { slug: product.slug, itemSlug: currentItemSlug }, + relatedCompetitors + ); + const hasMultipleRouteVariants = + isItemGroup || + (itemRoute?.kind === 'render' && itemRoute.variants.length > 1); + const currentCompareRow = { + id: product.id, + slug: product.slug, + item_slug: currentItemSlug, + ...(hasMultipleRouteVariants && defaultVariantKey + ? { variant_key: defaultVariantKey } + : {}), + }; + const singleCompareHref = buildPeerCompareHref( + categorySlug, + [currentCompareRow], + 1 + ); + const compareAllHref = buildPeerCompareHref( + categorySlug, + [currentCompareRow, ...relatedCompetitors], + 4 + ); + const compareAllCount = Math.min(4, relatedCompetitors.length + 1); + return ( @@ -1377,7 +1438,7 @@ export async function ProductDetailView({ {t('tenSpecGroupsBtn')}
{t('headToHeadCompareBtn')} @@ -1637,11 +1698,11 @@ export async function ProductDetailView({ {t('dealHub10PlatformsBtn')} c.slug)].slice(0, 4).join(',')}`} + href={compareAllHref} className="btn btn-sm btn-danger rounded-pill fw-semibold px-3" > {t('compareAllPeersBtn', { - count: Math.min(4, relatedCompetitors.length + 1), + count: compareAllCount, })}
@@ -1652,6 +1713,36 @@ export async function ProductDetailView({ data-testid="same-category-value-alternatives" > {relatedCompetitors.map((peer) => { + const peerCanonicalSlug = resolveCanonicalPeerSlug({ + slug: peer.slug, + item_slug: catalogRowSlug(peer), + }); + const peerDetailHref = peerCanonicalSlug + ? `${categoryBasePath}/${encodeURIComponent(peerCanonicalSlug)}` + : categoryBasePath; + const resolvedVersusHref = resolvePeerVersusHref( + product.slug, + peer.slug, + peerItemSlugMap + ); + const fallbackVersusHref = peerItemSlugMap + ? versusHref(product.slug, peer.slug, peerItemSlugMap) + : versusHref(product.slug, peer.slug); + const candidateVersusHref = + resolvedVersusHref ?? + (peerItemSlugMap ? null : fallbackVersusHref); + const candidateVersusSides = (candidateVersusHref ?? '') + .replace(/^\/compare\//, '') + .split('-vs-'); + const peerVersusHref = + resolvedVersusHref && + candidateVersusSides.length === 2 && + isParseableVersusPair( + candidateVersusSides[0]!, + candidateVersusSides[1]! + ) + ? resolvedVersusHref + : null; const peerPrice = Number(peer.min_price || 0); const priceDelta = peerPriceDelta(peerPrice, currentMinPrice); const peerThumb = productImageUrl(peer); @@ -1722,7 +1813,7 @@ export async function ProductDetailView({

{peer.name} @@ -1774,15 +1865,17 @@ export async function ProductDetailView({

- - {t('compare1v1Btn')} - + {peerVersusHref && ( + + {t('compare1v1Btn')} + + )} {t('detailsBtn')} diff --git a/apps/web/src/lib/__tests__/device-images.test.ts b/apps/web/src/lib/__tests__/device-images.test.ts index 1e00adc9..efea75fd 100644 --- a/apps/web/src/lib/__tests__/device-images.test.ts +++ b/apps/web/src/lib/__tests__/device-images.test.ts @@ -13,6 +13,7 @@ import { deviceImageSrcSet, type DeviceImageCredit, } from '../device-images'; +import { buildItemProductGroupNode } from '../item-page'; const here = dirname(fileURLToPath(import.meta.url)); const devicesDir = join(here, '../../../public/img/devices'); @@ -271,6 +272,204 @@ async function main() { 'https://rate.vn/img/devices/apple-iphone-16-128gb.jpg' ); + // DOS/Rate#1819: Multi-size ProductGroup JSON-LD preserves CC BY-SA + // ImageObject licence/credit metadata on both ProductGroup and hasVariant. + const creditedHero = deviceImageJsonLd( + '/img/devices/sample-phone.jpg', + 'https://rate.vn/img/devices/sample-phone.jpg', + sample + ); + const uncreditedGallery = deviceImageJsonLd( + '/img/devices/apple-iphone-16-128gb.jpg', + 'https://rate.vn/img/devices/apple-iphone-16-128gb.jpg', + sample + ); + const variantSample: Record = { + 'sample-phone-512gb': { + author: 'Variant Photographer', + sourceUrl: + 'https://commons.wikimedia.org/wiki/File:Sample_phone_512gb.jpg', + license: 'CC BY-SA 4.0', + licenseUrl: 'https://creativecommons.org/licenses/by-sa/4.0/', + }, + }; + const variantSpecificCredited = deviceImageJsonLd( + '/img/devices/sample-phone-512gb.jpg', + 'https://rate.vn/img/devices/sample-phone-512gb.jpg', + variantSample + ); + const groupNode = buildItemProductGroupNode({ + itemKey: 'brand:sample-phone', + name: 'Sample Phone', + brand: 'Brand', + canonicalUrl: 'https://rate.vn/smartphones/sample-phone', + description: 'Sample multi-size Item', + images: [ + creditedHero, + uncreditedGallery, + ' ', + {}, + { '@type': 'ImageObject', contentUrl: ' ' }, + ], + variants: [ + { + variantKey: '256gb', + label: '256GB', + minPrice: 19_990_000, + maxPrice: 21_990_000, + sellerCount: 4, + }, + { + variantKey: '512gb', + label: '512GB', + minPrice: 23_990_000, + maxPrice: 24_990_000, + sellerCount: 3, + images: [variantSpecificCredited], + }, + { + variantKey: '1tb', + label: '1TB', + minPrice: 0, + maxPrice: 0, + sellerCount: 0, + }, + ], + }) as { + image?: unknown[]; + hasVariant?: { sku: string; image?: unknown[] }[]; + }; + const expectedHeroImageObject = { + '@type': 'ImageObject', + contentUrl: 'https://rate.vn/img/devices/sample-phone.jpg', + creator: { '@type': 'Person', name: 'Sample Author' }, + creditText: 'Sample Author, Wikimedia Commons, CC BY-SA 4.0', + license: 'https://creativecommons.org/licenses/by-sa/4.0/', + acquireLicensePage: + 'https://commons.wikimedia.org/wiki/File:Sample_phone.jpg', + }; + assert.deepEqual(groupNode.image, [ + expectedHeroImageObject, + 'https://rate.vn/img/devices/apple-iphone-16-128gb.jpg', + ]); + assert.equal(groupNode.hasVariant?.length, 2); + assert.deepEqual( + groupNode.hasVariant![0]!.image, + [ + expectedHeroImageObject, + 'https://rate.vn/img/devices/apple-iphone-16-128gb.jpg', + ], + 'variant without custom images inherits ProductGroup ImageObject metadata' + ); + assert.deepEqual( + groupNode.hasVariant![1]!.image, + [ + { + '@type': 'ImageObject', + contentUrl: 'https://rate.vn/img/devices/sample-phone-512gb.jpg', + creator: { '@type': 'Person', name: 'Variant Photographer' }, + creditText: 'Variant Photographer, Wikimedia Commons, CC BY-SA 4.0', + license: 'https://creativecommons.org/licenses/by-sa/4.0/', + acquireLicensePage: + 'https://commons.wikimedia.org/wiki/File:Sample_phone_512gb.jpg', + }, + ], + 'variant with its own credited photo keeps its own ImageObject licence' + ); + assert.notEqual( + groupNode.image![0], + groupNode.hasVariant![0]!.image![0], + 'ImageObject records are cloned per node' + ); + assert.notEqual( + (groupNode.image![0] as { creator?: unknown }).creator, + (groupNode.hasVariant![0]!.image![0] as { creator?: unknown }).creator, + 'nested creator objects are cloned per node' + ); + + // Fallback when default variant has no images (`input.images = []`) on a + // multi-variant Item: BOTH ProductGroup and priced variants without custom + // images inherit the fallback ImageObject, and null contentUrl is stripped. + const fallbackGroupNode = buildItemProductGroupNode({ + itemKey: 'brand:sample-phone', + name: 'Sample Phone', + brand: 'Brand', + canonicalUrl: 'https://rate.vn/smartphones/sample-phone', + description: 'Sample multi-size Item', + images: [], + variants: [ + { + variantKey: '256gb', + label: '256GB', + minPrice: 19_990_000, + maxPrice: 20_990_000, + sellerCount: 2, + }, + { + variantKey: '512gb', + label: '512GB', + minPrice: 23_990_000, + maxPrice: 24_990_000, + sellerCount: 3, + images: [ + { + '@type': 'ImageObject', + contentUrl: ' https://rate.vn/img/devices/sample-phone.jpg ', + creator: { '@type': 'Person', name: 'Fallback Author' }, + license: 'https://creativecommons.org/licenses/by-sa/4.0/', + }, + { + '@type': 'ImageObject', + contentUrl: null, + url: ' https://rate.vn/img/devices/sample-phone-alt.jpg ', + }, + ], + }, + ], + }) as { + image?: unknown[]; + hasVariant?: { sku: string; image?: unknown[] }[]; + }; + const expectedFallbackImages = [ + { + '@type': 'ImageObject', + contentUrl: 'https://rate.vn/img/devices/sample-phone.jpg', + creator: { '@type': 'Person', name: 'Fallback Author' }, + license: 'https://creativecommons.org/licenses/by-sa/4.0/', + }, + { + '@type': 'ImageObject', + url: 'https://rate.vn/img/devices/sample-phone-alt.jpg', + }, + ]; + assert.deepEqual(fallbackGroupNode.image, expectedFallbackImages); + assert.equal(fallbackGroupNode.hasVariant?.length, 2); + assert.deepEqual( + fallbackGroupNode.hasVariant![0]!.image, + expectedFallbackImages, + 'preceding priced variant without custom images inherits fallbackGroupImages' + ); + assert.deepEqual( + fallbackGroupNode.hasVariant![1]!.image, + expectedFallbackImages + ); + assert.equal( + 'contentUrl' in (fallbackGroupNode.image![1] as Record), + false, + 'null contentUrl is stripped when url is present' + ); + + // ProductDetailView passes jsonLdProductImages (with deviceImageJsonLd) into + // buildItemProductGroupNode rather than plain URL strings (#1819). + assert.match( + productSource, + /buildItemProductGroupNode\(\{[\s\S]*?images:\s*jsonLdProductImages,/ + ); + assert.doesNotMatch( + productSource, + /const jsonLdImages = usableImageUrls\(product\.images\)\.map\(absoluteUrl\)/ + ); + console.warn('✅ Device image renditions suite passed'); } diff --git a/apps/web/src/lib/__tests__/product-competitors.test.ts b/apps/web/src/lib/__tests__/product-competitors.test.ts index 02324471..905622b1 100644 --- a/apps/web/src/lib/__tests__/product-competitors.test.ts +++ b/apps/web/src/lib/__tests__/product-competitors.test.ts @@ -1,15 +1,36 @@ import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import type { MasterProduct } from '@repo/shared-data'; import { buildKeySpecLine, peerPriceDelta, rankRelatedCompetitors, } from '../product-competitors'; +import { + buildPeerCompareHref, + buildPeerItemSlugMap, + resolveCanonicalPeerSlug, + resolvePeerCompareToken, + resolvePeerVersusHref, + withoutCurrentItemPeers, +} from '../item-page'; +import { catalogRowSlug } from '../item-directory'; +import { + isParseableVersusPair, + planVersusRoute, + type CompareResolution, +} from '../item-compare'; +import { computeProductStandingInCategory } from '../leaderboard-engine'; import { resolveCompareEffectiveUserRating, resolveCompareExpertRating10, } from '../rate-domain'; -function main() { +const here = dirname(fileURLToPath(import.meta.url)); + +async function main() { console.warn('🧪 Product competitors suite (#558)'); // Priced peers come first, closest price first; unpriced peers go last. @@ -128,7 +149,730 @@ function main() { 'A18 Pro • 8GB • 256GB • 4685 mAh' ); + // DOS/Rate#1819: Under RATE_ITEM_MODEL, group category peers by Item before + // standing & competitor rankings, emit canonical Item URLs, and guard 1v1 + // versus links. + const prevSupabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL; + const prevSupabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; + const prevItemModel = process.env.RATE_ITEM_MODEL; + const prevFetch = globalThis.fetch; + const { resetRateClientForTests } = await import('../rate-api'); + try { + process.env.NEXT_PUBLIC_SUPABASE_URL = 'https://stub.supabase.test'; + process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY = 'anon-test-key'; + resetRateClientForTests(); + + const CAT_PHONES = 'c0000000-0000-4000-8000-000000001819'; + const ITEM_SELF = '11111111-1111-4111-8111-111111111819'; + const ITEM_S25 = '11111111-1111-4111-8111-222222221819'; + const ITEM_XIAOMI = '11111111-1111-4111-8111-333333331819'; + const ITEM_VS_SLUG = '11111111-1111-4111-8111-444444441819'; + + const V_SELF_128 = '22222222-2222-4222-8222-111111110128'; + const V_SELF_256 = '22222222-2222-4222-8222-111111110256'; + const V_S25_256 = '22222222-2222-4222-8222-222222220256'; + const V_S25_512 = '22222222-2222-4222-8222-222222220512'; + const V_S25_1TB = '22222222-2222-4222-8222-222222221024'; + const V_XIAOMI_512 = '22222222-2222-4222-8222-333333330512'; + const V_VS_STD = '22222222-2222-4222-8222-444444440001'; + const V_PIXEL_LONE = '22222222-2222-4222-8222-555555550128'; + + const makePeer = ( + id: string, + slug: string, + name: string, + brand: string, + minPrice: number, + ratingScore: number, + storageGb: number | null = 256 + ): MasterProduct => + ({ + id, + slug, + name, + brand, + model: name, + model_key: `${brand.toLowerCase()}:${slug}`, + variant_name: storageGb ? `${storageGb}GB` : null, + category_id: CAT_PHONES, + status: 'published', + storage_gb: storageGb, + ram_gb: 12, + min_price: minPrice, + max_price: minPrice + 1_000_000, + merchant_count: 3, + rating_score: ratingScore, + user_rating_weighted: ratingScore, + average_rating: ratingScore, + expert_rating: 9.0, + authentic_review_count: 12, + total_reviews: 12, + images: [], + thumbnail: null, + key_specs: { chip: 'Flagship SoC', battery: '5000 mAh' }, + metadata: { category_slug: 'smartphones' }, + specifications: { + category_slug: 'smartphones', + } as MasterProduct['specifications'], + }) as unknown as MasterProduct; + + const storedCategoryPeers: MasterProduct[] = [ + makePeer( + V_SELF_128, + 'apple-iphone-16-pro-128gb', + 'Apple iPhone 16 Pro 128GB', + 'Apple', + 25_990_000, + 4.8, + 128 + ), + makePeer( + V_SELF_256, + 'apple-iphone-16-pro-256gb', + 'Apple iPhone 16 Pro 256GB', + 'Apple', + 28_990_000, + 4.8, + 256 + ), + makePeer( + V_S25_256, + 'samsung-galaxy-s25-ultra-256gb', + 'Samsung Galaxy S25 Ultra 256GB', + 'Samsung', + 27_990_000, + 4.9, + 256 + ), + makePeer( + V_S25_512, + 'samsung-galaxy-s25-ultra-512gb', + 'Samsung Galaxy S25 Ultra 512GB', + 'Samsung', + 29_490_000, + 4.9, + 512 + ), + makePeer( + V_S25_1TB, + 'samsung-galaxy-s25-ultra-1tb', + 'Samsung Galaxy S25 Ultra 1TB', + 'Samsung', + 30_990_000, + 4.9, + 1024 + ), + makePeer( + V_XIAOMI_512, + 'xiaomi-15-ultra-512gb', + 'Xiaomi 15 Ultra 512GB', + 'Xiaomi', + 24_990_000, + 4.7, + 512 + ), + makePeer( + V_VS_STD, + 'rog-vs-edition-256gb', + 'ASUS ROG VS Edition 256GB', + 'ASUS', + 26_990_000, + 4.6, + 256 + ), + makePeer( + V_PIXEL_LONE, + 'google-pixel-10-pro', + 'Google Pixel 10 Pro', + 'Google', + 23_990_000, + 4.6, + 256 + ), + ]; + + globalThis.fetch = (async (input: RequestInfo | URL) => { + const url = new URL( + typeof input === 'string' + ? input + : input instanceof URL + ? input.href + : input.url + ); + const q = decodeURIComponent(url.search); + const json = (body: unknown) => + new Response(JSON.stringify(body), { + status: 200, + headers: { + 'content-type': 'application/json', + 'content-range': '0-0/*', + }, + }); + if (Number(url.searchParams.get('offset') ?? 0) > 0) return json([]); + if (url.pathname === '/rest/v1/items') { + return json([ + { + id: ITEM_SELF, + item_key: 'apple:iphone-16-pro', + slug: 'Apple-iPhone-16-Pro', + name: 'Apple iPhone 16 Pro', + brand: 'Apple', + model: 'iPhone 16 Pro', + category_id: CAT_PHONES, + default_variant_id: V_SELF_256, + min_price: 25_990_000, + max_price: 29_990_000, + price_variant_count: 2, + merchant_count: 6, + listing_count: 8, + unknown_variant_listing_count: 0, + updated_at: '2026-11-21T00:00:00Z', + }, + { + id: ITEM_S25, + item_key: 'samsung:galaxy-s25-ultra', + slug: 'samsung-galaxy-s25-ultra', + name: 'Samsung Galaxy S25 Ultra', + brand: 'Samsung', + model: 'Galaxy S25 Ultra', + category_id: CAT_PHONES, + default_variant_id: V_S25_256, + min_price: 27_990_000, + max_price: 31_990_000, + price_variant_count: 3, + merchant_count: 7, + listing_count: 9, + unknown_variant_listing_count: 0, + updated_at: '2026-11-21T00:00:00Z', + }, + { + id: ITEM_XIAOMI, + item_key: 'xiaomi:15-ultra', + slug: 'xiaomi-15-ultra', + name: 'Xiaomi 15 Ultra', + brand: 'Xiaomi', + model: '15 Ultra', + category_id: CAT_PHONES, + default_variant_id: V_XIAOMI_512, + min_price: 24_990_000, + max_price: 25_990_000, + price_variant_count: 1, + merchant_count: 4, + listing_count: 4, + unknown_variant_listing_count: 0, + updated_at: '2026-11-21T00:00:00Z', + }, + { + id: ITEM_VS_SLUG, + item_key: 'asus:rog-vs-edition', + slug: 'asus-rog-vs-edition', + name: 'ASUS ROG VS Edition', + brand: 'ASUS', + model: 'ROG VS Edition', + category_id: CAT_PHONES, + default_variant_id: V_VS_STD, + min_price: 26_990_000, + max_price: 27_990_000, + price_variant_count: 1, + merchant_count: 2, + listing_count: 2, + unknown_variant_listing_count: 0, + updated_at: '2026-11-21T00:00:00Z', + }, + ]); + } + if (url.pathname === '/rest/v1/master_products') { + if (q.includes('select=id,item_id&')) { + return json([ + { id: V_SELF_128, item_id: ITEM_SELF }, + { id: V_SELF_256, item_id: ITEM_SELF }, + { id: V_S25_256, item_id: ITEM_S25 }, + { id: V_S25_512, item_id: ITEM_S25 }, + { id: V_S25_1TB, item_id: ITEM_S25 }, + { id: V_XIAOMI_512, item_id: ITEM_XIAOMI }, + { id: V_VS_STD, item_id: ITEM_VS_SLUG }, + { id: V_PIXEL_LONE, item_id: null }, + ]); + } + if (q.includes('item_id=in.')) { + return json([ + { + id: V_SELF_128, + item_id: ITEM_SELF, + slug: 'apple-iphone-16-pro-128gb', + name: 'Apple iPhone 16 Pro 128GB', + variant_key: '128gb', + variant_name: '128GB', + sort_order: 1, + storage_gb: 128, + min_price: 25_990_000, + max_price: 26_990_000, + merchant_count: 4, + }, + { + id: V_SELF_256, + item_id: ITEM_SELF, + slug: 'apple-iphone-16-pro-256gb', + name: 'Apple iPhone 16 Pro 256GB', + variant_key: '256gb', + variant_name: '256GB', + sort_order: 2, + storage_gb: 256, + min_price: 28_990_000, + max_price: 29_990_000, + merchant_count: 5, + }, + { + id: V_S25_256, + item_id: ITEM_S25, + slug: 'samsung-galaxy-s25-ultra-256gb', + name: 'Samsung Galaxy S25 Ultra 256GB', + variant_key: '256gb', + variant_name: '256GB', + sort_order: 1, + storage_gb: 256, + min_price: 27_990_000, + max_price: 28_990_000, + merchant_count: 5, + }, + { + id: V_S25_512, + item_id: ITEM_S25, + slug: 'samsung-galaxy-s25-ultra-512gb', + name: 'Samsung Galaxy S25 Ultra 512GB', + variant_key: '512gb', + variant_name: '512GB', + sort_order: 2, + storage_gb: 512, + min_price: 29_490_000, + max_price: 30_490_000, + merchant_count: 4, + }, + { + id: V_S25_1TB, + item_id: ITEM_S25, + slug: 'samsung-galaxy-s25-ultra-1tb', + name: 'Samsung Galaxy S25 Ultra 1TB', + variant_key: '1024gb', + variant_name: '1TB', + sort_order: 3, + storage_gb: 1024, + min_price: 30_990_000, + max_price: 31_990_000, + merchant_count: 3, + }, + { + id: V_XIAOMI_512, + item_id: ITEM_XIAOMI, + slug: 'xiaomi-15-ultra-512gb', + name: 'Xiaomi 15 Ultra 512GB', + variant_key: '512gb', + variant_name: '512GB', + sort_order: 1, + storage_gb: 512, + min_price: 24_990_000, + max_price: 25_990_000, + merchant_count: 4, + }, + { + id: V_VS_STD, + item_id: ITEM_VS_SLUG, + slug: 'rog-vs-edition-256gb', + name: 'ASUS ROG VS Edition 256GB', + variant_key: '256gb', + variant_name: '256GB', + sort_order: 1, + storage_gb: 256, + min_price: 26_990_000, + max_price: 27_990_000, + merchant_count: 2, + }, + ]); + } + } + return json([]); + }) as typeof fetch; + + const { groupCatalogRowsByItem } = + await import('../../server/rate/item-directory'); + const currentProduct = storedCategoryPeers[1]!; // apple-iphone-16-pro-256gb (28.99M) + + // Flag on: 3 Samsung storage sizes collapse into 1 rival Item, self Item + // (standing under 128gb) is excluded, and standing counts 5 distinct Items. + process.env.RATE_ITEM_MODEL = 'item'; + const groupedOn = await groupCatalogRowsByItem(storedCategoryPeers); + const rivalPeersOn = withoutCurrentItemPeers(groupedOn, { + productId: currentProduct.id, + productSlug: currentProduct.slug, + itemSlug: 'Apple-iPhone-16-Pro', + // Simulate 128gb failing loadItemPageContext so only itemSlug/route knows it. + siblingSlugs: [currentProduct.slug], + siblingIds: [currentProduct.id], + }); + assert.deepEqual( + rivalPeersOn.map((p) => resolveCanonicalPeerSlug(p)), + [ + 'samsung-galaxy-s25-ultra', + 'xiaomi-15-ultra', + 'asus-rog-vs-edition', + 'google-pixel-10-pro', + ], + 'rival peers contain at most one entry per Item and exclude current Item' + ); + const rankedOn = rankRelatedCompetitors( + rivalPeersOn, + currentProduct.slug, + Number(currentProduct.min_price), + 4 + ); + assert.deepEqual( + rankedOn.map((p) => ({ + slug: p.slug, + canonical: resolveCanonicalPeerSlug({ + slug: p.slug, + item_slug: catalogRowSlug(p), + }), + name: p.name, + })), + [ + { + slug: 'samsung-galaxy-s25-ultra-256gb', + canonical: 'samsung-galaxy-s25-ultra', + name: 'Samsung Galaxy S25 Ultra', + }, + { + slug: 'rog-vs-edition-256gb', + canonical: 'asus-rog-vs-edition', + name: 'ASUS ROG VS Edition', + }, + { + slug: 'xiaomi-15-ultra-512gb', + canonical: 'xiaomi-15-ultra', + name: 'Xiaomi 15 Ultra', + }, + { + slug: 'google-pixel-10-pro', + canonical: 'google-pixel-10-pro', + name: 'Google Pixel 10 Pro', + }, + ], + 'Samsung S25 Ultra appears once (cheapest 256GB standing row) instead of 3 times' + ); + + const standingOn = computeProductStandingInCategory( + currentProduct, + rivalPeersOn + ); + const combinedBadgeOn = standingOn.find((b) => b.criterion === 'combined'); + assert.ok(combinedBadgeOn); + assert.equal( + combinedBadgeOn.totalInCategory, + 5, + 'category standing counts 5 distinct Items (4 rivals + current Item)' + ); + assert.equal( + combinedBadgeOn.rank, + 2, + 'ranks #2 behind 1 Samsung Item rather than #4 behind 3 Samsung variants' + ); + + // Canonical & guarded 1v1 versus links: + const peerSlugMap = buildPeerItemSlugMap( + { slug: currentProduct.slug, itemSlug: 'Apple-iPhone-16-Pro' }, + rankedOn + ); + assert.ok(peerSlugMap); + // 1. Samsung S25 Ultra -> canonical sorted Item 1v1 URL, 0 redirect hops. + const vsSamsung = resolvePeerVersusHref( + currentProduct.slug, + rankedOn[0]!.slug, + peerSlugMap + ); + assert.equal( + vsSamsung, + '/compare/Apple-iPhone-16-Pro-vs-samsung-galaxy-s25-ultra' + ); + const s25Sides = vsSamsung!.replace(/^\/compare\//, '').split('-vs-'); + assert.equal(s25Sides.length, 2); + assert.equal(isParseableVersusPair(s25Sides[0]!, s25Sides[1]!), true); + const selfRes: CompareResolution = { + kind: 'item', + item: { + id: ITEM_SELF, + slug: 'Apple-iPhone-16-Pro', + default_variant_id: V_SELF_256, + }, + variants: [ + { + id: V_SELF_256, + slug: 'apple-iphone-16-pro-256gb', + variant_key: '256gb', + }, + ], + }; + const s25Res: CompareResolution = { + kind: 'item', + item: { + id: ITEM_S25, + slug: 'samsung-galaxy-s25-ultra', + default_variant_id: V_S25_256, + }, + variants: [ + { + id: V_S25_256, + slug: 'samsung-galaxy-s25-ultra-256gb', + variant_key: '256gb', + }, + ], + }; + assert.deepEqual( + planVersusRoute( + { slug: s25Sides[0]!, resolution: selfRes }, + { slug: s25Sides[1]!, resolution: s25Res } + ), + { + kind: 'render', + slugA: 'apple-iphone-16-pro-256gb', + slugB: 'samsung-galaxy-s25-ultra-256gb', + canonical: vsSamsung, + }, + 'emitted 1v1 link renders directly in planVersusRoute without 308 or 404' + ); + + // 2. ASUS ROG VS Edition (`item_slug: 'asus-rog-vs-edition'`) -> detail URL + // uses canonical Item slug, while 1v1 link is suppressed (null) because + // `planVersusRoute` 404s any Item slug containing `-vs-` (#1275). + assert.equal(resolveCanonicalPeerSlug(rankedOn[1]!), 'asus-rog-vs-edition'); + assert.equal( + resolvePeerVersusHref( + currentProduct.slug, + rankedOn[1]!.slug, + peerSlugMap + ), + null, + 'suppresses 1v1 link when rival Item slug contains -vs-' + ); + + // 3. Unlinked Google Pixel 10 Pro -> pairs canonical current Item with lone slug. + assert.equal( + resolvePeerVersusHref( + currentProduct.slug, + rankedOn[3]!.slug, + peerSlugMap + ), + '/compare/Apple-iPhone-16-Pro-vs-google-pixel-10-pro' + ); + + // 4. Multi-item `/compare?slugs=` tokens pin multi-size Items and keep + // single-size Item / unlinked slugs unambiguous. + assert.equal( + resolvePeerCompareToken({ + id: currentProduct.id, + slug: currentProduct.slug, + item_slug: 'Apple-iPhone-16-Pro', + variant_key: '256gb', + }), + 'Apple-iPhone-16-Pro@256gb' + ); + assert.equal( + resolvePeerCompareToken(rankedOn[0]!), + 'samsung-galaxy-s25-ultra@256gb' + ); + assert.equal(resolvePeerCompareToken(rankedOn[2]!), 'xiaomi-15-ultra'); + assert.equal(resolvePeerCompareToken(rankedOn[3]!), 'google-pixel-10-pro'); + + // Shared-slug multi-size Item (`item_slug === slug`) still pins `@variantKey` + // so `/compare` selects the standing size instead of `default_variant_id`. + assert.equal( + resolvePeerCompareToken({ + id: 'v-base-256', + slug: 'base', + item_slug: 'base', + item_variants: [ + { id: 'v-base-256', variant_key: '256gb' }, + { id: 'v-base-512', variant_key: '512gb' }, + ], + }), + 'base@256gb' + ); + + // `buildPeerCompareHref` preserves category (`cat=`) and appends + // `variant.=` fallback hints for irregular / 1TB / shared-slug + // / retail-alias masters while staying clean when pins match directly. + assert.equal( + buildPeerCompareHref('smartphones', [ + { + id: currentProduct.id, + slug: 'apple-iphone-16-pro-256gb', + item_slug: 'apple-iphone-16-pro', + variant_key: '256gb', + }, + { + id: V_S25_256, + slug: 'samsung-galaxy-s25-ultra-256gb', + item_slug: 'samsung-galaxy-s25-ultra', + variant_key: '256gb', + }, + ]), + '/compare?cat=smartphones&slugs=apple-iphone-16-pro@256gb,samsung-galaxy-s25-ultra@256gb' + ); + assert.equal( + buildPeerCompareHref('laptops', [ + { + id: 'v-1tb', + slug: 'macbook-pro-14-1tb', + item_slug: 'macbook-pro-14', + variant_key: '1024gb', + }, + { + id: 'v-base-256', + slug: 'base', + item_slug: 'base', + variant_key: '256gb', + }, + ]), + '/compare?cat=laptops&slugs=macbook-pro-14@1024gb,base@256gb&variant.macbook-pro-14%401024gb=macbook-pro-14-1tb&variant.base%40256gb=base' + ); + + // `nullable_variant` mode groups identically to `item` mode: + process.env.RATE_ITEM_MODEL = 'nullable_variant'; + const groupedNullable = await groupCatalogRowsByItem(storedCategoryPeers); + assert.equal(groupedNullable.length, groupedOn.length); + + // 5. Edge-case guards: unsafe Item & fallback slugs, control characters, + // prototype keys, same-Item pairs, `-vs` suffix reversal, and unparseable + // `-vs` collisions. + for (const unsafe of [ + '../evil', + 'a/b', + '__proto__', + 'constructor', + 'null', + '0', + `a${'b'.repeat(120)}`, + ]) { + assert.equal( + resolveCanonicalPeerSlug({ + slug: 'safe-peer-slug', + item_slug: unsafe, + }), + 'safe-peer-slug', + `unsafe item_slug ${unsafe} falls back to variant slug` + ); + assert.equal( + resolveCanonicalPeerSlug({ + slug: unsafe, + item_slug: unsafe, + }), + '', + `unsafe fallback slug ${unsafe} returns empty string` + ); + } + assert.equal(resolveCanonicalPeerSlug({ slug: '..' }), ''); + assert.equal( + resolveCanonicalPeerSlug({ slug: '_retail-1tb' }), + '_retail-1tb', + 'underscore-prefixed legacy master slug remains valid' + ); + assert.deepEqual( + withoutCurrentItemPeers( + [ + { slug: 'apple-iphone-16-pro-256gb\u0000' }, + { slug: '..' }, + { slug: 'constructor', item_slug: 'constructor' }, + { slug: 'valid-rival-phone\u0000' }, + ], + { productSlug: 'apple-iphone-16-pro-256gb' } + ).map((p) => resolveCanonicalPeerSlug(p)), + ['valid-rival-phone'], + 'strips control chars when matching skipSlugs and drops unsafe slugs' + ); + const protoMap = buildPeerItemSlugMap( + { slug: '__proto__', itemSlug: 'safe-item-a' }, + [{ slug: 'constructor', item_slug: 'safe-item-b' }] + ); + assert.equal(Object.getPrototypeOf(protoMap), null); + assert.equal( + resolvePeerVersusHref('__proto__', 'constructor', protoMap), + '/compare/safe-item-a-vs-safe-item-b' + ); + assert.equal( + resolvePeerVersusHref('v-a', 'v-b', { + 'v-a': 'Same-Item', + 'v-b': 'same-item', + }), + null, + 'same Item across two variant slugs never emits a 1v1 link' + ); + assert.equal( + resolvePeerVersusHref('v-a', 'v-b', { + 'v-a': 'a-phone-vs', + 'v-b': 'z-phone', + }), + '/compare/z-phone-vs-a-phone-vs', + 'slug ending in -vs reverses to the parseable order' + ); + assert.equal( + resolvePeerVersusHref('vs-a-phone-vs', 'vs-b-phone-vs'), + null, + 'pair unparseable in both orders returns null' + ); + + // 6. Flag off: peers stay ungrouped, map is null, 1v1 links use variant slugs. + delete process.env.RATE_ITEM_MODEL; + const groupedOff = await groupCatalogRowsByItem(storedCategoryPeers); + assert.equal(groupedOff, storedCategoryPeers); + assert.equal( + buildPeerItemSlugMap( + { slug: currentProduct.slug, itemSlug: null }, + groupedOff + ), + null + ); + assert.equal( + resolvePeerVersusHref( + currentProduct.slug, + 'samsung-galaxy-s25-ultra-256gb', + null + ), + '/compare/apple-iphone-16-pro-256gb-vs-samsung-galaxy-s25-ultra-256gb' + ); + } finally { + globalThis.fetch = prevFetch; + if (prevSupabaseUrl === undefined) { + delete process.env.NEXT_PUBLIC_SUPABASE_URL; + } else { + process.env.NEXT_PUBLIC_SUPABASE_URL = prevSupabaseUrl; + } + if (prevSupabaseAnonKey === undefined) { + delete process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; + } else { + process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY = prevSupabaseAnonKey; + } + if (prevItemModel === undefined) { + delete process.env.RATE_ITEM_MODEL; + } else { + process.env.RATE_ITEM_MODEL = prevItemModel; + } + resetRateClientForTests(); + } + + // Verify ProductDetailView wires groupCatalogRowsByItem, canonical Item hrefs, + // and guarded 1v1 links (#1819). + const pdvSource = readFileSync( + join(here, '../../app/[locale]/smartphones/[slug]/page.tsx'), + 'utf8' + ); + assert.match(pdvSource, /groupCatalogRowsByItem\(storedCategoryPeers\)/); + assert.match(pdvSource, /withoutCurrentItemPeers\(/); + assert.match(pdvSource, /catalogRowSlug\(peer\)/); + assert.match(pdvSource, /resolvePeerVersusHref\(/); + assert.match(pdvSource, /isParseableVersusPair\(/); + console.warn('✅ Product competitors suite passed'); } -main(); +void main().catch((error) => { + console.error(error); + process.exit(1); +}); diff --git a/apps/web/src/lib/item-page.ts b/apps/web/src/lib/item-page.ts index 53589316..16526dd8 100644 --- a/apps/web/src/lib/item-page.ts +++ b/apps/web/src/lib/item-page.ts @@ -1,7 +1,15 @@ /** - * Pure helpers for the product page per Item (DOS/Rate#1154; design doc + * Pure helpers for the product page per Item (DOS/Rate#1154, #1819; design doc * `Docs/Proposals/2026-10-27-item-variant-listing-model.md`, section 5.1). */ +import { catalogRowSlug } from './item-directory'; +import { + canonicalVersusOrder, + isParseableVersusPair, + itemSlugFromProductAnswer, + parseCompareToken, + versusHref, +} from './item-compare'; /** The `?v=` value shape the redirects write (a literal `variant_key`). */ const VARIANT_KEY_REGEX = /^[a-z0-9][a-z0-9-]{0,40}$/; @@ -74,12 +82,462 @@ export function withoutItemSiblings( return peers.filter((peer) => !skip.has(peer.slug)); } +export interface ItemPeerCurrentContext { + productId?: string | null; + productSlug: string; + itemSlug?: string | null; + siblingSlugs?: Iterable; + siblingIds?: Iterable; +} + +const COMPARE_PIN_TOKEN_REGEX = + /^[a-z0-9][a-z0-9._-]{0,159}@[a-z0-9][a-z0-9-]{0,40}$/i; +const SAFE_CATALOG_SLUG_REGEX = /^(?=.*[a-zA-Z0-9])[a-zA-Z0-9._-]{1,120}$/; +const UNSAFE_CATALOG_SLUG_KEYS = new Set([ + '0', + 'null', + 'undefined', + 'nan', + 'none', + 'na', + 'infinity', + '-infinity', + 'false', + 'true', + '__proto__', + 'constructor', + 'prototype', + 'tostring', + 'tolocalestring', + 'valueof', + 'hasownproperty', + 'isprototypeof', + 'propertyisenumerable', + ...Object.getOwnPropertyNames(Object.prototype).map((key) => + key.toLowerCase() + ), +]); + +function stripControlAndTrim(raw: unknown): string { + return typeof raw === 'string' ? raw.replace(/\p{Cc}+/gu, '').trim() : ''; +} + +function isSafeCatalogSlugToken(slug: string): boolean { + return ( + Boolean(slug) && + SAFE_CATALOG_SLUG_REGEX.test(slug) && + !/^0+$/.test(slug) && + !slug.startsWith('.') && + !slug.endsWith('.') && + !slug.includes('..') && + !(slug in Object.prototype) && + !UNSAFE_CATALOG_SLUG_KEYS.has(slug.toLowerCase()) + ); +} + +function isValidVersusSlugToken(slug: string): boolean { + return ( + Boolean(slug) && + itemSlugFromProductAnswer({ ok: true, data: { item: { slug } } }, '') === + slug + ); +} + +function setOwnSlugEntry( + map: Record, + key: string, + value: string +): void { + if (key === '__proto__') { + Object.defineProperty(map, key, { + value, + enumerable: true, + configurable: true, + writable: true, + }); + } else { + map[key] = value; + } +} + +function getOwnSlugEntry( + map: Readonly> | null | undefined, + key: string +): string | undefined { + if (!map || !key || !Object.prototype.hasOwnProperty.call(map, key)) { + return undefined; + } + const clean = stripControlAndTrim(map[key]); + return clean.length > 0 ? clean : undefined; +} + +/** + * The canonical slug a catalogue row links to under `/{category}/{slug}`: + * its Item slug when valid (preserving mixed case, dots and underscores), + * falling back to the validated variant slug when unlinked or malformed, + * or `''` when neither is a safe catalogue slug (DOS/Rate#1819). + */ +export function resolveCanonicalPeerSlug(row: { + slug: string; + item_slug?: string | null; +}): string { + const rawFallback = stripControlAndTrim(row?.slug); + const safeFallback = isSafeCatalogSlugToken(rawFallback) ? rawFallback : ''; + const rawItemSlug = stripControlAndTrim(row?.item_slug); + const candidate = stripControlAndTrim( + catalogRowSlug({ + slug: safeFallback, + ...(rawItemSlug ? { item_slug: rawItemSlug } : {}), + }) + ); + if (candidate && isValidVersusSlugToken(candidate)) { + return candidate; + } + return safeFallback; +} + +/** + * Category peers excluding the current product and every variant of its Item, + * deduplicated so at most one entry per rival Item remains (DOS/Rate#1819). + */ +export function withoutCurrentItemPeers< + T extends { + id?: string; + slug: string; + item_slug?: string; + item_variants?: readonly { id: string }[]; + }, +>(peers: readonly T[], current: ItemPeerCurrentContext): T[] { + if (!Array.isArray(peers)) return []; + const skipSlugs = new Set(); + const addSlug = (raw: string | null | undefined) => { + const clean = stripControlAndTrim(raw).toLowerCase(); + if (clean) skipSlugs.add(clean); + }; + addSlug(current.productSlug); + addSlug(current.itemSlug); + if (current.siblingSlugs) { + for (const s of current.siblingSlugs) addSlug(s); + } + + const skipIds = new Set(); + const addId = (raw: string | null | undefined) => { + const clean = stripControlAndTrim(raw); + if (clean) skipIds.add(clean); + }; + addId(current.productId); + if (current.siblingIds) { + for (const id of current.siblingIds) addId(id); + } + + const seenRivals = new Set(); + const out: T[] = []; + for (const peer of peers) { + if ( + !peer || + typeof peer !== 'object' || + Array.isArray(peer) || + typeof peer.slug !== 'string' + ) { + continue; + } + const rawSlug = stripControlAndTrim(peer.slug); + if (!rawSlug) continue; + const peerId = stripControlAndTrim(peer.id); + if (peerId && skipIds.has(peerId)) continue; + if (skipSlugs.has(rawSlug.toLowerCase())) continue; + + const rawItemSlug = stripControlAndTrim(peer.item_slug); + if (rawItemSlug && skipSlugs.has(rawItemSlug.toLowerCase())) continue; + const rawVariants = peer.item_variants; + if ( + Array.isArray(rawVariants) && + rawVariants.some((v: { id?: unknown }) => { + const vid = v ? stripControlAndTrim(v.id) : ''; + return Boolean(vid) && skipIds.has(vid); + }) + ) { + continue; + } + + const dedupeKey = resolveCanonicalPeerSlug(peer).toLowerCase(); + if (!dedupeKey || seenRivals.has(dedupeKey)) continue; + seenRivals.add(dedupeKey); + out.push(peer); + } + return out; +} + +/** + * Variant-slug to Item-slug map for in-app 1v1 versus links on a product page, + * or null when neither the current product nor any peer carries an Item slug. + */ +export function buildPeerItemSlugMap( + current: { slug: string; itemSlug?: string | null }, + peers: readonly { slug: string; item_slug?: string | null }[] +): Record | null { + const map = Object.create(null) as Record; + let count = 0; + const currentSlug = stripControlAndTrim(current.slug); + const currentItem = stripControlAndTrim(current.itemSlug); + if (currentSlug && currentItem) { + setOwnSlugEntry(map, currentSlug, currentItem); + count++; + } + if (Array.isArray(peers)) { + for (const peer of peers) { + if (!peer || typeof peer.slug !== 'string') continue; + const slug = stripControlAndTrim(peer.slug); + const itemSlug = stripControlAndTrim(peer.item_slug); + if (slug && itemSlug && !getOwnSlugEntry(map, slug)) { + setOwnSlugEntry(map, slug, itemSlug); + count++; + } + } + } + return count > 0 ? map : null; +} + +/** + * Canonical, guarded `/compare/` link for two catalogue rows, or null + * when the pair names the same Item, either Item slug holds `-vs-`, or the + * resulting pair would not round-trip `/compare/[versus]` parsing (#1819). + */ +export function resolvePeerVersusHref( + slugA: string, + slugB: string, + itemSlugBySlug?: Readonly> | null +): string | null { + const cleanA = stripControlAndTrim(slugA); + const cleanB = stripControlAndTrim(slugB); + if (!cleanA || !cleanB || cleanA.toLowerCase() === cleanB.toLowerCase()) { + return null; + } + const rawItemA = getOwnSlugEntry(itemSlugBySlug, cleanA); + const rawItemB = getOwnSlugEntry(itemSlugBySlug, cleanB); + if ( + rawItemA && + rawItemB && + rawItemA.toLowerCase() === rawItemB.toLowerCase() + ) { + return null; + } + // `planVersusRoute` returns `not_found` whenever either side's Item slug + // contains `-vs-` (#1275), even if `versusHref` fell back to a variant slug. + if ( + rawItemA?.toLowerCase().includes('-vs-') || + rawItemB?.toLowerCase().includes('-vs-') + ) { + return null; + } + const resolveSide = (slug: string, rawItem: string | undefined): string => { + if (!rawItem) return slug; + return isValidVersusSlugToken(rawItem) && + !rawItem.toLowerCase().includes('-vs-') + ? rawItem + : slug; + }; + const sideA = resolveSide(cleanA, rawItemA); + const sideB = resolveSide(cleanB, rawItemB); + if ( + !isValidVersusSlugToken(sideA) || + !isValidVersusSlugToken(sideB) || + sideA.toLowerCase().includes('-vs-') || + sideB.toLowerCase().includes('-vs-') || + sideA.toLowerCase() === sideB.toLowerCase() + ) { + return null; + } + const [first, second] = canonicalVersusOrder(sideA, sideB); + if ( + !isParseableVersusPair(first, second) || + !isParseableVersusPair(first.toLowerCase(), second.toLowerCase()) + ) { + return null; + } + return itemSlugBySlug + ? versusHref(cleanA, cleanB, itemSlugBySlug) + : versusHref(cleanA, cleanB); +} + +export interface PeerCompareRowInput { + id?: string; + slug: string; + item_slug?: string | null; + variant_key?: string | null; + item_variants?: readonly { id: string; variant_key: string }[]; +} + +/** + * Multi-item `/compare?slugs=` token for a product or grouped Item row: pins + * multi-size Items (including shared-slug Items) as `itemSlug@variantKey` so + * compare resolves the exact standing size (#1819). + */ +export function resolvePeerCompareToken(row: PeerCompareRowInput): string { + const rawSlug = stripControlAndTrim(row?.slug); + const safeSlug = isSafeCatalogSlugToken(rawSlug) ? rawSlug : ''; + const rawItemSlug = stripControlAndTrim(row?.item_slug); + const hasValidItem = Boolean( + rawItemSlug && isValidVersusSlugToken(rawItemSlug) + ); + const canonical = resolveCanonicalPeerSlug(row); + if ( + hasValidItem && + canonical && + ((Array.isArray(row.item_variants) && row.item_variants.length > 1) || + Boolean(cleanVariantKey(row.variant_key))) + ) { + const cleanId = stripControlAndTrim(row.id); + const matchedKey = + cleanVariantKey(row.variant_key) ?? + cleanVariantKey( + cleanId && Array.isArray(row.item_variants) + ? row.item_variants.find((v) => stripControlAndTrim(v.id) === cleanId) + ?.variant_key + : null + ); + const pinned = matchedKey ? `${canonical}@${matchedKey}` : ''; + return pinned && COMPARE_PIN_TOKEN_REGEX.test(pinned) ? pinned : safeSlug; + } + return canonical || safeSlug; +} + +/** + * Multi-item `/compare` link with `cat=` and `variant.` + * fallback hints when `legacyTarget` would not directly infer `variantSlug` + * (e.g. `1024gb` -> `-1tb`, `standard`, shared-slug multi-size Items, or + * single-size retail aliases; DOS/Rate#1782, #1819). + */ +export function buildPeerCompareHref( + categorySlug: string, + rows: readonly PeerCompareRowInput[], + limit = 4 +): string { + const safeCategory = stripControlAndTrim(categorySlug) || 'smartphones'; + const safeLimit = + typeof limit === 'number' && Number.isFinite(limit) && limit > 0 + ? Math.min(4, Math.floor(limit)) + : 4; + const entries: Array<{ token: string; variantSlug: string }> = []; + const seenTokens = new Set(); + const seenVariants = new Set(); + if (Array.isArray(rows)) { + for (const row of rows) { + if (!row || typeof row.slug !== 'string') continue; + const rawVariantSlug = stripControlAndTrim(row.slug); + if (!isSafeCatalogSlugToken(rawVariantSlug)) continue; + const token = resolvePeerCompareToken(row); + if (!token) continue; + const tokenKey = token.toLowerCase(); + const variantKey = rawVariantSlug.toLowerCase(); + if (seenTokens.has(tokenKey) || seenVariants.has(variantKey)) continue; + seenTokens.add(tokenKey); + seenVariants.add(variantKey); + entries.push({ token, variantSlug: rawVariantSlug }); + if (entries.length >= safeLimit) break; + } + } + const slugs = entries + .map(({ token }) => encodeURIComponent(token).replace(/%40/gi, '@')) + .join(','); + const fallbacks = new URLSearchParams(); + const needsFallback = entries.some(({ token, variantSlug }) => { + const { slug, variantKey } = parseCompareToken(token); + const master = variantSlug.toLowerCase(); + if (!variantKey) return master !== slug; + return ( + variantKey === 'standard' || + slug.endsWith(`-${variantKey}`) || + `${slug}-${variantKey}` !== master + ); + }); + if (needsFallback) { + for (const { token, variantSlug } of entries) { + if (token !== variantSlug) { + fallbacks.set(`variant.${token.toLowerCase()}`, variantSlug); + } + } + } + const baseHref = `/compare?cat=${encodeURIComponent(safeCategory)}&slugs=${slugs}`; + return fallbacks.size > 0 ? `${baseHref}&${fallbacks}` : baseHref; +} + +export type ItemJsonLdImage = string | Record; + export interface ItemJsonLdVariant { variantKey: string; label: string; minPrice: number; maxPrice: number; sellerCount: number; + images?: readonly ItemJsonLdImage[]; +} + +function normalizeItemJsonLdImage( + entry: ItemJsonLdImage +): ItemJsonLdImage | null { + if (typeof entry === 'string') { + const trimmed = entry.trim(); + return trimmed.length > 0 ? trimmed : null; + } + if (!entry || typeof entry !== 'object' || Array.isArray(entry)) { + return null; + } + const hasContentUrl = Object.prototype.hasOwnProperty.call( + entry, + 'contentUrl' + ); + const hasUrl = Object.prototype.hasOwnProperty.call(entry, 'url'); + const rawContentUrl = hasContentUrl ? entry.contentUrl : undefined; + const rawUrl = hasUrl ? entry.url : undefined; + const contentUrl = + typeof rawContentUrl === 'string' ? rawContentUrl.trim() : ''; + const url = typeof rawUrl === 'string' ? rawUrl.trim() : ''; + if (!contentUrl && !url) { + return null; + } + const copy: Record = { ...entry }; + if (hasContentUrl) { + if (contentUrl) copy.contentUrl = contentUrl; + else delete copy.contentUrl; + } + if (hasUrl) { + if (url) copy.url = url; + else delete copy.url; + } + if ( + copy.creator && + typeof copy.creator === 'object' && + !Array.isArray(copy.creator) + ) { + copy.creator = { ...(copy.creator as Record) }; + } + return copy; +} + +function cloneItemJsonLdImage(entry: ItemJsonLdImage): ItemJsonLdImage { + if (typeof entry === 'string') return entry; + const copy: Record = { ...entry }; + if ( + copy.creator && + typeof copy.creator === 'object' && + !Array.isArray(copy.creator) + ) { + copy.creator = { ...(copy.creator as Record) }; + } + return copy; +} + +function normalizeItemJsonLdImages( + entries: readonly ItemJsonLdImage[] | undefined +): ItemJsonLdImage[] { + if (!Array.isArray(entries)) return []; + const out: ItemJsonLdImage[] = []; + for (const entry of entries) { + const normalized = normalizeItemJsonLdImage(entry); + if (normalized !== null) out.push(normalized); + } + return out; } /** @@ -87,6 +545,8 @@ export interface ItemJsonLdVariant { * size, each with its own sku and AggregateOffer, and one Item-level rating * (Q4). A size with no price is left out: a variant Product needs an offer. * The variant URLs carry `?v=`, and the group URL is the canonical (Q1). + * Credited `ImageObject` entries from `deviceImageJsonLd` keep their CC BY-SA + * licence and creator fields on both `ProductGroup` and `hasVariant` (#1819). */ export function buildItemProductGroupNode(input: { itemKey: string; @@ -94,41 +554,51 @@ export function buildItemProductGroupNode(input: { brand: string; canonicalUrl: string; description: string; - images: readonly string[]; + images: readonly ItemJsonLdImage[]; variants: readonly ItemJsonLdVariant[]; aggregateRating?: Record | null; }): Record { - const images = input.images - .map((url) => (typeof url === 'string' ? url.trim() : '')) - .filter((url) => url.length > 0); - const hasVariant = input.variants + const images = normalizeItemJsonLdImages(input.images); + const pricedVariants = input.variants .filter((v) => Number.isFinite(v.minPrice) && v.minPrice > 0) - .map((v) => { - const rawSellers = Number(v.sellerCount); - const offerCount = Number.isFinite(rawSellers) - ? Math.max(1, Math.floor(rawSellers)) - : 1; - const highPrice = - Number.isFinite(v.maxPrice) && v.maxPrice > v.minPrice - ? v.maxPrice - : v.minPrice; - return { - '@type': 'Product', - name: `${input.name} ${v.label}`, - sku: `${input.itemKey}:${v.variantKey}`, - inProductGroupWithID: input.itemKey, - size: v.label, - url: `${input.canonicalUrl}?v=${encodeURIComponent(v.variantKey)}`, - ...(images.length > 0 ? { image: [...images] } : {}), - offers: { - '@type': 'AggregateOffer', - priceCurrency: 'VND', - lowPrice: v.minPrice, - highPrice, - offerCount, - }, - }; - }); + .map((v) => ({ + variant: v, + variantImages: normalizeItemJsonLdImages(v.images), + })); + const fallbackGroupImages = + pricedVariants.find((entry) => entry.variantImages.length > 0) + ?.variantImages ?? []; + const groupImages = images.length > 0 ? images : fallbackGroupImages; + const hasVariant = pricedVariants.map(({ variant: v, variantImages }) => { + const rawSellers = Number(v.sellerCount); + const offerCount = Number.isFinite(rawSellers) + ? Math.max(1, Math.floor(rawSellers)) + : 1; + const highPrice = + Number.isFinite(v.maxPrice) && v.maxPrice > v.minPrice + ? v.maxPrice + : v.minPrice; + const effectiveImages = + variantImages.length > 0 ? variantImages : groupImages; + return { + '@type': 'Product', + name: `${input.name} ${v.label}`, + sku: `${input.itemKey}:${v.variantKey}`, + inProductGroupWithID: input.itemKey, + size: v.label, + url: `${input.canonicalUrl}?v=${encodeURIComponent(v.variantKey)}`, + ...(effectiveImages.length > 0 + ? { image: effectiveImages.map(cloneItemJsonLdImage) } + : {}), + offers: { + '@type': 'AggregateOffer', + priceCurrency: 'VND', + lowPrice: v.minPrice, + highPrice, + offerCount, + }, + }; + }); return { '@type': 'ProductGroup', name: input.name, @@ -136,7 +606,9 @@ export function buildItemProductGroupNode(input: { productGroupID: input.itemKey, url: input.canonicalUrl, description: input.description, - ...(images.length > 0 ? { image: [...images] } : {}), + ...(groupImages.length > 0 + ? { image: groupImages.map(cloneItemJsonLdImage) } + : {}), variesBy: ['https://schema.org/size'], ...(hasVariant.length > 0 ? { hasVariant } : {}), ...(input.aggregateRating From b377aa9dcfc846b72514ebf1f8dff904f34e2fa6 Mon Sep 17 00:00:00 2001 From: JOY <5027251+JOY@users.noreply.github.com> Date: Sun, 22 Nov 2026 08:44:57 +0700 Subject: [PATCH 11/24] fix(web): sync ?brand= sidebar filter, guard 1v1 links, and localize catalog (#1835) * fix(web): sync ?brand= sidebar filter, guard 1v1 links, and localize catalog UI/exports Seed sidebar active brands from ?brand= fallback and clear legacy brand param on toggle, guard category and toolbar /compare/ 1v1 links with isParseableVersusPair and Object.hasOwn lookups, export canonical catalogRowSlug(p) in toolbar CSV/JSON exports, and localize price chips, slider unit, and Markdown headers across vi/en/cs. Refs #1828. --- apps/web/locales/cs.json | 16 +- apps/web/locales/en.json | 16 +- apps/web/locales/vi.json | 16 +- .../[locale]/components/list-sidebar-one.tsx | 53 +- .../web/src/app/[locale]/smartphones/page.tsx | 258 +++++++-- .../leaderboard/CategoryCatalogToolbar.tsx | 421 +++++++++++---- .../src/lib/__tests__/catalog-listing.test.ts | 499 +++++++++++++++++- 7 files changed, 1119 insertions(+), 160 deletions(-) diff --git a/apps/web/locales/cs.json b/apps/web/locales/cs.json index 70171981..ba8463cc 100644 --- a/apps/web/locales/cs.json +++ b/apps/web/locales/cs.json @@ -1822,6 +1822,9 @@ "chipKeyword": "Klíčové slovo: \"{query}\"", "chipBrand": "Značka: {brand}", "chipPrice": "Cena: {min} - {max}", + "chipPriceZero": "0 VND", + "chipPriceMillion": "{amount}M VND", + "chipPriceMillionPlus": "{amount}M+ VND", "searchLabel": "Zařízení", "brandLabel": "Značka", "searchBtn": "Hledat", @@ -1868,7 +1871,15 @@ "copyMdBtn": "Kopírovat katalog Markdown", "exportCsvBtn": "Exportovat CSV kategorie", "exportJsonBtn": "Exportovat JSON", - "exportMenu": "Exportovat data" + "exportMenu": "Exportovat data", + "mdTitle": "### 📂 Katalog kategorie Rate.vn: {title} ({count, plural, one {# zařízení} few {# zařízení} other {# zařízení}})", + "mdColModel": "Model", + "mdColBrand": "Značka", + "mdColUserRating": "Hodnocení kupujících (/5,0)", + "mdColExpertScore": "Hodnocení expertů (/10)", + "mdColAuthenticReviews": "Skutečné recenze", + "mdColMinPrice": "Nejnižší cena (VND)", + "mdColPriceSignal": "Cenový signál" }, "sidebar": { "filterTitle": "Filtry parametrů", @@ -1888,7 +1899,8 @@ "rangeTo": "Do:", "minPriceAria": "Minimální cena", "maxPriceAria": "Maximální cena", - "closeAria": "Zavřít filtry" + "closeAria": "Zavřít filtry", + "priceMillionUnit": "M VND" } }, "heroSearch": { diff --git a/apps/web/locales/en.json b/apps/web/locales/en.json index fe2c486c..75f743ab 100644 --- a/apps/web/locales/en.json +++ b/apps/web/locales/en.json @@ -1822,6 +1822,9 @@ "chipKeyword": "Keyword: \"{query}\"", "chipBrand": "Brand: {brand}", "chipPrice": "Price: {min} - {max}", + "chipPriceZero": "0 VND", + "chipPriceMillion": "{amount}M VND", + "chipPriceMillionPlus": "{amount}M+ VND", "searchLabel": "Device", "brandLabel": "Brand", "searchBtn": "Search", @@ -1868,7 +1871,15 @@ "copyMdBtn": "Copy catalog Markdown", "exportCsvBtn": "Export category CSV", "exportJsonBtn": "Export JSON", - "exportMenu": "Export data" + "exportMenu": "Export data", + "mdTitle": "### 📂 Rate.vn category catalog: {title} ({count, plural, one {# device} other {# devices}})", + "mdColModel": "Model", + "mdColBrand": "Brand", + "mdColUserRating": "User rating (/5.0)", + "mdColExpertScore": "Expert score (/10)", + "mdColAuthenticReviews": "Authentic reviews", + "mdColMinPrice": "Min price (VND)", + "mdColPriceSignal": "Price signal" }, "sidebar": { "filterTitle": "Spec filters", @@ -1888,7 +1899,8 @@ "rangeTo": "To:", "minPriceAria": "Minimum price", "maxPriceAria": "Maximum price", - "closeAria": "Close filters" + "closeAria": "Close filters", + "priceMillionUnit": "M VND" } }, "heroSearch": { diff --git a/apps/web/locales/vi.json b/apps/web/locales/vi.json index c58f9d06..3a96b56e 100644 --- a/apps/web/locales/vi.json +++ b/apps/web/locales/vi.json @@ -1822,6 +1822,9 @@ "chipKeyword": "Từ khóa: \"{query}\"", "chipBrand": "Hãng: {brand}", "chipPrice": "Giá: {min} - {max}", + "chipPriceZero": "0 ₫", + "chipPriceMillion": "{amount}tr", + "chipPriceMillionPlus": "{amount}tr+", "searchLabel": "Tìm máy", "brandLabel": "Hãng", "searchBtn": "Tìm kiếm", @@ -1868,7 +1871,15 @@ "copyMdBtn": "Sao chép danh mục Markdown", "exportCsvBtn": "Xuất CSV ngành hàng", "exportJsonBtn": "Xuất JSON", - "exportMenu": "Xuất dữ liệu" + "exportMenu": "Xuất dữ liệu", + "mdTitle": "### 📂 Danh mục ngành hàng Rate.vn: {title} ({count} thiết bị)", + "mdColModel": "Mẫu máy", + "mdColBrand": "Hãng", + "mdColUserRating": "Điểm người mua (/5.0)", + "mdColExpertScore": "Điểm chuyên gia (/10)", + "mdColAuthenticReviews": "Đánh giá thật", + "mdColMinPrice": "Giá thấp nhất (VNĐ)", + "mdColPriceSignal": "Tín hiệu giá" }, "sidebar": { "filterTitle": "Bộ lọc thông số", @@ -1888,7 +1899,8 @@ "rangeTo": "Đến:", "minPriceAria": "Giá tối thiểu", "maxPriceAria": "Giá tối đa", - "closeAria": "Đóng bộ lọc" + "closeAria": "Đóng bộ lọc", + "priceMillionUnit": "tr ₫" } }, "heroSearch": { diff --git a/apps/web/src/app/[locale]/components/list-sidebar-one.tsx b/apps/web/src/app/[locale]/components/list-sidebar-one.tsx index d0c1bd77..94341b90 100644 --- a/apps/web/src/app/[locale]/components/list-sidebar-one.tsx +++ b/apps/web/src/app/[locale]/components/list-sidebar-one.tsx @@ -76,6 +76,47 @@ export function sanitizeSidebarBrands(csvOrValue: unknown): string[] { return out; } +export function resolveSidebarActiveBrands( + brandsParam: unknown, + legacyBrandParam?: unknown +): string[] { + const multi = sanitizeSidebarBrands(brandsParam); + if (multi.length > 0) return multi; + if (typeof legacyBrandParam !== 'string') return []; + const single = legacyBrandParam + .replace(/\p{Cc}+/gu, '') + .trim() + .slice(0, 64); + if (!single || single.includes(',')) return []; + return [single]; +} + +export function applySidebarBrandToggle( + params: URLSearchParams, + brand: string +): void { + const cleanVal = brand + .replace(/\p{Cc}+/gu, '') + .trim() + .slice(0, 64); + if (!cleanVal || cleanVal.includes(',')) return; + const existing = resolveSidebarActiveBrands( + params.get('brands'), + params.get('brand') + ); + params.delete('brand'); + const lower = cleanVal.toLowerCase(); + const has = existing.some((x) => x.toLowerCase() === lower); + const next = has + ? existing.filter((x) => x.toLowerCase() !== lower) + : [...existing, cleanVal]; + if (next.length > 0) { + params.set('brands', next.join(',')); + } else { + params.delete('brands'); + } +} + export function sanitizeSidebarPriceMillion( rawPriceVnd: unknown, sliderMin: number, @@ -143,7 +184,10 @@ export default function ListSidebarOne({ // Parse active filter state from URL query params const currentQ = sanitizeSidebarKeyword(searchParams?.get('q')); const currentMinRating = sanitizeSidebarRating(searchParams?.get('rating')); - const currentBrands = sanitizeSidebarBrands(searchParams?.get('brands')); + const currentBrands = resolveSidebarActiveBrands( + searchParams?.get('brands'), + searchParams?.get('brand') + ); const currentRams = sanitizeSidebarPositiveInts( searchParams?.get('ram'), 1024 @@ -276,7 +320,12 @@ export default function ListSidebarOne({ .trim() .slice(0, 64); if (!cleanVal) return; + if (paramKey === 'brands' && cleanVal.includes(',')) return; updateUrlParams((params) => { + if (paramKey === 'brands') { + applySidebarBrandToggle(params, cleanVal); + return; + } const existing = sanitizeSidebarBrands(params.get(paramKey)); const lower = cleanVal.toLowerCase(); const has = existing.some((x) => x.toLowerCase() === lower); @@ -642,7 +691,7 @@ export default function ListSidebarOne({ max={sliderMax} step={schema.priceRangeMillion.step} value={priceRange} - unitSuffix="tr ₫" + unitSuffix={t('sidebar.priceMillionUnit')} onChange={setPriceRange} onChangeComplete={handlePriceSliderComplete} /> diff --git a/apps/web/src/app/[locale]/smartphones/page.tsx b/apps/web/src/app/[locale]/smartphones/page.tsx index 7c3c6d7c..4d708293 100644 --- a/apps/web/src/app/[locale]/smartphones/page.tsx +++ b/apps/web/src/app/[locale]/smartphones/page.tsx @@ -1,6 +1,7 @@ import { Suspense } from 'react'; import { MAX_VALID_VND_PRICE, + isCanonicalCategorySlug, resolveCompareEffectiveUserRating, } from '@/lib/rate-domain'; import type { Metadata } from 'next'; @@ -64,7 +65,11 @@ import { formatPlainNumber, formatPriceVnd, } from '@/lib/locale-format'; -import { versusHref } from '@/lib/item-compare'; +import { + canonicalVersusOrder, + isParseableVersusPair, + versusHref, +} from '@/lib/item-compare'; import { withoutSpecSheet } from '@/lib/hero-products'; // Reads searchParams, so it renders per request. The [locale] layout's @@ -143,6 +148,159 @@ export function formatCatalogRadarPriceVnd( return safe != null ? formatPriceVnd(safe, locale) : '-'; } +const SAFE_PAGE_VERSUS_SLUG_RE = /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,119}$/; +const UNSAFE_PAGE_VERSUS_SLUG_KEYS = new Set([ + '0', + 'null', + 'undefined', + 'nan', + 'none', + 'na', + 'false', + 'true', + '__proto__', + 'constructor', + 'prototype', + 'tostring', + 'tolocalestring', + 'valueof', + 'hasownproperty', + 'isprototypeof', + 'propertyisenumerable', + ...Object.getOwnPropertyNames(Object.prototype).map((k) => k.toLowerCase()), +]); + +function isSafePageVersusSlug(slug: unknown): slug is string { + return ( + typeof slug === 'string' && + SAFE_PAGE_VERSUS_SLUG_RE.test(slug) && + !slug.includes('..') && + !slug.endsWith('.') && + !(slug in Object.prototype) && + !UNSAFE_PAGE_VERSUS_SLUG_KEYS.has(slug.toLowerCase()) + ); +} + +export function lookupCatalogPageOwnSlug( + map: Readonly> | null | undefined, + slug: string +): string | undefined { + if ( + !map || + typeof map !== 'object' || + Array.isArray(map) || + typeof slug !== 'string' || + !slug || + !Object.hasOwn(map, slug) + ) { + return undefined; + } + const val = map[slug]; + return typeof val === 'string' && val ? val : undefined; +} + +function canBeCatalogPageVersusSide( + slug: string, + itemSlugBySlug?: Readonly> | null +): boolean { + if (typeof slug !== 'string' || !slug) return false; + const rawItem = lookupCatalogPageOwnSlug(itemSlugBySlug, slug); + if (rawItem?.toLowerCase().includes('-vs-')) return false; + const side = + isSafePageVersusSlug(rawItem) && !rawItem.toLowerCase().includes('-vs-') + ? rawItem + : slug; + return isSafePageVersusSlug(side) && !side.toLowerCase().includes('-vs-'); +} + +export function isValidCatalogPageVersusPair( + slugA: string, + slugB: string, + itemSlugBySlug?: Readonly> | null +): boolean { + if ( + typeof slugA !== 'string' || + typeof slugB !== 'string' || + !slugA || + !slugB || + slugA.toLowerCase() === slugB.toLowerCase() + ) { + return false; + } + if ( + !canBeCatalogPageVersusSide(slugA, itemSlugBySlug) || + !canBeCatalogPageVersusSide(slugB, itemSlugBySlug) + ) { + return false; + } + const rawItemA = lookupCatalogPageOwnSlug(itemSlugBySlug, slugA); + const rawItemB = lookupCatalogPageOwnSlug(itemSlugBySlug, slugB); + if ( + rawItemA && + rawItemB && + rawItemA.toLowerCase() === rawItemB.toLowerCase() + ) { + return false; + } + const sideA = + isSafePageVersusSlug(rawItemA) && !rawItemA.toLowerCase().includes('-vs-') + ? rawItemA + : slugA; + const sideB = + isSafePageVersusSlug(rawItemB) && !rawItemB.toLowerCase().includes('-vs-') + ? rawItemB + : slugB; + if (sideA.toLowerCase() === sideB.toLowerCase()) return false; + const [first, second] = canonicalVersusOrder(sideA, sideB); + return isParseableVersusPair(first, second); +} + +export function pickCatalogVersusPair( + rows: readonly T[], + itemSlugBySlug?: Readonly> | null +): [T, T] | null { + for (let i = 0; i < rows.length; i++) { + const a = rows[i]; + if (!a || !canBeCatalogPageVersusSide(a.slug, itemSlugBySlug)) continue; + for (let j = i + 1; j < rows.length; j++) { + const b = rows[j]; + if (!b || !canBeCatalogPageVersusSide(b.slug, itemSlugBySlug)) continue; + if (isValidCatalogPageVersusPair(a.slug, b.slug, itemSlugBySlug)) { + return [a, b]; + } + } + } + return null; +} + +export type CatalogPriceChipKey = + | 'page.chipPrice' + | 'page.chipPriceZero' + | 'page.chipPriceMillion' + | 'page.chipPriceMillionPlus'; + +export function formatCatalogPriceChip( + minPrice: number, + maxPrice: number, + maxSliderMillion: number, + t: ( + key: CatalogPriceChipKey, + values?: Record + ) => string +): string { + const safeMin = Number.isFinite(minPrice) && minPrice > 0 ? minPrice : 0; + const safeMax = Number.isFinite(maxPrice) && maxPrice > 0 ? maxPrice : 0; + const minLabel = + safeMin > 0 + ? t('page.chipPriceMillion', { amount: Math.round(safeMin / 1_000_000) }) + : t('page.chipPriceZero'); + const maxLabel = + safeMax > 0 + ? t('page.chipPriceMillion', { amount: Math.round(safeMax / 1_000_000) }) + : t('page.chipPriceMillionPlus', { amount: maxSliderMillion }); + return t('page.chipPrice', { min: minLabel, max: maxLabel }); +} + export function buildCatalogPageItemSlugMap( rows: readonly { slug: string; item_slug?: string }[] ): Record { @@ -150,8 +308,17 @@ export function buildCatalogPageItemSlugMap( for (const row of rows) { if (!row || typeof row.slug !== 'string' || !row.slug) continue; const itemSlug = catalogRowSlug(row); - if (itemSlug && itemSlug !== row.slug) { - map[row.slug] = itemSlug; + if (itemSlug && itemSlug !== row.slug && !Object.hasOwn(map, row.slug)) { + if (row.slug === '__proto__') { + Object.defineProperty(map, row.slug, { + value: itemSlug, + enumerable: true, + configurable: true, + writable: true, + }); + } else { + map[row.slug] = itemSlug; + } } } return map; @@ -372,18 +539,9 @@ export async function CategoryCatalogContent({ const radarIndex = buildRadarIndex(categoryDealRadar); const itemSlugBySlug = buildCatalogPageItemSlugMap(products); const quickCompareSlugs = buildCatalogQuickCompareSlugs(products); - const resolveSideSlug = (slug: string): string => { - const mapped = itemSlugBySlug[slug]; - return mapped && !mapped.includes('-vs-') ? mapped : slug; - }; - const versusPairA = products[0]; - const versusPairB = versusPairA - ? products.find( - (p) => - p.slug !== versusPairA.slug && - resolveSideSlug(p.slug) !== resolveSideSlug(versusPairA.slug) - ) - : undefined; + const versusPair = pickCatalogVersusPair(products, itemSlugBySlug); + const versusPairA = versusPair?.[0]; + const versusPairB = versusPair?.[1]; const validGenuineDeals = Array.isArray(categoryDealRadar?.genuineDeals) ? categoryDealRadar.genuineDeals.filter( @@ -446,14 +604,14 @@ export async function CategoryCatalogContent({ } if (minPrice > 0 || maxPrice > 0) { - const minM = minPrice > 0 ? `${Math.round(minPrice / 1_000_000)}tr` : '0đ'; - const maxM = - maxPrice > 0 - ? `${Math.round(maxPrice / 1_000_000)}tr` - : `${schema.priceRangeMillion.max}tr+`; activeChips.push({ id: 'price', - label: t('page.chipPrice', { min: minM, max: maxM }), + label: formatCatalogPriceChip( + minPrice, + maxPrice, + schema.priceRangeMillion.max, + t + ), removeHref: buildFilterHref(basePath, rawSearch, { minPrice: null, maxPrice: null, @@ -817,31 +975,35 @@ export async function CategoryCatalogContent({ : t('page.quickCompare2')} )} - {versusPairA && - versusPairB && - products[0] && - products[1] && ( - - - )} + {versusPairA && versusPairB && ( + + + )} k.toLowerCase()), +]); + +function isSafeCatalogVersusSlug(slug: unknown): slug is string { + return ( + typeof slug === 'string' && + SAFE_CATALOG_VERSUS_SLUG_RE.test(slug) && + !slug.includes('..') && + !slug.endsWith('.') && + !(slug in Object.prototype) && + !UNSAFE_CATALOG_VERSUS_SLUG_KEYS.has(slug.toLowerCase()) + ); +} + +export function lookupCatalogOwnSlug( + map: Readonly> | null | undefined, + slug: string +): string | undefined { + if ( + !map || + typeof map !== 'object' || + Array.isArray(map) || + typeof slug !== 'string' || + !slug || + !Object.hasOwn(map, slug) + ) { + return undefined; + } + const val = map[slug]; + return typeof val === 'string' && val ? val : undefined; +} + +function setCatalogOwnSlug( + map: Record, + slug: string, + value: string +): void { + if (slug === '__proto__') { + Object.defineProperty(map, slug, { + value, + enumerable: true, + configurable: true, + writable: true, + }); + } else { + map[slug] = value; + } +} + +export function resolveCatalogVersusSideSlug( + slug: string, + itemSlugBySlug?: Readonly> | null +): string { + if (typeof slug !== 'string' || !slug) return ''; + const mapped = lookupCatalogOwnSlug(itemSlugBySlug, slug); + return isSafeCatalogVersusSlug(mapped) && + !mapped.toLowerCase().includes('-vs-') + ? mapped + : slug; +} + +function canBeCatalogVersusSide( + slug: string, + itemSlugBySlug?: Readonly> | null +): boolean { + if (typeof slug !== 'string' || !slug) return false; + if ( + lookupCatalogOwnSlug(itemSlugBySlug, slug)?.toLowerCase().includes('-vs-') + ) { + return false; + } + const side = resolveCatalogVersusSideSlug(slug, itemSlugBySlug); + return isSafeCatalogVersusSlug(side) && !side.toLowerCase().includes('-vs-'); +} + +export function isValidCatalogVersusPair( + slugA: string, + slugB: string, + itemSlugBySlug?: Readonly> | null +): boolean { + if ( + typeof slugA !== 'string' || + typeof slugB !== 'string' || + !slugA || + !slugB || + slugA.toLowerCase() === slugB.toLowerCase() + ) { + return false; + } + if ( + !canBeCatalogVersusSide(slugA, itemSlugBySlug) || + !canBeCatalogVersusSide(slugB, itemSlugBySlug) + ) { + return false; + } + const rawItemA = lookupCatalogOwnSlug(itemSlugBySlug, slugA); + const rawItemB = lookupCatalogOwnSlug(itemSlugBySlug, slugB); + if ( + rawItemA && + rawItemB && + rawItemA.toLowerCase() === rawItemB.toLowerCase() + ) { + return false; + } + const sideA = resolveCatalogVersusSideSlug(slugA, itemSlugBySlug); + const sideB = resolveCatalogVersusSideSlug(slugB, itemSlugBySlug); + if (sideA.toLowerCase() === sideB.toLowerCase()) return false; + const [first, second] = canonicalVersusOrder(sideA, sideB); + return isParseableVersusPair(first, second); +} export function sanitizeCatalogToolbarText( value: unknown, @@ -85,16 +222,26 @@ export function sanitizeCatalogDownloadSlug( return slug || fallback; } +export function resolveCatalogExportSlug( + p: { slug: string; item_slug?: string }, + itemSlugBySlug?: Readonly> | null +): string { + const mapped = lookupCatalogOwnSlug(itemSlugBySlug, p.slug); + const raw = mapped || catalogRowSlug(p); + return sanitizeCatalogToolbarText(raw, '', 120); +} + export function buildCatalogJsonProductEntry( - p: MasterProduct, + p: MasterProduct & { item_slug?: string }, rank: number, - signal: RadarSignal | null | undefined + signal: RadarSignal | null | undefined, + itemSlugBySlug?: Readonly> | null ) { const rawUser = resolveCompareEffectiveUserRating(p); const expertScore10 = resolveCompareExpertRating10(p); return { rank, - slug: sanitizeCatalogToolbarText(p.slug, '', 120), + slug: resolveCatalogExportSlug(p, itemSlugBySlug), name: sanitizeCatalogToolbarText(p.name, 'Unknown'), brand: sanitizeCatalogToolbarText(p.brand, '', 80), userRating: rawUser > 0 ? rawUser : null, @@ -110,18 +257,22 @@ export function buildCatalogItemSlugMap( itemSlugBySlug?: Readonly> | null ): Record { const map: Record = {}; - if (itemSlugBySlug) { + if ( + itemSlugBySlug && + typeof itemSlugBySlug === 'object' && + !Array.isArray(itemSlugBySlug) + ) { for (const [slug, itemSlug] of Object.entries(itemSlugBySlug)) { if (slug && typeof itemSlug === 'string' && itemSlug) { - map[slug] = itemSlug; + setCatalogOwnSlug(map, slug, itemSlug); } } } for (const p of products) { if (!p || typeof p.slug !== 'string' || !p.slug) continue; const rowSlug = catalogRowSlug(p); - if (rowSlug && rowSlug !== p.slug && !map[p.slug]) { - map[p.slug] = rowSlug; + if (rowSlug && rowSlug !== p.slug && !Object.hasOwn(map, p.slug)) { + setCatalogOwnSlug(map, p.slug, rowSlug); } } return map; @@ -131,7 +282,8 @@ export function buildCatalogCsvRow( p: ItemCatalogRow, rank: number, safeCategorySlug: string, - radar: RadarSignal | null | undefined + radar: RadarSignal | null | undefined, + itemSlugBySlug?: Readonly> | null ): string[] { const rawUser = resolveCompareEffectiveUserRating(p); const userRating = rawUser > 0 ? rawUser : null; @@ -140,7 +292,7 @@ export function buildCatalogCsvRow( const maxPrice = sanitizeCatalogPriceVnd(p.max_price); return [ String(rank), - sanitizeCatalogToolbarText(p.slug, '', 120), + resolveCatalogExportSlug(p, itemSlugBySlug), sanitizeCatalogToolbarText(p.name, 'Unknown'), sanitizeCatalogToolbarText(p.brand, '', 80), safeCategorySlug, @@ -156,6 +308,86 @@ export function buildCatalogCsvRow( ]; } +export type CatalogMarkdownKey = + | 'toolbar.mdTitle' + | 'toolbar.mdColModel' + | 'toolbar.mdColBrand' + | 'toolbar.mdColUserRating' + | 'toolbar.mdColExpertScore' + | 'toolbar.mdColAuthenticReviews' + | 'toolbar.mdColMinPrice' + | 'toolbar.mdColPriceSignal' + | 'toolbar.unratedText'; + +export function buildCatalogMarkdown( + products: readonly ItemCatalogRow[], + categoryTitle: string, + radarFor: (slug: string) => RadarSignal | null | undefined, + locale: string, + t: ( + key: CatalogMarkdownKey, + values?: Record + ) => string +): string { + const safeTitle = sanitizeCatalogToolbarText( + categoryTitle, + 'smartphones', + 120 + ); + const headers = [ + '#', + sanitizeCatalogToolbarText(t('toolbar.mdColModel'), 'Model'), + sanitizeCatalogToolbarText(t('toolbar.mdColBrand'), 'Brand'), + sanitizeCatalogToolbarText( + t('toolbar.mdColUserRating'), + 'User rating (/5.0)' + ), + sanitizeCatalogToolbarText( + t('toolbar.mdColExpertScore'), + 'Expert score (/10)' + ), + sanitizeCatalogToolbarText( + t('toolbar.mdColAuthenticReviews'), + 'Authentic reviews' + ), + sanitizeCatalogToolbarText(t('toolbar.mdColMinPrice'), 'Min price (VND)'), + sanitizeCatalogToolbarText(t('toolbar.mdColPriceSignal'), 'Price signal'), + ]; + const sep = headers.map(() => '---'); + const rows = products.map((p, idx) => { + const rawUser = resolveCompareEffectiveUserRating(p); + const userRating = rawUser > 0 ? rawUser : null; + const expertScore10 = resolveCompareExpertRating10(p); + const radar = radarFor(p.slug); + const minPrice = sanitizeCatalogPriceVnd(p.min_price); + const authReviews = sanitizeCatalogCount( + p.authentic_review_count || p.total_reviews || 0 + ); + return [ + String(idx + 1), + sanitizeCatalogToolbarText(p.name, 'Unknown'), + sanitizeCatalogToolbarText(p.brand, '-', 80), + userRating != null + ? `${formatDecimal(displayedScore5(userRating), locale, 1)}/${formatDecimal(5, locale, 1)}` + : t('toolbar.unratedText'), + expertScore10 != null + ? `${formatDecimal(expertScore10, locale, 1)}/10` + : t('toolbar.unratedText'), + formatNumber(authReviews, locale), + minPrice > 0 ? formatPriceVnd(minPrice, locale) : '-', + radarMarkdownCell(radar, locale), + ]; + }); + + return [ + t('toolbar.mdTitle', { title: safeTitle, count: products.length }), + '', + `| ${headers.join(' | ')} |`, + `| ${sep.join(' | ')} |`, + ...rows.map((r) => `| ${r.join(' | ')} |`), + ].join('\n'); +} + interface CategoryCatalogToolbarProps { readonly categorySlug: string; readonly categoryTitle: string; @@ -196,80 +428,54 @@ export default function CategoryCatalogToolbar({ [products, itemSlugBySlug] ); - const sideSlugFor = (slug: string): string => { - const mapped = resolvedItemSlugBySlug[slug]; - return mapped && !mapped.includes('-vs-') ? mapped : slug; - }; + const validCandidates = useMemo( + () => + products.filter((p) => + canBeCatalogVersusSide(p.slug, resolvedItemSlugBySlug) + ), + [products, resolvedItemSlugBySlug] + ); + + const selectableProductsA = useMemo( + () => + validCandidates.filter((pA) => + validCandidates.some((pB) => + isValidCatalogVersusPair(pA.slug, pB.slug, resolvedItemSlugBySlug) + ) + ), + [validCandidates, resolvedItemSlugBySlug] + ); const effectiveSlugA = useMemo(() => { - if (slugA && products.some((p) => p.slug === slugA)) return slugA; - return products[0]?.slug ?? ''; - }, [slugA, products]); + if (slugA && selectableProductsA.some((p) => p.slug === slugA)) { + return slugA; + } + return selectableProductsA[0]?.slug ?? ''; + }, [slugA, selectableProductsA]); - const activeSideA = sideSlugFor(effectiveSlugA); + const selectableProductsB = useMemo(() => { + if (!effectiveSlugA) return []; + return validCandidates.filter((p) => + isValidCatalogVersusPair(effectiveSlugA, p.slug, resolvedItemSlugBySlug) + ); + }, [effectiveSlugA, validCandidates, resolvedItemSlugBySlug]); const effectiveSlugB = useMemo(() => { - if ( - slugB && - slugB !== effectiveSlugA && - sideSlugFor(slugB) !== activeSideA && - products.some((p) => p.slug === slugB) - ) { + if (slugB && selectableProductsB.some((p) => p.slug === slugB)) { return slugB; } - return ( - products.find( - (p) => p.slug !== effectiveSlugA && sideSlugFor(p.slug) !== activeSideA - )?.slug ?? '' - ); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [slugB, effectiveSlugA, activeSideA, products, resolvedItemSlugBySlug]); + return selectableProductsB[0]?.slug ?? ''; + }, [slugB, selectableProductsB]); const handleCopyCatalogMarkdown = async () => { if (typeof navigator === 'undefined' || products.length === 0) return; - const headers = [ - '#', - 'Model', - 'Brand', - 'User Rating (/5.0)', - 'Expert Score (/10)', - 'Authentic Reviews', - 'Min Price (VND)', - 'Price signal', - ]; - const sep = headers.map(() => '---'); - const rows = products.map((p, idx) => { - const rawUser = resolveCompareEffectiveUserRating(p); - const userRating = rawUser > 0 ? rawUser : null; - const expertScore10 = resolveCompareExpertRating10(p); - const radar = radarFor(p.slug); - const minPrice = sanitizeCatalogPriceVnd(p.min_price); - const authReviews = sanitizeCatalogCount( - p.authentic_review_count || p.total_reviews || 0 - ); - return [ - String(idx + 1), - sanitizeCatalogToolbarText(p.name, 'Unknown'), - sanitizeCatalogToolbarText(p.brand, '-', 80), - userRating != null - ? `${formatScoreFixed(displayedScore5(userRating), 1)}/5.0` - : t('toolbar.unratedText'), - expertScore10 != null - ? `${formatScoreFixed(expertScore10, 1)}/10` - : t('toolbar.unratedText'), - String(authReviews), - minPrice > 0 ? `${formatNumber(minPrice, locale)} ₫` : '-', - radarMarkdownCell(radar, locale), - ]; - }); - - const md = [ - `### 📂 Rate.vn category catalog: ${safeCategoryTitle} (${products.length} devices)`, - '', - `| ${headers.join(' | ')} |`, - `| ${sep.join(' | ')} |`, - ...rows.map((r) => `| ${r.join(' | ')} |`), - ].join('\n'); + const md = buildCatalogMarkdown( + products, + safeCategoryTitle, + radarFor, + locale, + t + ); try { await navigator.clipboard.writeText(md); @@ -297,7 +503,13 @@ export default function CategoryCatalogToolbar({ ...RADAR_CSV_HEADERS, ]; const rows = products.map((p, idx) => - buildCatalogCsvRow(p, idx + 1, safeCategorySlug, radarFor(p.slug)) + buildCatalogCsvRow( + p, + idx + 1, + safeCategorySlug, + radarFor(p.slug), + resolvedItemSlugBySlug + ) ); const csvContent = @@ -327,7 +539,12 @@ export default function CategoryCatalogToolbar({ exportedAt: new Date().toISOString(), productCount: products.length, products: products.map((p, idx) => - buildCatalogJsonProductEntry(p, idx + 1, radarFor(p.slug)) + buildCatalogJsonProductEntry( + p, + idx + 1, + radarFor(p.slug), + resolvedItemSlugBySlug + ) ), }; const blob = new Blob([JSON.stringify(payload, null, 2)], { @@ -348,7 +565,13 @@ export default function CategoryCatalogToolbar({ return (
- {effectiveSlugA && effectiveSlugB ? ( + {effectiveSlugA && + effectiveSlugB && + isValidCatalogVersusPair( + effectiveSlugA, + effectiveSlugB, + resolvedItemSlugBySlug + ) ? (
{t('toolbar.quick1v1Label', { title: safeCategoryTitle })} @@ -360,7 +583,7 @@ export default function CategoryCatalogToolbar({ onChange={(e) => setSlugA(e.target.value)} aria-label={t('toolbar.selectDeviceAAria')} > - {products.map((p) => ( + {selectableProductsA.map((p) => ( @@ -376,33 +599,23 @@ export default function CategoryCatalogToolbar({ onChange={(e) => setSlugB(e.target.value)} aria-label={t('toolbar.selectDeviceBAria')} > - {products - .filter( - (p) => - p.slug !== effectiveSlugA && - sideSlugFor(p.slug) !== activeSideA - ) - .map((p) => ( - - ))} + {selectableProductsB.map((p) => ( + + ))} - {effectiveSlugA && - effectiveSlugB && - sideSlugFor(effectiveSlugB) !== activeSideA && ( - - {t('toolbar.versusGoBtn')} - + + {t('toolbar.versusGoBtn')} +
) : (
diff --git a/apps/web/src/lib/__tests__/catalog-listing.test.ts b/apps/web/src/lib/__tests__/catalog-listing.test.ts index ccf4e56f..a26e16ad 100644 --- a/apps/web/src/lib/__tests__/catalog-listing.test.ts +++ b/apps/web/src/lib/__tests__/catalog-listing.test.ts @@ -1,5 +1,24 @@ import assert from 'node:assert/strict'; +import Module from 'node:module'; +import React from 'react'; +import { renderToStaticMarkup } from 'react-dom/server'; +import { NextIntlClientProvider, createTranslator } from 'next-intl'; +import { + AppRouterContext, + type AppRouterInstance, +} from 'next/dist/shared/lib/app-router-context.shared-runtime'; +import { + PathnameContext, + SearchParamsContext, +} from 'next/dist/shared/lib/hooks-client-context.shared-runtime'; +import csMessages from '../../../locales/cs.json'; +import enMessages from '../../../locales/en.json'; +import viMessages from '../../../locales/vi.json'; import { isPlainListing } from '../catalog-listing'; +import { pickClientMessages } from '../client-messages'; +import { EMPTY_RADAR_SIGNAL } from '../deal-radar'; +import type { ItemCatalogRow } from '../item-directory'; +import { NBSP } from '../locale-format'; console.warn('🧪 Catalogue plain-listing suite'); @@ -71,4 +90,482 @@ assert.equal( false ); -console.warn('✅ Catalogue plain-listing suite passed'); +async function runCatalogSyncGuardsAndI18nSuite() { + const extMap = ( + Module as unknown as { + _extensions?: Record void>; + } + )._extensions; + if (extMap && !extMap['.css']) { + extMap['.css'] = (mod) => { + mod.exports = { bar: 'bar' }; + }; + } + + const { + default: ListSidebarOne, + applySidebarBrandToggle, + resolveSidebarActiveBrands, + } = await import('../../app/[locale]/components/list-sidebar-one'); + const { + buildCatalogPageItemSlugMap, + formatCatalogPriceChip, + isValidCatalogPageVersusPair, + lookupCatalogPageOwnSlug, + pickCatalogVersusPair, + } = await import('../../app/[locale]/smartphones/page'); + const { + default: CategoryCatalogToolbar, + buildCatalogCsvRow, + buildCatalogItemSlugMap, + buildCatalogJsonProductEntry, + buildCatalogMarkdown, + isValidCatalogVersusPair, + lookupCatalogOwnSlug, + resolveCatalogExportSlug, + } = await import('../../components/leaderboard/CategoryCatalogToolbar'); + + // 1. Sidebar ?brand= fallback seeding and legacy brand cleanup (#1828). + assert.deepEqual(resolveSidebarActiveBrands(null, 'Apple'), ['Apple']); + assert.deepEqual(resolveSidebarActiveBrands('', ' Apple\u0000 '), [ + 'Apple', + ]); + assert.deepEqual(resolveSidebarActiveBrands(',', 'Samsung'), ['Samsung']); + assert.deepEqual(resolveSidebarActiveBrands('Samsung,Xiaomi', 'Apple'), [ + 'Samsung', + 'Xiaomi', + ]); + assert.deepEqual(resolveSidebarActiveBrands(null, 'Apple,Samsung'), []); + assert.deepEqual(resolveSidebarActiveBrands(null, null), []); + + // Adding a second brand when URL started with ?brand=Apple keeps Apple and deletes legacy brand. + const addBrandParams = new URLSearchParams( + 'brand=Apple&page=2&sort=price_asc' + ); + applySidebarBrandToggle(addBrandParams, 'Samsung'); + assert.equal(addBrandParams.get('brand'), null); + assert.equal(addBrandParams.get('brands'), 'Apple,Samsung'); + assert.equal(addBrandParams.get('sort'), 'price_asc'); + + // Unchecking Apple when URL started with ?brand=Apple clears both brand and brands. + const removeSingleBrandParams = new URLSearchParams('brand=Apple'); + applySidebarBrandToggle(removeSingleBrandParams, 'apple'); + assert.equal(removeSingleBrandParams.toString(), ''); + + // Unchecking the active brands entry when stale ?brand=Apple also existed never resurrects Apple. + const noResurrectParams = new URLSearchParams('brands=Samsung&brand=Apple'); + applySidebarBrandToggle(noResurrectParams, 'Samsung'); + assert.equal(noResurrectParams.get('brand'), null); + assert.equal(noResurrectParams.get('brands'), null); + assert.equal(noResurrectParams.toString(), ''); + + const stubRouter: AppRouterInstance = { + bfcacheId: 'test', + back: () => {}, + forward: () => {}, + refresh: () => {}, + push: () => {}, + replace: () => {}, + prefetch: () => {}, + }; + const Provider = NextIntlClientProvider as unknown as React.ComponentType<{ + locale: string; + messages: Record; + timeZone: string; + children?: React.ReactNode; + }>; + + const localeCatalogs = { + vi: viMessages, + en: enMessages, + cs: csMessages, + } as const; + + for (const locale of ['vi', 'en', 'cs'] as const) { + const sidebarHtml = renderToStaticMarkup( + React.createElement( + AppRouterContext.Provider, + { value: stubRouter }, + React.createElement( + PathnameContext.Provider, + { value: '/smartphones' }, + React.createElement( + SearchParamsContext.Provider, + { value: new URLSearchParams('brand=Apple') }, + React.createElement( + Provider, + { + locale, + messages: pickClientMessages( + localeCatalogs[locale] as unknown as Record, + 'catalog' + ), + timeZone: 'Asia/Ho_Chi_Minh', + }, + React.createElement(ListSidebarOne, { + categorySlug: 'smartphones', + brandFacets: [ + { name: 'Apple', count: 12 }, + { name: 'Samsung', count: 9 }, + ], + }) + ) + ) + ) + ) + ); + assert.match( + sidebarHtml, + /]*id="brand-apple"[^>]*checked=""/, + `${locale}: ?brand=Apple renders Apple checkbox checked in ListSidebarOne` + ); + assert.doesNotMatch( + sidebarHtml, + /]*id="brand-samsung"[^>]*checked=""/, + `${locale}: ?brand=Apple leaves Samsung unchecked` + ); + if (locale === 'vi') { + assert.match(sidebarHtml, /tr ₫/); + } else { + assert.match(sidebarHtml, /M VND/); + assert.doesNotMatch( + sidebarHtml, + /tr ₫/, + `${locale}: RangeSlider must not leak Vietnamese "tr ₫"` + ); + } + } + + // 2. Prototype-safe Object.hasOwn lookups + isParseableVersusPair 1v1 guards (#1828). + const plainSlugMap: Record = { + 'iphone-16-256gb': 'iphone-16', + 'galaxy-s24-256gb': 'galaxy-s24', + }; + for (const protoKey of [ + 'toString', + 'constructor', + 'valueOf', + 'hasOwnProperty', + '__proto__', + ]) { + assert.equal(lookupCatalogPageOwnSlug(plainSlugMap, protoKey), undefined); + assert.equal(lookupCatalogOwnSlug(plainSlugMap, protoKey), undefined); + assert.equal( + isValidCatalogPageVersusPair(protoKey, 'iphone-16-256gb', plainSlugMap), + false, + `page guard rejects prototype slug ${protoKey}` + ); + assert.equal( + isValidCatalogVersusPair(protoKey, 'iphone-16-256gb', plainSlugMap), + false, + `toolbar guard rejects prototype slug ${protoKey}` + ); + } + + // Safe storage of own prototype-named keys in item slug maps. + const protoMapPage = buildCatalogPageItemSlugMap([ + { slug: 'toString', item_slug: 'safe-item-one' }, + { slug: '__proto__', item_slug: 'safe-item-two' }, + ]); + assert.equal(Object.getPrototypeOf(protoMapPage), Object.prototype); + assert.equal( + lookupCatalogPageOwnSlug(protoMapPage, 'toString'), + 'safe-item-one' + ); + assert.equal( + lookupCatalogPageOwnSlug(protoMapPage, '__proto__'), + 'safe-item-two' + ); + assert.equal(lookupCatalogPageOwnSlug(protoMapPage, 'valueOf'), undefined); + + const protoMapToolbar = buildCatalogItemSlugMap([ + { + slug: 'toString', + item_slug: 'safe-item-one', + } as unknown as ItemCatalogRow, + { + slug: '__proto__', + item_slug: 'safe-item-two', + } as unknown as ItemCatalogRow, + ]); + assert.equal(Object.getPrototypeOf(protoMapToolbar), Object.prototype); + assert.equal( + lookupCatalogOwnSlug(protoMapToolbar, 'toString'), + 'safe-item-one' + ); + assert.equal( + lookupCatalogOwnSlug(protoMapToolbar, '__proto__'), + 'safe-item-two' + ); + assert.equal(lookupCatalogOwnSlug(protoMapToolbar, 'constructor'), undefined); + + // Unparseable -vs- pairs & ending -vs pairs. + assert.equal( + isValidCatalogPageVersusPair('phone-a-vs-b', 'iphone-16', {}), + false + ); + assert.equal( + isValidCatalogVersusPair('phone-a-vs-b', 'iphone-16', {}), + false + ); + assert.equal( + isValidCatalogPageVersusPair('phone-a-256gb', 'iphone-16-256gb', { + 'phone-a-256gb': 'phone-a-vs-b', + 'iphone-16-256gb': 'iphone-16', + }), + false + ); + assert.equal( + isValidCatalogVersusPair('phone-a-256gb', 'iphone-16-256gb', { + 'phone-a-256gb': 'phone-a-vs-b', + 'iphone-16-256gb': 'iphone-16', + }), + false + ); + // Two slugs both ending in -vs fail round-trip in both orders. + assert.equal(isValidCatalogPageVersusPair('alpha-vs', 'beta-vs', {}), false); + assert.equal(isValidCatalogVersusPair('alpha-vs', 'beta-vs', {}), false); + // Single slug ending in -vs paired with normal slug round-trips in canonical reversed order. + assert.equal(isValidCatalogPageVersusPair('alpha-vs', 'zeta', {}), true); + assert.equal(isValidCatalogVersusPair('alpha-vs', 'zeta', {}), true); + + // pickCatalogVersusPair skips unparseable first row and picks valid pair. + const pickedPair = pickCatalogVersusPair( + [ + { slug: 'broken-a-vs-b', brand: 'Broken' }, + { slug: 'iphone-16-256gb', brand: 'Apple' }, + { slug: 'galaxy-s24-256gb', brand: 'Samsung' }, + ], + plainSlugMap + ); + assert.deepEqual( + pickedPair?.map((r) => r.slug), + ['iphone-16-256gb', 'galaxy-s24-256gb'] + ); + assert.equal( + pickCatalogVersusPair([{ slug: 'alpha-vs' }, { slug: 'beta-vs' }], {}), + null + ); + + // CategoryCatalogToolbar SSR skips unparseable rows and hides picker if none parseable. + const rowBrokenVs = { + id: 'r-broken', + slug: 'broken-a-vs-b', + name: 'Broken VS Phone', + brand: 'Test', + min_price: 10_000_000, + } as unknown as ItemCatalogRow; + const rowIphone = { + id: 'r-iphone', + slug: 'apple-iphone-16-256gb', + item_slug: 'apple-iphone-16', + name: 'iPhone 16', + brand: 'Apple', + min_price: 21_990_000, + max_price: 24_990_000, + rating_score: 4.8, + expert_rating: 9.1, + authentic_review_count: 1200, + merchant_count: 5, + } as unknown as ItemCatalogRow; + const rowGalaxy = { + id: 'r-galaxy', + slug: 'samsung-galaxy-s24-256gb', + item_slug: 'samsung-galaxy-s24', + name: 'Galaxy S24', + brand: 'Samsung', + min_price: 18_990_000, + max_price: 21_490_000, + rating_score: 4.6, + expert_rating: 8.8, + authentic_review_count: 85, + merchant_count: 4, + } as unknown as ItemCatalogRow; + + const skippedBrokenToolbarHtml = renderToStaticMarkup( + React.createElement( + Provider, + { + locale: 'en', + messages: pickClientMessages( + enMessages as unknown as Record, + 'catalog' + ), + timeZone: 'Asia/Ho_Chi_Minh', + }, + React.createElement(CategoryCatalogToolbar, { + categorySlug: 'smartphones', + categoryTitle: 'Smartphones', + products: [rowBrokenVs, rowIphone, rowGalaxy], + }) + ) + ); + assert.match( + skippedBrokenToolbarHtml, + /href="\/en\/compare\/apple-iphone-16-vs-samsung-galaxy-s24"|href="\/compare\/apple-iphone-16-vs-samsung-galaxy-s24"/, + 'CategoryCatalogToolbar skips unparseable first row and links valid canonical Item pair' + ); + assert.doesNotMatch(skippedBrokenToolbarHtml, /broken-a-vs-b/); + + const allUnparseableToolbarHtml = renderToStaticMarkup( + React.createElement( + Provider, + { + locale: 'en', + messages: pickClientMessages( + enMessages as unknown as Record, + 'catalog' + ), + timeZone: 'Asia/Ho_Chi_Minh', + }, + React.createElement(CategoryCatalogToolbar, { + categorySlug: 'smartphones', + categoryTitle: 'Smartphones', + products: [ + rowBrokenVs, + { ...rowBrokenVs, id: 'r-broken-2', slug: 'other-c-vs-d' }, + ], + }) + ) + ); + assert.doesNotMatch( + allUnparseableToolbarHtml, + /data-testid="catalog-1v1-go-btn"/, + 'CategoryCatalogToolbar hides 1v1 Go button when all pairs are unparseable' + ); + + // 3. Canonical Item slugs in toolbar CSV & JSON exports (#1828). + assert.equal(resolveCatalogExportSlug(rowIphone), 'apple-iphone-16'); + assert.equal( + resolveCatalogExportSlug( + { slug: 'samsung-galaxy-s24-256gb' }, + { 'samsung-galaxy-s24-256gb': 'samsung-galaxy-s24' } + ), + 'samsung-galaxy-s24' + ); + assert.equal(resolveCatalogExportSlug({ slug: 'toString' }, {}), 'toString'); + const csvRow = buildCatalogCsvRow( + rowIphone, + 1, + 'smartphones', + EMPTY_RADAR_SIGNAL + ); + assert.equal(csvRow[1], 'apple-iphone-16'); + const jsonEntry = buildCatalogJsonProductEntry( + rowIphone, + 1, + EMPTY_RADAR_SIGNAL + ); + assert.equal(jsonEntry.slug, 'apple-iphone-16'); + + // 4. Localized price chips & Markdown export headers/title across vi/en/cs (#1828). + const tVi = createTranslator({ + locale: 'vi', + messages: viMessages, + namespace: 'catalog', + }); + const tEn = createTranslator({ + locale: 'en', + messages: enMessages, + namespace: 'catalog', + }); + const tCs = createTranslator({ + locale: 'cs', + messages: csMessages, + namespace: 'catalog', + }); + + assert.equal( + formatCatalogPriceChip(0, 7_000_000, 50, tVi as never), + 'Giá: 0 ₫ - 7tr' + ); + assert.equal( + formatCatalogPriceChip(25_000_000, 0, 50, tVi as never), + 'Giá: 25tr - 50tr+' + ); + assert.equal( + formatCatalogPriceChip(0, 7_000_000, 50, tEn as never), + 'Price: 0 VND - 7M VND' + ); + assert.equal( + formatCatalogPriceChip(25_000_000, 0, 50, tEn as never), + 'Price: 25M VND - 50M+ VND' + ); + assert.equal( + formatCatalogPriceChip(0, 7_000_000, 50, tCs as never), + 'Cena: 0 VND - 7M VND' + ); + assert.equal( + formatCatalogPriceChip(25_000_000, 0, 50, tCs as never), + 'Cena: 25M VND - 50M+ VND' + ); + + const mdVi = buildCatalogMarkdown( + [rowIphone, rowGalaxy], + 'Điện thoại thông minh', + () => EMPTY_RADAR_SIGNAL, + 'vi', + tVi as never + ); + assert.match( + mdVi, + /^### 📂 Danh mục ngành hàng Rate\.vn: Điện thoại thông minh \(2 thiết bị\)/ + ); + assert.match( + mdVi, + /\| # \| Mẫu máy \| Hãng \| Điểm người mua \(\/5\.0\) \| Điểm chuyên gia \(\/10\) \| Đánh giá thật \| Giá thấp nhất \(VNĐ\) \| Tín hiệu giá \|/ + ); + assert.match( + mdVi, + /\| 1 \| iPhone 16 \| Apple \| 4\.8\/5\.0 \| 9\.1\/10 \| 1\.200 \| 21\.990\.000 ₫ \|/ + ); + + const mdEnSingle = buildCatalogMarkdown( + [rowIphone], + 'Smartphones', + () => EMPTY_RADAR_SIGNAL, + 'en', + tEn as never + ); + assert.match( + mdEnSingle, + /^### 📂 Rate\.vn category catalog: Smartphones \(1 device\)/ + ); + assert.match( + mdEnSingle, + /\| # \| Model \| Brand \| User rating \(\/5\.0\) \| Expert score \(\/10\) \| Authentic reviews \| Min price \(VND\) \| Price signal \|/ + ); + + const mdCs = buildCatalogMarkdown( + [rowIphone, rowGalaxy], + 'Chytré telefony', + () => EMPTY_RADAR_SIGNAL, + 'cs', + tCs as never + ); + assert.match( + mdCs, + /^### 📂 Katalog kategorie Rate\.vn: Chytré telefony \(2 zařízení\)/ + ); + assert.match( + mdCs, + /\| # \| Model \| Značka \| Hodnocení kupujících \(\/5,0\) \| Hodnocení expertů \(\/10\) \| Skutečné recenze \| Nejnižší cena \(VND\) \| Cenový signál \|/ + ); + assert.ok( + mdCs.includes( + `4,8/5,0 | 9,1/10 | 1${NBSP}200 | 21${NBSP}990${NBSP}000${NBSP}₫` + ), + 'Czech Markdown export formats ratings, review counts, and VND prices in cs locale' + ); +} + +runCatalogSyncGuardsAndI18nSuite() + .then(() => { + console.warn( + '✅ Catalogue plain-listing, ?brand= sync, 1v1 guards & i18n suite passed' + ); + }) + .catch((err) => { + console.error(err); + process.exit(1); + }); From 7599112e2b4ccce9b5905f27782dbc61880dfc42 Mon Sep 17 00:00:00 2001 From: JOY <5027251+JOY@users.noreply.github.com> Date: Sun, 22 Nov 2026 08:46:37 +0700 Subject: [PATCH 12/24] fix(web): dedupe compare item variants, wire combobox a11y, and guard versus urls (#1831) * fix(web): dedupe Item variants in Compare, wire combobox ARIA/Escape, and guard SEO/versus slugs * fix(web): preserve non-cheapest tray variants and wire listbox/option focus semantics --- .../app/[locale]/compare/[versus]/page.tsx | 29 +- apps/web/src/app/[locale]/compare/page.tsx | 22 +- .../components/compare/CompareWorkspace.tsx | 875 +++++++++++++----- .../src/lib/__tests__/item-compare.test.ts | 737 +++++++++++++++ .../rate/__tests__/compare-selection.test.ts | 165 +++- apps/web/src/server/rate/compare-selection.ts | 24 +- 6 files changed, 1601 insertions(+), 251 deletions(-) diff --git a/apps/web/src/app/[locale]/compare/[versus]/page.tsx b/apps/web/src/app/[locale]/compare/[versus]/page.tsx index 24395f20..d54f4ca7 100644 --- a/apps/web/src/app/[locale]/compare/[versus]/page.tsx +++ b/apps/web/src/app/[locale]/compare/[versus]/page.tsx @@ -47,7 +47,13 @@ export function parseVersusSlugs( rawVersus: string, keepCase = false ): [string, string] | null { - const decoded = decodeURIComponent(rawVersus || '').trim(); + if (typeof rawVersus !== 'string') return null; + let decoded: string; + try { + decoded = decodeURIComponent(rawVersus).trim(); + } catch { + return null; + } const cleaned = keepCase ? decoded : decoded.toLowerCase(); const parts = cleaned.split('-vs-'); if (parts.length !== 2) return null; @@ -102,16 +108,21 @@ export async function generateMetadata({ const fullTitle = t('versusMetaTitle', { nameA: a.name, nameB: b.name }); // Both names first; the fixed text, then the site name, give way to fit a // search result (DOS/Rate#777). The share card keeps the full title. - const title = fitTitle([ - fullTitle, - t('versusMetaTitleShort', { nameA: a.name, nameB: b.name }), - `${a.name} vs ${b.name}`, - ]); + const title = fitTitle( + [ + fullTitle, + t('versusMetaTitleShort', { nameA: a.name, nameB: b.name }), + `${a.name} vs ${b.name}`, + ], + undefined, + lang + ); const description = fitDescription( t('versusMetaDescription', { nameA: a.name, nameB: b.name, - }) + }), + lang ); return { @@ -191,12 +202,12 @@ export function buildVersusItemListSchema( export default async function VersusComparePage({ params }: VersusPageProps) { const { locale, versus } = await params; setRequestLocale(locale); - const lang = normalizeRateLocale(locale); - const t = await getTranslations({ locale: lang, namespace: 'compare' }); const parsed = parseVersusSlugs(versus); if (!parsed) { notFound(); } + const lang = normalizeRateLocale(locale); + const t = await getTranslations({ locale: lang, namespace: 'compare' }); // RATE_ITEM_MODEL on: versus URLs name Items, an old variant pair 308s to // its Item pair and a same-Item pair is no versus (DOS/Rate#1156); off: diff --git a/apps/web/src/app/[locale]/compare/page.tsx b/apps/web/src/app/[locale]/compare/page.tsx index 69661416..caa880f5 100644 --- a/apps/web/src/app/[locale]/compare/page.tsx +++ b/apps/web/src/app/[locale]/compare/page.tsx @@ -45,17 +45,18 @@ export async function generateMetadata({ const names = compared.map((p) => p.name).join(' vs '); const fullTitle = t('compareMetaTitleWithNames', { names }); const description = fitDescription( - t('compareMetaDescWithNames', { names }) + t('compareMetaDescWithNames', { names }), + lang ); return { // The names come first; the fixed text, then the site name, give way // to fit a search result (DOS/Rate#777). The share card keeps the // full title, as on the versus page. - title: fitTitle([ - fullTitle, - t('compareMetaTitleWithNamesShort', { names }), - names, - ]), + title: fitTitle( + [fullTitle, t('compareMetaTitleWithNamesShort', { names }), names], + undefined, + lang + ), description, alternates: { canonical: canonicalUrl, @@ -88,19 +89,26 @@ export default async function ComparePhonesPage({ const t = await getTranslations({ locale: lang, namespace: 'compare' }); const { catalogCandidates, + rawCandidateSlugs, activeCategory, initialProducts, hasExplicitSlugs, resolvedSelection, } = await getComparePageSelection(await searchParams); // RATE_ITEM_MODEL on: versus links name Items (DOS/Rate#1290, DOS/Rate#1768); off: null. - const itemSlugBySlug = await getItemSlugsFor( + const baseItemSlugBySlug = await getItemSlugsFor( [ ...initialProducts.map((p) => p.slug), ...catalogCandidates.map((p) => p.slug), ], resolvedSelection ); + const rawItemSlugBySlug = baseItemSlugBySlug + ? await getItemSlugsFor(rawCandidateSlugs, resolvedSelection) + : null; + const itemSlugBySlug = baseItemSlugBySlug + ? { ...rawItemSlugBySlug, ...baseItemSlugBySlug } + : null; return ( diff --git a/apps/web/src/components/compare/CompareWorkspace.tsx b/apps/web/src/components/compare/CompareWorkspace.tsx index d29be617..92b9cf08 100644 --- a/apps/web/src/components/compare/CompareWorkspace.tsx +++ b/apps/web/src/components/compare/CompareWorkspace.tsx @@ -1,6 +1,12 @@ 'use client'; -import React, { useEffect, useMemo, useRef, useState } from 'react'; +import React, { + useCallback, + useEffect, + useMemo, + useRef, + useState, +} from 'react'; import { useTranslations } from 'next-intl'; import type { IconType } from 'react-icons'; import { @@ -63,6 +69,7 @@ import { escapeCsvCell } from '@/lib/csv-safe'; import RadarBadge from '@/components/radar/RadarBadge'; import { useDealRadarIndex } from '@/components/radar/use-deal-radar'; import { + type CompareTrayItem, getCompareTray, MAX_COMPARE_ITEMS, setCompareTray, @@ -456,6 +463,192 @@ export function isValidCompareVersusPair( ); } +export function dedupeCompareProductsByItem( + rows: readonly T[], + versusSlugMap?: Readonly> | null +): T[] { + const kept: T[] = []; + for (const row of rows) { + if (!row || typeof row.slug !== 'string' || !row.slug) continue; + if ( + kept.some((existing) => + isSameCompareVersusItem(existing.slug, row.slug, versusSlugMap) + ) + ) { + continue; + } + kept.push(row); + } + return kept; +} + +export function isCompareCandidateSelected( + products: readonly Pick[], + candidateSlug: string, + versusSlugMap?: Readonly> | null +): boolean { + if (!candidateSlug) return false; + return products.some((p) => + isSameCompareVersusItem(p.slug, candidateSlug, versusSlugMap) + ); +} + +export function pickCompareCategoryDefaults( + catalogCandidates: readonly MasterProduct[], + category: CanonicalCategorySlug, + versusSlugMap?: Readonly> | null, + limit = 3 +): MasterProduct[] { + return dedupeCompareProductsByItem( + catalogCandidates.filter( + (c) => getMasterProductCategorySlug(c) === category + ), + versusSlugMap + ).slice(0, limit); +} + +export function buildCompareAutocompleteSuggestions( + catalogCandidates: readonly MasterProduct[], + remoteResults: readonly MasterProduct[] | null, + activeCategory: CanonicalCategorySlug, + searchQuery: string, + versusSlugMap?: Readonly> | null, + limit = 8 +): MasterProduct[] { + const sameCategoryCatalog = catalogCandidates.filter( + (c) => getMasterProductCategorySlug(c) === activeCategory + ); + const q = searchQuery.trim().toLowerCase(); + if (!q) { + return dedupeCompareProductsByItem( + sameCategoryCatalog, + versusSlugMap + ).slice(0, limit); + } + const localFiltered = sameCategoryCatalog.filter( + (item) => + item.name.toLowerCase().includes(q) || + item.brand.toLowerCase().includes(q) || + item.model.toLowerCase().includes(q) || + item.slug.toLowerCase().includes(q) + ); + const sameCategoryRemote = (remoteResults ?? []).filter( + (item) => getMasterProductCategorySlug(item) === activeCategory + ); + return dedupeCompareProductsByItem( + [...localFiltered, ...sameCategoryRemote], + versusSlugMap + ).slice(0, limit); +} + +export function applyCompareCandidateSelection( + products: readonly MasterProduct[], + candidate: MasterProduct, + activeCategory: CanonicalCategorySlug, + activeReplaceIndex: number | null, + versusSlugMap?: Readonly> | null +): { + nextProducts: MasterProduct[]; + nextCategory: CanonicalCategorySlug; +} | null { + const candidateCat = getMasterProductCategorySlug(candidate); + if (candidateCat !== activeCategory) { + return { nextProducts: [candidate], nextCategory: candidateCat }; + } + + if ( + activeReplaceIndex !== null && + activeReplaceIndex >= 0 && + activeReplaceIndex < products.length + ) { + if (isCompareCandidateSelected(products, candidate.slug, versusSlugMap)) { + return null; + } + const next = [...products]; + next[activeReplaceIndex] = candidate; + return { + nextProducts: dedupeCompareProductsByItem(next, versusSlugMap), + nextCategory: activeCategory, + }; + } + + if (isCompareCandidateSelected(products, candidate.slug, versusSlugMap)) { + return null; + } + if (products.length >= MAX_COMPARE_ITEMS) { + return null; + } + return { + nextProducts: dedupeCompareProductsByItem( + [...products, candidate], + versusSlugMap + ), + nextCategory: activeCategory, + }; +} + +export function resolveCompareTrayRestore( + trayItems: readonly CompareTrayItem[], + catalogCandidates: readonly MasterProduct[], + itemSlugBySlug?: Readonly> | null +): { + activeCategory: CanonicalCategorySlug; + products: MasterProduct[]; + slugsCsv: string; +} | null { + if (!Array.isArray(trayItems) || trayItems.length === 0) return null; + const bySlug = new Map(catalogCandidates.map((c) => [c.slug, c])); + const slugMap = buildCompareVersusSlugMap(catalogCandidates, itemSlugBySlug); + const matched: MasterProduct[] = []; + for (const trayItem of trayItems) { + if (!trayItem?.slug) continue; + const direct = bySlug.get(trayItem.slug); + if (direct) { + matched.push(direct); + continue; + } + // Under RATE_ITEM_MODEL, catalogCandidates collapses each Item to its + // standing variant. Match a saved non-standing variant back to its Item's + // candidate while keeping the saved variant's own slug, name, thumbnail, + // and price so storedProducts hydration and ?slugs= load that exact size (#1820). + const standingSibling = slugMap + ? catalogCandidates.find((c) => + isSameCompareVersusItem(c.slug, trayItem.slug, slugMap) + ) + : undefined; + if (standingSibling) { + matched.push({ + ...standingSibling, + id: `tray-${trayItem.slug}`, + slug: trayItem.slug, + name: trayItem.name || standingSibling.name, + brand: trayItem.brand || standingSibling.brand, + thumbnail: trayItem.thumbnail ?? standingSibling.thumbnail, + min_price: + sanitizeComparePriceVnd(trayItem.min_price) || + standingSibling.min_price, + max_price: sanitizeComparePriceVnd(trayItem.min_price), + }); + } + } + if (matched.length === 0) return null; + const primaryCat = getMasterProductCategorySlug(matched[0]!); + const mergedSlugMap = buildCompareVersusSlugMap( + [...catalogCandidates, ...matched], + itemSlugBySlug + ); + const sameCatMatched = dedupeCompareProductsByItem( + matched.filter((m) => getMasterProductCategorySlug(m) === primaryCat), + mergedSlugMap + ); + if (sameCatMatched.length === 0) return null; + return { + activeCategory: primaryCat, + products: sameCatMatched, + slugsCsv: sameCatMatched.map((m) => m.slug).join(','), + }; +} + export function buildCompareVersusSlugMap( rows: readonly MasterProduct[], itemSlugBySlug?: Readonly> | null, @@ -771,6 +964,33 @@ export function resolveCompareAddSlotAriaLabel( .trim(); } +export const COMPARE_AUTOCOMPLETE_LIST_ID = 'compare-autocomplete-listbox'; +export const COMPARE_AUTOCOMPLETE_HEADING_ID = 'compare-autocomplete-heading'; + +export function resolveCompareAutocompleteA11y(options: { + dropdownOpen: boolean; + productCount: number; + activeReplaceIndex: number | null; + maxItems?: number; +}): { + role: 'combobox'; + ariaAutocomplete: 'list'; + ariaExpanded: boolean; + ariaControls: string; + disabled: boolean; +} { + const max = options.maxItems ?? MAX_COMPARE_ITEMS; + const canOpen = + options.productCount < max || options.activeReplaceIndex !== null; + return { + role: 'combobox', + ariaAutocomplete: 'list', + ariaExpanded: Boolean(options.dropdownOpen && canOpen), + ariaControls: COMPARE_AUTOCOMPLETE_LIST_ID, + disabled: !canOpen, + }; +} + export function shouldDismissCompareOnOutsidePointerDown( target: { closest?: (selector: string) => unknown } | null | undefined, isInsideSearchBox: boolean @@ -786,6 +1006,20 @@ export function shouldDismissCompareOnOutsidePointerDown( return true; } +export function shouldDismissCompareOnFocusOut( + relatedTarget: { closest?: (selector: string) => unknown } | null | undefined, + isInsideSearchBox: boolean +): boolean { + if (!relatedTarget || isInsideSearchBox) return false; + if ( + typeof relatedTarget.closest === 'function' && + relatedTarget.closest('[data-compare-slot-trigger]') + ) { + return false; + } + return true; +} + export function nextCompareReplaceState( currentReplaceIndex: number | null, clickedIndex: number @@ -1638,9 +1872,17 @@ export default function CompareWorkspace({ initialProducts.length > 0 ? getMasterProductCategorySlug(initialProducts[0]!) : initialCategory; - return initialProducts.filter( + const sameCat = initialProducts.filter( (p) => getMasterProductCategorySlug(p) === targetCat ); + if (hasExplicitSlugs) return sameCat; + return dedupeCompareProductsByItem( + sameCat, + buildCompareVersusSlugMap( + [...catalogCandidates, ...initialProducts], + itemSlugBySlug + ) + ); }); const [searchQuery, setSearchQuery] = useState(''); const [remoteResults, setRemoteResults] = useState( @@ -1681,11 +1923,17 @@ export default function CompareWorkspace({ const versusSlugMap = useMemo( () => buildCompareVersusSlugMap( - [...catalogCandidates, ...products], + [...catalogCandidates, ...products, ...(remoteResults ?? [])], itemSlugBySlug, clientItemSlugs ), - [catalogCandidates, products, itemSlugBySlug, clientItemSlugs] + [ + catalogCandidates, + products, + remoteResults, + itemSlugBySlug, + clientItemSlugs, + ] ); const popularBattles = useMemo( () => @@ -1701,6 +1949,7 @@ export default function CompareWorkspace({ const candidates = new Set([ ...products.map((p) => p.slug), ...popularBattles.flatMap((b) => [b.productA.slug, b.productB.slug]), + ...(remoteResults ?? []).map((p) => p.slug), ]); const inFlight = itemLookupsRef.current; for (const [slug, controller] of inFlight) { @@ -1751,6 +2000,7 @@ export default function CompareWorkspace({ itemSlugBySlug, products, popularBattles, + remoteResults, clientItemSlugs, itemLookupRetryTick, ]); @@ -1841,13 +2091,19 @@ export default function CompareWorkspace({ [activeCategory, categoryTabs] ); - // Candidates strictly locked to the active category + // Candidates deduplicated per Item under RATE_ITEM_MODEL (#1820), then + // strictly locked to the active category. + const dedupedCatalogCandidates = useMemo( + () => dedupeCompareProductsByItem(catalogCandidates, versusSlugMap), + [catalogCandidates, versusSlugMap] + ); + const categoryLockedCandidates = useMemo( () => - catalogCandidates.filter( + dedupedCatalogCandidates.filter( (c) => getMasterProductCategorySlug(c) === activeCategory ), - [catalogCandidates, activeCategory] + [dedupedCatalogCandidates, activeCategory] ); // Rows known to carry their full spec sheet: the page's own and the search @@ -1912,10 +2168,27 @@ export default function CompareWorkspace({ activeReplaceIndexRef.current = null; setReplaceIndex(null); setActiveCategory(cat); + const sameCat = initialProducts.filter( + (p) => getMasterProductCategorySlug(p) === cat + ); setProducts( - initialProducts.filter((p) => getMasterProductCategorySlug(p) === cat) + hasExplicitSlugs + ? sameCat + : dedupeCompareProductsByItem( + sameCat, + buildCompareVersusSlugMap( + [...catalogCandidates, ...initialProducts], + itemSlugBySlug + ) + ) ); - }, [initialProducts, initialCategory, hasExplicitSlugs]); + }, [ + initialProducts, + initialCategory, + hasExplicitSlugs, + catalogCandidates, + itemSlugBySlug, + ]); // If user visits /compare without ?slugs=, restore their saved Compare Tray (strictly filtered to same category) useEffect(() => { @@ -1923,30 +2196,25 @@ export default function CompareWorkspace({ restoredTrayRef.current = true; if (!hasExplicitSlugs) { - const trayItems = getCompareTray(); - if (trayItems.length > 0) { - const bySlug = new Map(catalogCandidates.map((c) => [c.slug, c])); - const matched = trayItems - .map((t) => bySlug.get(t.slug)) - .filter((item): item is MasterProduct => Boolean(item)); - if (matched.length > 0) { - const primaryCat = getMasterProductCategorySlug(matched[0]!); - const sameCatMatched = matched.filter( - (m) => getMasterProductCategorySlug(m) === primaryCat - ); - setActiveCategory(primaryCat); - setProducts(sameCatMatched); - const traySlugsCsv = sameCatMatched.map((t) => t.slug).join(','); - router.replace(`/compare?slugs=${traySlugsCsv}`, { scroll: false }); - return; - } + const restored = resolveCompareTrayRestore( + getCompareTray(), + catalogCandidates, + itemSlugBySlug + ); + if (restored) { + setActiveCategory(restored.activeCategory); + setProducts(restored.products); + router.replace(`/compare?slugs=${restored.slugsCsv}`, { + scroll: false, + }); + return; } } // Otherwise only show the page's defaults or a shared ?slugs= list: the // tray changes on the visitor's own edits (syncSelection), never on load, // so it does not follow them site-wide or lose what they saved (#714). - }, [hasExplicitSlugs, catalogCandidates, router]); + }, [hasExplicitSlugs, catalogCandidates, itemSlugBySlug, router]); // Sync URL and localStorage tray whenever compared products change (always locked to 1 category) const syncSelection = ( @@ -1958,8 +2226,9 @@ export default function CompareWorkspace({ (nextProducts.length > 0 ? getMasterProductCategorySlug(nextProducts[0]!) : activeCategory); - const sameCategoryProducts = nextProducts.filter( - (p) => getMasterProductCategorySlug(p) === targetCat + const sameCategoryProducts = dedupeCompareProductsByItem( + nextProducts.filter((p) => getMasterProductCategorySlug(p) === targetCat), + versusSlugMap ); clearReplaceMode(); setActiveCategory(targetCat); @@ -1986,14 +2255,75 @@ export default function CompareWorkspace({ clearReplaceMode(); setSearchQuery(''); setDropdownOpen(false); - const nextDefaults = catalogCandidates - .filter((c) => getMasterProductCategorySlug(c) === nextCat) - .slice(0, 3); + const nextDefaults = pickCompareCategoryDefaults( + catalogCandidates, + nextCat, + versusSlugMap, + 3 + ); syncSelection(nextDefaults, nextCat); }; - // Close autocomplete dropdown when clicking outside + const dropdownOpenRef = useRef(dropdownOpen); + const productsCountRef = useRef(products.length); + const suppressNextInputFocusOpenRef = useRef(false); + useEffect(() => { + dropdownOpenRef.current = dropdownOpen; + productsCountRef.current = products.length; + }, [dropdownOpen, products.length]); + + const focusSearchInputWithoutOpening = useCallback(() => { + const input = searchInputRef.current; + if (!input || input.disabled) return; + suppressNextInputFocusOpenRef.current = true; + input.focus(); + queueMicrotask(() => { + suppressNextInputFocusOpenRef.current = false; + }); + }, []); + + const restoreFocusAfterAutocompleteClose = useCallback( + (previousReplaceIndex: number | null, nextProductCount: number) => { + if (nextProductCount >= MAX_COMPARE_ITEMS) { + const targetIdx = + previousReplaceIndex !== null + ? previousReplaceIndex + : nextProductCount - 1; + requestAnimationFrame(() => { + const triggers = document.querySelectorAll( + '[data-compare-slot-trigger="replace"]' + ); + triggers[targetIdx]?.focus(); + }); + return; + } + focusSearchInputWithoutOpening(); + }, + [focusSearchInputWithoutOpening] + ); + + const dismissAutocomplete = () => { + cancelPendingCompareReplaceFocus( + replaceFocusTimerRef, + scheduledReplaceIndexRef + ); + activeReplaceIndexRef.current = null; + setDropdownOpen(false); + setReplaceIndex(null); + }; + + // Close autocomplete dropdown on outside pointer down, outside focus loss, or Escape (#1820). useEffect(() => { + const closeAndClearReplace = () => { + cancelPendingCompareReplaceFocus( + replaceFocusTimerRef, + scheduledReplaceIndexRef + ); + activeReplaceIndexRef.current = null; + setDropdownOpen(false); + setReplaceIndex(null); + }; + const handleOutside = (e: MouseEvent) => { if (!searchBoxRef.current) return; const isInside = searchBoxRef.current.contains(e.target as Node); @@ -2002,18 +2332,58 @@ export default function CompareWorkspace({ ? (e.target as { closest?: (selector: string) => unknown }) : null; if (shouldDismissCompareOnOutsidePointerDown(targetEl, isInside)) { - cancelPendingCompareReplaceFocus( - replaceFocusTimerRef, - scheduledReplaceIndexRef + closeAndClearReplace(); + } + }; + + const handleFocusOut = (e: FocusEvent) => { + const box = searchBoxRef.current; + if (!box) return; + const next = e.relatedTarget as Node | null; + const isInside = Boolean(next && box.contains(next)); + const relatedEl = + next && typeof next === 'object' + ? (next as { closest?: (selector: string) => unknown }) + : null; + if (shouldDismissCompareOnFocusOut(relatedEl, isInside)) { + closeAndClearReplace(); + } + }; + + const handleKeyDown = (e: KeyboardEvent) => { + if (e.key !== 'Escape' || e.defaultPrevented) return; + const box = searchBoxRef.current; + const activeEl = + typeof document !== 'undefined' ? document.activeElement : null; + const wasInside = Boolean(box && activeEl && box.contains(activeEl)); + const previousReplaceIndex = activeReplaceIndexRef.current; + if ( + !wasInside && + previousReplaceIndex === null && + !dropdownOpenRef.current + ) { + return; + } + e.preventDefault(); + closeAndClearReplace(); + if (wasInside) { + restoreFocusAfterAutocompleteClose( + previousReplaceIndex, + productsCountRef.current ); - activeReplaceIndexRef.current = null; - setDropdownOpen(false); - setReplaceIndex(null); } }; + + const box = searchBoxRef.current; document.addEventListener('mousedown', handleOutside); - return () => document.removeEventListener('mousedown', handleOutside); - }, []); + document.addEventListener('keydown', handleKeyDown); + box?.addEventListener('focusout', handleFocusOut); + return () => { + document.removeEventListener('mousedown', handleOutside); + document.removeEventListener('keydown', handleKeyDown); + box?.removeEventListener('focusout', handleFocusOut); + }; + }, [restoreFocusAfterAutocompleteClose]); // Debounced remote search via Layer 1 REST API `/api/v1/rate/products` (locked to activeCategory) useEffect(() => { @@ -2058,68 +2428,53 @@ export default function CompareWorkspace({ }; }, [searchQuery, activeCategory]); - // Combine instant client-side matches + remote REST API matches (strictly within activeCategory) - const suggestions = useMemo(() => { - const q = searchQuery.trim().toLowerCase(); - if (!q) { - return categoryLockedCandidates.slice(0, 8); - } - const localFiltered = categoryLockedCandidates.filter( - (item) => - item.name.toLowerCase().includes(q) || - item.brand.toLowerCase().includes(q) || - item.model.toLowerCase().includes(q) || - item.slug.toLowerCase().includes(q) - ); - const mergedMap = new Map(); - for (const item of [...localFiltered, ...(remoteResults ?? [])]) { - if ( - getMasterProductCategorySlug(item) === activeCategory && - !mergedMap.has(item.slug) - ) { - mergedMap.set(item.slug, item); - } - } - return Array.from(mergedMap.values()).slice(0, 8); - }, [searchQuery, remoteResults, categoryLockedCandidates, activeCategory]); - - const selectedSlugs = useMemo( - () => new Set(products.map((p) => p.slug)), - [products] + // Combine instant client-side matches + remote REST API matches, deduplicated per Item (#1820) + const suggestions = useMemo( + () => + buildCompareAutocompleteSuggestions( + catalogCandidates, + remoteResults, + activeCategory, + searchQuery, + versusSlugMap, + 8 + ), + [ + catalogCandidates, + remoteResults, + activeCategory, + searchQuery, + versusSlugMap, + ] ); const handleSelectCandidate = (candidate: MasterProduct) => { - const candidateCat = getMasterProductCategorySlug(candidate); - if (candidateCat !== activeCategory) { - syncSelection([candidate], candidateCat); - clearReplaceMode(); - setSearchQuery(''); - setDropdownOpen(false); - return; - } - - if (activeReplaceIndex !== null) { - const next = [...products]; - next[activeReplaceIndex] = candidate; - const deduped = Array.from( - new Map(next.map((p) => [p.slug, p])).values() - ); - syncSelection(deduped, activeCategory); - clearReplaceMode(); - setSearchQuery(''); - setDropdownOpen(false); - return; - } - - if (selectedSlugs.has(candidate.slug)) { - return; - } - if (products.length >= MAX_COMPARE_ITEMS) { - return; - } - syncSelection([...products, candidate], activeCategory); + const previousReplaceIndex = activeReplaceIndex; + const activeEl = + typeof document !== 'undefined' ? document.activeElement : null; + const wasInsideSearchBox = Boolean( + searchBoxRef.current && + activeEl && + searchBoxRef.current.contains(activeEl) + ); + const applied = applyCompareCandidateSelection( + products, + candidate, + activeCategory, + activeReplaceIndex, + versusSlugMap + ); + if (!applied) return; + syncSelection(applied.nextProducts, applied.nextCategory); + clearReplaceMode(); setSearchQuery(''); setDropdownOpen(false); + if (wasInsideSearchBox) { + restoreFocusAfterAutocompleteClose( + previousReplaceIndex, + applied.nextProducts.length + ); + } }; const handleRemoveProduct = (slug: string) => { @@ -2355,6 +2710,12 @@ export default function CompareWorkspace({ [products] ); + const autocompleteA11y = resolveCompareAutocompleteA11y({ + dropdownOpen, + productCount: products.length, + activeReplaceIndex, + }); + const controlsBlock = ( <> {/* 5-Category Lock Switcher Bar */} @@ -2371,7 +2732,7 @@ export default function CompareWorkspace({ > {categoryTabs.map((tab) => { const isActive = tab.slug === activeCategory; - const count = catalogCandidates.filter( + const count = dedupedCatalogCandidates.filter( (c) => getMasterProductCategorySlug(c) === tab.slug ).length; return ( @@ -2398,7 +2759,23 @@ export default function CompareWorkspace({
{/* Autocomplete Input */} -
+
{ + const box = searchBoxRef.current; + if (!box) return; + const next = e.relatedTarget as Node | null; + const isInside = Boolean(next && box.contains(next)); + const relatedEl = + next && typeof next === 'object' + ? (next as { closest?: (selector: string) => unknown }) + : null; + if (shouldDismissCompareOnFocusOut(relatedEl, isInside)) { + dismissAutocomplete(); + } + }} + >
{/* Autocomplete Suggestions Dropdown */} - {dropdownOpen && - (products.length < MAX_COMPARE_ITEMS || - activeReplaceIndex !== null) && ( + {autocompleteA11y.ariaExpanded && ( +
+ + {searchQuery.trim() + ? t('searchResultsFor', { + category: activeCategoryMeta.label, + query: searchQuery.trim(), + }) + : t('sameCategoryList', { + unit: activeCategoryMeta.unitLabel, + })} + + {isSearching && ( + + {t('searchingBadge')} + + )} +
+ +
-
- - {searchQuery.trim() - ? t('searchResultsFor', { - category: activeCategoryMeta.label, - query: searchQuery.trim(), - }) - : t('sameCategoryList', { - unit: activeCategoryMeta.unitLabel, - })} - - {isSearching && ( - - {t('searchingBadge')} - - )} -
- {suggestions.length === 0 ? ( -
+
{t('noMatchingDevices', { unit: activeCategoryMeta.unitLabel, query: searchQuery, })}
) : ( -
- {suggestions.map((item) => { - const alreadySelected = selectedSlugs.has(item.slug); - const thumb = resolveProductImageUrl( - item.slug, - productImageUrl(item) - ); - const priceFormatted = formatComparePriceVnd( - item.min_price, - locale - ); - const rating = getEffectiveRating(item); - - return ( - - ); - })} -
+ ? 'bg-dark text-white' + : 'bg-danger text-white' + }`} + > + {alreadySelected + ? t('alreadyComparingBadge') + : activeReplaceIndex !== null + ? t('selectReplacementBadge') + : t('addToCompareBadge')} + + + ); + }) )}
- )} +
+ )}
@@ -2711,7 +3141,10 @@ export default function CompareWorkspace({ })} {categoryLockedCandidates - .filter((c) => !selectedSlugs.has(c.slug)) + .filter( + (c) => + !isCompareCandidateSelected(products, c.slug, versusSlugMap) + ) .slice(0, 5) .map((c) => ( `; }) @@ -206,11 +207,14 @@ export function offerMarks(offer: PanelOffer): string[] { } /** A star rating, or null when unknown or zero (DOS/Rate#1301). */ -export function ratingStarText(raw: unknown): string | null { +export function ratingStarText( + raw: unknown, + locale = t('langCode') +): string | null { const rating = normalizeFiveStarRating(raw); return rating == null ? null - : `★ ${formatSafeNumber(rating, { min: 0, max: 5, decimals: 1 })}`; + : `★ ${formatSafeNumber(rating, { min: 0, max: 5, decimals: 1 }, locale)}`; } export function officialBadgeHtml(): string { @@ -219,11 +223,12 @@ export function officialBadgeHtml(): string { function renderOfferRowHtml( off: ExtensionOfferItem, - cheapest: boolean + cheapest: boolean, + locale: string ): string { const marks = offerMarkItems(off); const muted = !isOfferBuyable(off); - const star = ratingStarText(off.rating_star); + const star = ratingStarText(off.rating_star, locale); const meta = [ escapeHtml(String(off.platform ?? '').toUpperCase()), star, @@ -233,7 +238,11 @@ function renderOfferRowHtml( off.merchant.risk_score == null ? t('offerCombinedRiskNoData') : t('offerCombinedRisk', [ - formatSafeNumber(off.merchant.risk_score, { min: 0, max: 100 }), + formatSafeNumber( + off.merchant.risk_score, + { min: 0, max: 100 }, + locale + ), ]) ), ].filter(Boolean); @@ -264,23 +273,26 @@ function renderOfferRowHtml(
- ${formatSafeExtensionVnd(off.price_current)} + ${formatSafeExtensionVnd(off.price_current, t('notAvailable'), locale)}
`; } /** The store price list card, or '' with no offer (DOS/Rate#1297). */ -export function renderOffersCardHtml(offers: ExtensionOfferItem[]): string { +export function renderOffersCardHtml( + offers: ExtensionOfferItem[], + locale = t('langCode') +): string { if (offers.length === 0) return ''; const ordered = orderOffersForPanel(offers); const cheapestId = cheapestBuyableOfferId(ordered); return `
-
${escapeHtml(t('offersCardTitle', [formatSafeNumber(offers.length, { min: 0 })]))}
+
${escapeHtml(t('offersCardTitle', [formatSafeNumber(offers.length, { min: 0 }, locale)]))}
${ordered .slice(0, 5) - .map((off) => renderOfferRowHtml(off, off.id === cheapestId)) + .map((off) => renderOfferRowHtml(off, off.id === cheapestId, locale)) .join('')}
`; @@ -298,18 +310,22 @@ export function renderReviewCardHtml( | 'rating_score' | 'user_rating_weighted' | 'average_rating' - > | null + > | null, + locale = t('langCode') ): string { if (!reviews) return ''; - const star = ratingStarText(resolveExtensionMasterRating(master)); + const star = ratingStarText(resolveExtensionMasterRating(master), locale); const analysed = Number(reviews.total_analyzed); const hasReviews = Number.isFinite(analysed) && analysed > 0; // The server writes the summary in Vietnamese only. An English or Czech // panel leaves it out rather than machine-translating it or guessing its // language: the genuine share and the count above say the same // (DOS/Rate#1374). + const isViLocale = + typeof locale === 'string' && + locale.trim().slice(0, 32).toLowerCase().split(/[-_]/)[0] === 'vi'; const summary = - hasReviews && reviews.ai_summary && t('langCode') === 'vi' + hasReviews && reviews.ai_summary && isViLocale && t('langCode') === 'vi' ? reviews.ai_summary : null; return ` @@ -324,7 +340,7 @@ export function renderReviewCardHtml(
${escapeHtml(t('reviewAuthenticShare'))}
${ hasReviews - ? `
${escapeHtml(t('reviewAuthenticValue', [formatSafeNumber(reviews.authentic_pct, { min: 0, max: 100 }), formatSafeNumber(analysed, { min: 0 })]))}
` + ? `
${escapeHtml(t('reviewAuthenticValue', [formatSafeNumber(reviews.authentic_pct, { min: 0, max: 100 }, locale), formatSafeNumber(analysed, { min: 0 }, locale)]))}
` : `
${escapeHtml(t('reviewNone'))}
` }
@@ -409,7 +425,8 @@ const LOOKUP_FAILURE_MESSAGE = { export function renderLookupErrorHtml( kind: LookupFailureKind, productTitle?: string | null, - retryAfterSeconds?: number | null + retryAfterSeconds?: number | null, + locale = t('langCode') ): string { const title = stripLoneSurrogates(String(productTitle ?? '')).trim(); // A rate limit says how long to wait when the server said so (DOS/Rate#1466). @@ -422,7 +439,9 @@ export function renderLookupErrorHtml( : null; const message = wait !== null - ? t('lookupErrorRateLimitedWait', [String(wait)]) + ? t('lookupErrorRateLimitedWait', [ + formatSafeNumber(wait, { min: 1 }, locale), + ]) : t(LOOKUP_FAILURE_MESSAGE[kind]); return `
- ${escapeHtml(String(d.platform ?? '').toUpperCase())} (${contentTextHtml(stripLoneSurrogates(String(d.merchantName ?? '')))}) - ${escapeHtml(t('radarSavings', [formatVnd(d.savingsVsMedianVnd), formatSafeNumber(d.savingsVsMedianPct, { min: 0, max: 100 })]))} + ${escapeHtml(String(d.platform ?? '').toUpperCase())} (${contentTextHtml(stripLoneSurrogates(String(d.merchantName ?? '')))}) - ${escapeHtml(t('radarSavings', [formatVnd(d.savingsVsMedianVnd, uiLocale), formatSafeNumber(d.savingsVsMedianPct, { min: 0, max: 100 }, uiLocale)]))}
- ${formatVnd(d.priceCurrent)} + ${formatVnd(d.priceCurrent, uiLocale)}
` @@ -176,11 +180,11 @@ async function fetchAndRenderDealRadar(
${contentTextHtml(stripLoneSurrogates(String(s.productName ?? '')))}
- ${escapeHtml(t('radarSpreadVersus', [String(s.cheapestPlatform ?? '').toUpperCase(), formatVnd(s.minPrice), String(s.priciestPlatform ?? '').toUpperCase(), formatVnd(s.maxPrice)]))} + ${escapeHtml(t('radarSpreadVersus', [String(s.cheapestPlatform ?? '').toUpperCase(), formatVnd(s.minPrice, uiLocale), String(s.priciestPlatform ?? '').toUpperCase(), formatVnd(s.maxPrice, uiLocale)]))}
- ${escapeHtml(t('radarSpreadAmount', [formatVnd(s.spreadVnd)]))} + ${escapeHtml(t('radarSpreadAmount', [formatVnd(s.spreadVnd, uiLocale)]))}
` @@ -199,10 +203,10 @@ async function fetchAndRenderDealRadar(
${contentTextHtml(stripLoneSurrogates(String(trap.productName ?? '')))}
- ${escapeHtml(String(trap.platform ?? '').toUpperCase())} - ${escapeHtml(t('radarTrapMarkup', [formatSafeNumber(trap.fakeMarkupPct, { min: 0, max: 10000 }), formatVnd(trap.priceOriginal)]))} + ${escapeHtml(String(trap.platform ?? '').toUpperCase())} - ${escapeHtml(t('radarTrapMarkup', [formatSafeNumber(trap.fakeMarkupPct, { min: 0, max: 10000 }, uiLocale), formatVnd(trap.priceOriginal, uiLocale)]))}
- ${formatVnd(trap.priceCurrent)} + ${formatVnd(trap.priceCurrent, uiLocale)}
` ) @@ -287,9 +291,10 @@ async function fetchAndRenderMerchantTrustDirectory( ); }); + const uiLocale = t('langCode'); slot.innerHTML = `
-
${escapeHtml(t('merchantDirTitle', [formatSafeNumber(allMerchants.length, { min: 0 })]))}
+
${escapeHtml(t('merchantDirTitle', [formatSafeNumber(allMerchants.length, { min: 0 }, uiLocale)]))}
${sorted .slice(0, 5) .map((m) => { @@ -316,7 +321,7 @@ async function fetchAndRenderMerchantTrustDirectory( ${m.is_official ? officialBadgeHtml() : ''}
- ${escapeHtml(String(m.platform ?? '').toUpperCase())} (${escapeHtml(t('merchantProductCount', [formatSafeNumber(m.product_count, { min: 0 })]))}) - ${escapeHtml(t('merchantScamRisk', [riskScoreText(m.dosafe_risk_score)]))} | ${escapeHtml(t('merchantReviewRisk', [riskScoreText(m.review_risk_score)]))} + ${escapeHtml(String(m.platform ?? '').toUpperCase())} (${escapeHtml(t('merchantProductCount', [formatSafeNumber(m.product_count, { min: 0 }, uiLocale)]))}) - ${escapeHtml(t('merchantScamRisk', [riskScoreText(m.dosafe_risk_score)]))} | ${escapeHtml(t('merchantReviewRisk', [riskScoreText(m.review_risk_score)]))}
@@ -380,6 +385,7 @@ function renderPanelContent( return; } + const uiLocale = t('langCode'); const assessment = lookup.priceAssessment; // The verdict in the extension's language (DOS/Rate#1326). const verdict = verdictTexts(assessment); @@ -387,7 +393,7 @@ function renderPanelContent( const reviews = lookup.reviewSummary; const merchant = lookup.merchantProfile; - const variantButtonsHtml = renderVariantPickerHtml(lookup); + const variantButtonsHtml = renderVariantPickerHtml(lookup, uiLocale); const keySpecs = master?.key_specs; const keySpecsHtml = @@ -425,9 +431,9 @@ function renderPanelContent( const offersList = lookup.allOffers.length > 0 ? lookup.allOffers : lookup.topCheaperOffers; - const offersHtml = renderOffersCardHtml(offersList); + const offersHtml = renderOffersCardHtml(offersList, uiLocale); - const reviewHtml = renderReviewCardHtml(reviews, master); + const reviewHtml = renderReviewCardHtml(reviews, master, uiLocale); const merchantNoTrust = !merchant || @@ -440,7 +446,7 @@ function renderPanelContent(
${escapeHtml(t('merchantTrustScore'))}
-
${escapeHtml(merchantNoTrust ? t('noDataYet') : `${formatSafeNumber(merchant.trust_score, { min: 0, max: 100 })}/100`)}
+
${escapeHtml(merchantNoTrust ? t('noDataYet') : `${formatSafeNumber(merchant.trust_score, { min: 0, max: 100 }, uiLocale)}/100`)}
${escapeHtml(t('merchantRiskPair'))}
@@ -459,11 +465,11 @@ function renderPanelContent(
${escapeHtml(t('historyLow'))}
-
${formatVnd(lookup.priceHistory.lowestEverPrice)}
+
${formatVnd(lookup.priceHistory.lowestEverPrice, uiLocale)}
${escapeHtml(t('historyHigh'))}
-
${formatVnd(lookup.priceHistory.highestEverPrice)}
+
${formatVnd(lookup.priceHistory.highestEverPrice, uiLocale)}
@@ -512,11 +518,11 @@ function renderPanelContent(
${escapeHtml(t('leaderboardRank'))}
-
#${formatSafeNumber(overallBadge.rank, { min: 1 })} / ${formatSafeNumber(overallBadge.totalInCategory, { min: 1 })}
+
#${formatSafeNumber(overallBadge.rank, { min: 1 }, uiLocale)} / ${formatSafeNumber(overallBadge.totalInCategory, { min: 1 }, uiLocale)}
${escapeHtml(t('leaderboardScore'))}
-
${formatSafeNumber(overallBadge.score10, { min: 0, max: 10, decimals: 1 })} / 10
+
${formatSafeNumber(overallBadge.score10, { min: 0, max: 10, decimals: 1 }, uiLocale)} / 10
${ @@ -525,7 +531,7 @@ function renderPanelContent( ${subBadges .map( (sr) => - `#${formatSafeNumber(sr.rank, { min: 1 })} ${escapeHtml(focusLabel(sr.criterion))} (${formatSafeNumber(sr.score10, { min: 0, max: 10, decimals: 1 })})` + `#${formatSafeNumber(sr.rank, { min: 1 }, uiLocale)} ${escapeHtml(focusLabel(sr.criterion))} (${formatSafeNumber(sr.score10, { min: 0, max: 10, decimals: 1 }, uiLocale)})` ) .join('')}
` @@ -588,12 +594,12 @@ function renderPanelContent( ? // Only a 3+ offer floor is the multi-platform floor; fewer offers are // just below the median (DOS/Rate#1145). radarSig.genuineDeal?.verdict === 'genuine_all_time_low' - ? `${escapeHtml(t('radarPillDeal', [formatSafeNumber(radarSig.savingsPct, { min: 0, max: 100 })]))}` - : `${escapeHtml(t('radarPillBelowMedian', [formatSafeNumber(radarSig.savingsPct, { min: 0, max: 100 })]))}` + ? `${escapeHtml(t('radarPillDeal', [formatSafeNumber(radarSig.savingsPct, { min: 0, max: 100 }, uiLocale)]))}` + : `${escapeHtml(t('radarPillBelowMedian', [formatSafeNumber(radarSig.savingsPct, { min: 0, max: 100 }, uiLocale)]))}` : radarSig.verdict === 'fake_sale_trap' - ? `${escapeHtml(t('radarPillTrap', [formatSafeNumber(radarSig.fakeMarkupPct, { min: 0, max: 10000 })]))}` + ? `${escapeHtml(t('radarPillTrap', [formatSafeNumber(radarSig.fakeMarkupPct, { min: 0, max: 10000 }, uiLocale)]))}` : Number.isFinite(radarSpreadPct) && radarSpreadPct >= 5 - ? `${escapeHtml(t('radarPillSpread', [formatSafeNumber(radarSig.spreadPct, { min: 0, max: 10000 })]))}` + ? `${escapeHtml(t('radarPillSpread', [formatSafeNumber(radarSig.spreadPct, { min: 0, max: 10000 }, uiLocale)]))}` : '' : ''; @@ -626,11 +632,11 @@ function renderPanelContent(
${escapeHtml(t('observedPrice'))}
-
${formatVnd(assessment.observedPrice ?? signal.price)}
+
${formatVnd(assessment.observedPrice ?? signal.price, uiLocale)}
${escapeHtml(t('marketMedian'))}
-
${formatVnd(assessment.marketMedianPrice)}
+
${formatVnd(assessment.marketMedianPrice, uiLocale)}
${renderActionRowsHtml(actionLinks)} diff --git a/apps/extension/src/test-extension-pipeline.ts b/apps/extension/src/test-extension-pipeline.ts index 88d941b7..90a82646 100644 --- a/apps/extension/src/test-extension-pipeline.ts +++ b/apps/extension/src/test-extension-pipeline.ts @@ -10928,6 +10928,829 @@ async function runTests() { } } } + + // --- DOS/Rate#1839: Czech (cs) VND non-breaking-space grouping & comma-decimal formatting --- + { + const { + buildCrowdIngestPayload, + buildExtensionObservationV1, + buildLookupParams, + buildProductSignalSignature, + formatSafeExtensionVnd, + formatSafeNumber, + } = await import('./security-guards.js'); + const { + buildSvgSparkline, + ratingStarText, + renderLookupErrorHtml, + renderOffersCardHtml, + renderReviewCardHtml, + renderVariantPickerHtml, + riskScoreText, + trustText, + } = await import('./sidepanel/panel-view.js'); + const { verdictTexts } = await import('./verdict-display.js'); + const { formatCatalogueEntry } = await import('./i18n.js'); + const { formatDecimal, formatPriceVnd } = + await import('../../web/src/lib/locale-format.js'); + + const NBSP = '\u00A0'; + const catalogues = readLocaleCatalogues(); + + // 1. Direct helper checks across omitted, vi, en, and cs locales + parity with locale-format.ts. + for (const loc of [ + undefined, + 'vi', + 'vi-VN', + 'en', + 'en-US', + 'de', + ] as const) { + assert.equal( + formatSafeExtensionVnd(15_990_000, 'N/A', loc), + '15.990.000 ₫' + ); + assert.equal( + formatSafeExtensionVnd(15_990_000, 'N/A', loc), + formatPriceVnd( + 15_990_000, + loc === 'en' || loc === 'en-US' ? 'en' : 'vi' + ) + ); + assert.equal( + formatSafeNumber(4.5, { min: 0, max: 5, decimals: 1 }, loc), + '4.5' + ); + assert.equal( + formatSafeNumber(8.8, { min: 0, max: 10, decimals: 1 }, loc), + '8.8' + ); + assert.equal(formatSafeNumber(1250, { min: 0 }, loc), '1250'); + } + + for (const csLoc of ['cs', 'cs-CZ', 'CS_cz', ' cs '] as const) { + assert.equal( + formatSafeExtensionVnd(15_990_000, 'N/A', csLoc), + `15${NBSP}990${NBSP}000${NBSP}₫` + ); + assert.equal( + formatSafeExtensionVnd(15_990_000, 'N/A', csLoc), + formatPriceVnd(15_990_000, 'cs') + ); + assert.equal(formatSafeExtensionVnd(999, 'N/A', csLoc), `999${NBSP}₫`); + assert.equal( + formatSafeExtensionVnd(1_000_000_000, 'N/A', csLoc), + `1${NBSP}000${NBSP}000${NBSP}000${NBSP}₫` + ); + assert.equal( + formatSafeNumber(4.5, { min: 0, max: 5, decimals: 1 }, csLoc), + '4,5' + ); + assert.equal( + formatSafeNumber(4.5, { min: 0, max: 5, decimals: 1 }, csLoc), + formatDecimal(4.5, 'cs', 1) + ); + assert.equal( + formatSafeNumber(8.8, { min: 0, max: 10, decimals: 1 }, csLoc), + '8,8' + ); + assert.equal( + formatSafeNumber(8.8, { min: 0, max: 10, decimals: 1, locale: csLoc }), + '8,8' + ); + assert.equal( + formatSafeNumber(5, { min: 0, max: 5, decimals: 1 }, csLoc), + '5,0' + ); + assert.equal(formatSafeNumber(1250, { min: 0 }, csLoc), `1${NBSP}250`); + assert.equal( + formatSafeNumber(1250.5, { decimals: 1 }, csLoc), + `1${NBSP}250,5` + ); + assert.equal( + formatSafeNumber(-1250.5, { decimals: 1 }, csLoc), + `-1${NBSP}250,5` + ); + } + + // Invalid inputs still return fallback across locales. + for (const badVnd of [ + 0, + 0.2, + -5000, + Number.NaN, + Number.POSITIVE_INFINITY, + 2_000_000_000, + ]) { + assert.equal(formatSafeExtensionVnd(badVnd, 'N/A', 'cs'), 'N/A'); + assert.equal(formatSafeExtensionVnd(badVnd, 'N/A', 'vi'), 'N/A'); + } + assert.equal( + formatSafeNumber('