Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(editor): a .esfsm the editor cannot read opens empty, not fatal

yhhb509fd9ad97e
diff --git a/desktop/src/fsm/FsmGraphDocument.ts b/desktop/src/fsm/FsmGraphDocument.ts
index c0d3c5fc2..5c1d54d36 100644
--- a/desktop/src/fsm/FsmGraphDocument.ts
+++ b/desktop/src/fsm/FsmGraphDocument.ts
@@ -15,8 +15,22 @@ class FsmGraphDocumentImpl extends AssetDocument<FsmDefinition> {
open(def: FsmDefinition, filePath: string | null): void {
this.openAsset(def, filePath);
}
+ /**
+ * Open whatever the file held, as a state machine.
+ *
+ * The cast used to be blind, so a `.esfsm` that was not one — hand-written,
+ * from another tool, half-migrated — reached the panel and crashed it on
+ * `def.states.map` with "Cannot read properties of undefined". A file the
+ * editor cannot read should open as an EMPTY machine you can see and repair,
+ * not take the panel down: the crash names neither the file nor the reason,
+ * and the panel is the only place to fix it from.
+ */
openJson(raw: unknown, filePath: string | null): void {
- this.open(raw as FsmDefinition, filePath);
+ const d = (raw ?? {}) as Partial<FsmDefinition>;
+ this.open({
+ initial: typeof d.initial === 'string' ? d.initial : '',
+ states: Array.isArray(d.states) ? d.states : [],
+ }, filePath);
}
close(): void {
this.closeAsset();
diff --git a/desktop/tests/fsm-document-open.test.ts b/desktop/tests/fsm-document-open.test.ts
new file mode 100644
index 000000000..4de720f57
--- /dev/null
+++ b/desktop/tests/fsm-document-open.test.ts
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file A `.esfsm` the editor cannot read opens empty, not fatal.
+ *
+ * From a dogfood run: an agent hand-wrote a state machine with the fields it
+ * guessed at, opened it, and the panel died on `def.states.map` with "Cannot
+ * read properties of undefined" — a message naming neither the file nor the
+ * reason, in the one place the file could have been repaired from.
+ */
+import { describe, it, expect, afterEach } from 'vitest';
+import { FsmGraphDocument } from '@/fsm/FsmGraphDocument';
+
+afterEach(() => FsmGraphDocument.close());
+
+describe('opening a .esfsm', () => {
+ it('keeps a well-formed machine exactly as it is', () => {
+ const def = { initial: 'Idle', states: [{ name: 'Idle', transitions: [] }] };
+ FsmGraphDocument.openJson(def, 'assets/ai.esfsm');
+ expect(FsmGraphDocument.asset?.initial).toBe('Idle');
+ expect(FsmGraphDocument.asset?.states).toHaveLength(1);
+ });
+
+ // The shape a person (or an agent) writes when guessing at the format.
+ it('opens a foreign document as an empty machine', () => {
+ FsmGraphDocument.openJson({ states: undefined, transitions: [], variables: {} }, 'assets/chess.esfsm');
+ expect(FsmGraphDocument.asset?.states).toEqual([]);
+ expect(FsmGraphDocument.asset?.initial).toBe('');
+ });
+
+ it('survives a file that holds nothing like an object', () => {
+ for (const raw of [null, undefined, 42, 'a string', []]) {
+ FsmGraphDocument.openJson(raw, 'assets/x.esfsm');
+ expect(Array.isArray(FsmGraphDocument.asset?.states)).toBe(true);
+ }
+ });
+});