fix(environment): apply collection mappings to identifier lists (#12354)

* fix(environment): apply collection mappings to identifier lists

* test(environment): cover scoped identifier lists

* fix(environment): scope ID lists to owned response fields

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
ShuaixinHuangandmorluto authored and GitHub committed 2026-11-19 09:08:11 -05:00
1 parent 0fead85457
commit 0a55ccba71
3 files changed
+75 -20

No files matched your search

@@ -731,6 +731,47 @@ describe("comparison body coverage", () => {
});
describe("scalar sets of mapped ids", () => {
it("scopes identifier lists to their owning response entity", () => {
const map = new IdMap();
map.learn(101, 1, "/api/books");
map.learn(101, 2, "/api/authors");
const answer = (body: JsonValue) => ({
status: 200,
body: { kind: "json" as const, value: body },
});
const live = answer({
data: [{ ids: [101, "101"], books: { ids: [101] }, counts: [101] }],
books: { ids: [101] },
});
const entity = { scope: "/api/authors", at: "/data" };
const compared = compareAnswers(
live,
answer({
data: [{ ids: [2, "2"], books: { ids: [1] }, counts: [101] }],
books: { ids: [1] },
}),
map,
entity,
);
expect(compared.differences).toEqual([]);
expect(compared.normalised.ids).toBe(4);
const differs = compareAnswers(
live,
answer({
data: [{ ids: [2, "2"], books: { ids: [2] }, counts: [2] }],
books: { ids: [2] },
}),
map,
entity,
);
expect(differs.differences.map(({ pointer }) => pointer)).toEqual([
"/data/0/books/ids/0",
"/data/0/counts/0",
"/books/ids/0",
]);
});
it("differ only in order once live ids are mapped", () => {
const map = new IdMap();
map.learn(101, 1);
@@ -21,7 +21,7 @@ import {
IDENTITY_FIELD,
keyedByLearnedIds,
pairElements,
translateValue,
translateScalarId,
type IdMap,
} from "./environmentCompareIds.js";
import { isRecord } from "../../kernel/isRecord.js";
@@ -326,11 +326,16 @@ class Differ {
live: JsonValue[],
env: JsonValue[],
pointer: string,
clockKey: string | undefined,
at: {
readonly key?: string;
readonly own?: boolean;
readonly clockKey?: string | undefined;
},
generation: Generation | undefined,
): void {
const own = pointer === this.entity.at;
if (this.sameItems(live, env, pointer)) {
const clockKey = at.clockKey ?? at.key;
if (this.sameItems(live, env, pointer, at.key, own || at.own === true)) {
// Equal/reordered scalar sets still establish unchanged generated pairs;
// otherwise another field could split an alias hidden by this fast path.
if (generation !== undefined)
@@ -373,22 +378,28 @@ class Differ {
}
/** Arrays of scalars holding the same items in another order differ only in order, as sets an application returns unordered do. */
sameItems(live: JsonValue[], env: JsonValue[], pointer: string): boolean {
sameItems(
live: JsonValue[],
env: JsonValue[],
pointer: string,
field: string | undefined,
owned: boolean,
): boolean {
const scalar = (item: JsonValue): boolean =>
item === null || typeof item !== "object";
if (live.length !== env.length || !live.every(scalar) || !env.every(scalar))
return false;
// Only identifier lists or known string identifiers are translated; numeric measurements stay literal.
const field = pointer.slice(pointer.lastIndexOf("/") + 1);
const mapped = live.map((item) => {
if (
(typeof item === "string" && this.map.isStateReference(item, field)) ||
((typeof item === "number" || typeof item === "string") &&
ID_LIST_FIELD.test(field))
)
return translateValue(item, this.map);
return item;
});
const idList = field !== undefined && ID_LIST_FIELD.test(field);
const scope = idList && owned ? this.entity.scope : undefined;
const mapped = live.map((item) =>
idList ||
(typeof item === "string" &&
field !== undefined &&
this.map.isStateReference(item, field))
? translateScalarId(item, this.map, scope)
: item,
);
const key = (items: JsonValue[]): string =>
JSON.stringify(items.map((item) => JSON.stringify(item)).sort());
if (key(mapped) !== key(env)) return false;
@@ -515,7 +526,7 @@ class Differ {
generation?: Generation,
): void {
if (Array.isArray(live) && Array.isArray(env))
this.array(live, env, pointer, at.clockKey ?? at.key, generation);
this.array(live, env, pointer, at, generation);
else if (isRecord(live) && isRecord(env))
this.object(
live,
@@ -405,10 +405,13 @@ export const learnIds = (
);
/** A live identifier as the environment's value, in its own type; unchanged when unmapped. */
const inEnvTerms = (
value: string | number,
env: string | number | undefined,
export const translateScalarId = (
value: JsonValue,
map: IdMap,
scope?: string,
): JsonValue => {
if (!isScalarId(value)) return value;
const env = map.env(value, scope);
if (env === undefined) return value;
return typeof value === "string"
? String(env)
@@ -428,7 +431,7 @@ export const translateValue = (value: JsonValue, map: IdMap): JsonValue => {
translateValue(item, map),
]),
);
return isScalarId(value) ? inEnvTerms(value, map.env(value)) : value;
return translateScalarId(value, map);
};
/** A bulk write's body argument, how it names its entities, and the collection whose own mapping translates those ids. */
@@ -447,7 +450,7 @@ const translateBulkBody = (
): JsonValue => {
const { address, idField, scope } = bulk;
const id = (value: JsonValue): JsonValue =>
isScalarId(value) ? inEnvTerms(value, map.env(value, scope)) : value;
translateScalarId(value, map, scope);
const entries = (value: JsonValue): JsonValue =>
Array.isArray(value)
? value.map((entry) =>