From b1d1dad94898cb6533c2e1d3d16715fdc8f7d8ab Mon Sep 17 00:00:00 2001 From: Siarhei Date: Fri, 11 Sep 2026 14:30:40 +0200 Subject: [PATCH] test(translator): make nine fragment assertions fail when nothing is collected MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An audit of the suite replaced `collectInlineFragments` with an implementation that silently returns `[]` and ran the tests. Eleven of the forty-nine passed. The mechanism was the same in every case: the assertion reached the value through an optional chain, or ran a predicate over an array that was empty, so it was satisfied by the absence of a result. const fragment = collectInlineFragments(root)[0]?.fragments[0]; expect(fragment?.top).toBe(fragment?.node); // undefined === undefined expect(containers.every((c) => c.skip !== undefined)).toBe(true); // [] → true expect(containers.some((c) => c.node === link)).toBe(false); // [] → false expect(collectInlineFragments(root)[0]?.skip).toBeUndefined(); // undefined The worst of them was the check that a multi-leaf wrapper's `top` is a copy rather than the wrapper from the tree — the distinction the whole module exists for, passing against a collector that collected nothing. Each now asserts that the container or fragment exists before asserting anything about it. Re-running the same audit: eleven survivors down to two, and those two are `toEqual([])` on an empty root and on a node with no text children — assertions whose whole content is the absence, which is their contract clause rather than an oversight. Also drops the three `any`s from the `createNode` helper, which the repo's lint does not flag but the project forbids. Verification: 1469 unit tests, unchanged in count and all green; check-types clean; lint 58 warnings and 0 errors, identical to main. --- .../lexical/collectInlineFragments.test.ts | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/packages/payload-plugin-translator/src/core/kernel/lexical/collectInlineFragments.test.ts b/packages/payload-plugin-translator/src/core/kernel/lexical/collectInlineFragments.test.ts index cdbf6e0d..33bf57bd 100644 --- a/packages/payload-plugin-translator/src/core/kernel/lexical/collectInlineFragments.test.ts +++ b/packages/payload-plugin-translator/src/core/kernel/lexical/collectInlineFragments.test.ts @@ -1,8 +1,13 @@ import { describe, it, expect } from "vitest"; import { collectInlineFragments } from "./collectInlineFragments"; +import type { SerializedLexicalNode } from "./types"; -const createNode = (type: string, props?: Record, children?: any[]) => - ({ type, ...props, ...(children && { children }) }) as any; +const createNode = ( + type: string, + props?: Record, + children?: unknown[] +): SerializedLexicalNode => + ({ type, ...props, ...(children && { children }) }) as SerializedLexicalNode; const childrenOf = (node: unknown): unknown[] => ((node as { children?: unknown[] }).children ?? []) as unknown[]; @@ -43,6 +48,7 @@ describe("collectInlineFragments", () => { const containers = collectInlineFragments(root); + expect(containers).toHaveLength(1); expect(containers.some((container) => container.node === link)).toBe(false); }); @@ -146,9 +152,10 @@ describe("collectInlineFragments", () => { ]), ]); - expect(collectInlineFragments(root).every((container) => container.skip !== undefined)).toBe( - true - ); + const containers = collectInlineFragments(root); + + expect(containers).toHaveLength(2); + expect(containers.every((container) => container.skip !== undefined)).toBe(true); }); }); @@ -221,6 +228,7 @@ describe("collectInlineFragments", () => { const fragment = collectInlineFragments(root)[0]?.fragments[0]; + expect(fragment).toBeDefined(); expect(fragment?.top).toBe(fragment?.node); }); @@ -278,8 +286,10 @@ describe("collectInlineFragments", () => { it("gives a multi-leaf wrapper's fragment a copy of the wrapper as top, not the wrapper", () => { const { root, link } = linkWithTwoLeaves(); + const fragment = collectInlineFragments(root)[0]?.fragments[1]; - expect(collectInlineFragments(root)[0]?.fragments[1]?.top).not.toBe(link); + expect(fragment).toBeDefined(); + expect(fragment?.top).not.toBe(link); }); it("gives each leaf of a multi-leaf wrapper its own separate copy", () => { @@ -378,6 +388,7 @@ describe("collectInlineFragments", () => { const fragments = collectInlineFragments(root)[0]?.fragments ?? []; + expect(fragments).toHaveLength(2); expect(fragments.some((piece) => piece.node === space || piece.top === space)).toBe(false); }); @@ -502,7 +513,10 @@ describe("collectInlineFragments", () => { ]), ]); - expect(collectInlineFragments(root)[0]?.skip).toBeUndefined(); + const containers = collectInlineFragments(root); + + expect(containers).toHaveLength(1); + expect(containers[0]?.skip).toBeUndefined(); }); it("mark-shaped: a comparison like 5 < 10 is not mark-shaped", () => { @@ -515,7 +529,10 @@ describe("collectInlineFragments", () => { ]), ]); - expect(collectInlineFragments(root)[0]?.skip).toBeUndefined(); + const containers = collectInlineFragments(root); + + expect(containers).toHaveLength(1); + expect(containers[0]?.skip).toBeUndefined(); }); it("skips a container that is one plain text leaf", () => { @@ -536,7 +553,10 @@ describe("collectInlineFragments", () => { ]), ]); - expect(collectInlineFragments(root)[0]?.skip).toBeUndefined(); + const containers = collectInlineFragments(root); + + expect(containers).toHaveLength(1); + expect(containers[0]?.skip).toBeUndefined(); }); }); @@ -579,9 +599,7 @@ describe("collectInlineFragments", () => { // Length assertion as above: without it a collector returning [] would pass this test. expect(containers[0]?.fragments).toHaveLength(3); - expect(containers[0]?.fragments[1]?.top).not.toBe( - (root as { children: { children: unknown[] }[] }).children[0]?.children[1] - ); + expect(containers[0]?.fragments[1]?.top).not.toBe(childrenOf(childrenOf(root)[0])[1]); expect(root).toEqual(before); }); }); @@ -613,7 +631,10 @@ describe("collectInlineFragments", () => { ]), ]); - expect(collectInlineFragments(root)[0]?.skip).toBeUndefined(); + const containers = collectInlineFragments(root); + + expect(containers).toHaveLength(1); + expect(containers[0]?.skip).toBeUndefined(); }); }); });