chore: harden local schema hints and tests

This commit is contained in:
Ryan Di
2026-03-20 19:33:01 +11:00
parent eda7c8d6e9
commit 8168d46f87
4 changed files with 80 additions and 9 deletions
+40
View File
@@ -3,6 +3,7 @@ import { DEFAULT_ELEMENT_PROPS } from "@excalidraw/common";
import { API } from "../tests/helpers/api";
import {
ALL_SCOPES,
hasElementSchemaVersion,
type SchemaMigration,
migrateAPIElements,
migrateClipboardElements,
@@ -179,6 +180,34 @@ describe("schema migration", () => {
expect(migrated[0].backgroundColor).toBe("#ff0000");
});
it("should migrate mixed-hint elements individually when payload schema is missing", () => {
const legacyFrame = API.createElement({
type: "frame",
backgroundColor: "#ff0000",
});
const modernFrame = API.createElement({
type: "frame",
backgroundColor: "#00ff00",
});
const modernFrameWithHint = {
...modernFrame,
schemaVersion: SCHEMA_VERSIONS.latest,
} as typeof modernFrame & { schemaVersion: number };
const migrated = migrateSceneElements(
[legacyFrame, modernFrameWithHint],
{
payloadSchemaVersion: undefined,
fallbackVersion: SCHEMA_VERSIONS.initial,
},
)!;
expect(migrated[0].backgroundColor).toBe(
DEFAULT_ELEMENT_PROPS.backgroundColor,
);
expect(migrated[1].backgroundColor).toBe("#00ff00");
});
it("should prefer payload schema over per-element schema", () => {
const frame = API.createElement({
type: "frame",
@@ -198,4 +227,15 @@ describe("schema migration", () => {
DEFAULT_ELEMENT_PROPS.backgroundColor,
);
});
it("should detect schema hints on elements", () => {
const frame = API.createElement({ type: "frame" });
const withHint = {
...frame,
schemaVersion: SCHEMA_VERSIONS.latest,
} as typeof frame & { schemaVersion: number };
expect(hasElementSchemaVersion([frame])).toBe(false);
expect(hasElementSchemaVersion([withHint])).toBe(true);
});
});
+9
View File
@@ -85,6 +85,15 @@ const isValidSchemaVersion = (
);
};
export const hasElementSchemaVersion = (
elements: readonly ExcalidrawElement[] | null | undefined,
) =>
!!elements?.some((element) =>
isValidSchemaVersion(
(element as ExcalidrawElement & { schemaVersion?: number }).schemaVersion,
),
);
export const validateSchemaMigrations = (
migrations: readonly SchemaMigration[],
) => {