fix(environment): preserve submillisecond insert ordering

Compare retained submillisecond components after milliseconds, so ordered collections recognize and place distinct timestamps correctly.

Fixes #6525
This commit is contained in:
morluto committed 2026-10-23 18:07:45 +08:00
1 parent 54c666a5ae
commit 18225b9bd6
2 files changed
+23 -1

No files matched your search

@@ -553,6 +553,7 @@ it.each<[JsonValue | undefined, JsonValue | undefined, number | undefined]>([
["10", 9, 1],
["7", 7, 0],
["2026-03-04T05:00:00Z", "2026-03-04T06:30:00+02:00", 1],
["2026-03-04T05:00:00.123000001Z", "2026-03-04T05:00:00.123000002Z", -1],
[Date.UTC(2026, 2, 4, 6) / 1000, "2026-03-04T05:00:00Z", 1],
["apple", "banana", -1],
["Zebra", "apple", -1],
@@ -583,6 +584,26 @@ it("offers each strictly ordered field in its direction, then last and first", (
expect(insertCandidates([], ["id"])).toEqual(fallback);
});
it("uses retained submillisecond precision to recognize and place sorted timestamps", () => {
const members = [
{ at: "2026-03-04T05:00:00.123000002Z" },
{ at: "2026-03-04T05:00:00.123000003Z" },
];
const rule: InsertRule = {
rule: "sorted",
field: "at",
direction: "ascending",
};
expect(insertCandidates(members, ["at"])).toEqual([
rule,
{ rule: "last" },
{ rule: "first" },
]);
expect(
insertIndex(rule, members, { at: "2026-03-04T05:00:00.123000001Z" }),
).toBe(0);
});
const ASCENDING = [{ n: 1 }, { n: 3 }, { n: 5 }];
const DESCENDING = [{ n: 5 }, { n: 3 }, { n: 1 }];
const up: InsertRule = { rule: "sorted", field: "n", direction: "ascending" };
@@ -431,7 +431,8 @@ export const compareValues = (
const [a, b] = [integerOf(left), integerOf(right)];
if (a !== undefined && b !== undefined) return a - b;
const [c, d] = [readClock(left), readClock(right)];
if (c !== undefined && d !== undefined) return c.ms - d.ms;
if (c !== undefined && d !== undefined)
return c.ms - d.ms || c.subMillisecond - d.subMillisecond;
return typeof left === "string" && typeof right === "string"
? compare(left, right)
: undefined;