Skip to content

test(e2e): regression guards for previously-reported "Unexpected error" crashes - #4268

Merged
Jocs merged 3 commits into
developfrom
test/regression-unexpected-error-crashes
May 23, 2026
Merged

Jocs merged 3 commits into
developfrom
test/regression-unexpected-error-crashes

Conversation

@Jocs

@Jocs Jocs commented May 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Triaged the 88 OPEN issues with the title prefix Unexpected error (gh issue list --search "Unexpected error in:title" --state open), grouped them by error signature, located each crash site in current code, and attempted to reproduce every reproducible category with Playwright user-action recipes only (no programmatic state hacks).

Empirical finding: on current develop (0.18.9), none of the historical user recipes still crashes. Cumulative changes from the electron-vite refactor (#4001) and subsequent fixes already closed the main crash surfaces:

Original crash Where the guard now lives
getMuyaIndexCursor null anchorBlock destructure (#4117/#4090/#4079/#4078) src/muya/lib/utils/importMarkdown.js:464-467 — `if (!anchorBlock
quickInsert.selectItem null block destructure (#2099/#3571/#3663/#3667/#3879) src/muya/lib/ui/quickInsert/index.js:160-164
CLOSE_TABS undefined-tab pathname destructure (#3100/#3551) src/renderer/src/store/editor.ts:1044 — ?? { pathname: '' }
bringToFront null window deref (#2065/#3321/#4137) src/main/windows/base.ts:85 early return
setCursorRange element-offset clamp (#4159 et al.) src/muya/lib/selection/index.js:524-530
selectionChange null cursor (#4160/#3942) src/muya/lib/contentState/paragraphCtrl.js:18-22 model-cursor fallback

Rather than add speculative defensive fixes that cannot be validated by a failing test, this PR encodes the historical crash recipes as regression-guard e2e specs. If any of these crash paths reopens, CI fails before another user files the same issue.

What's in the PR

3 new e2e specs under test/e2e/, totalling 14 tests:

Helper infra in test/e2e/helpers.ts:

  • Adds a LaunchOptions { suppressErrorDialog?: boolean } parameter on launchElectron / launchWithDoc / launchWithMarkdown. When opted in, sets MARKTEXT_ERROR_INTERACTION=1 so the main-process exception handler does not pop the modal "Unexpected error" dialog under Playwright (would deadlock the worker). Only the crash-guard specs opt in — existing specs keep getting the dialog if a renderer error leaks (no silent-regression risk).
  • Installs a second mt::handle-renderer-error IPC listener on ipcMain that accumulates errors into a shared global, plus getRendererErrors / clearRendererErrors / expectNoRendererErrors helpers.
  • A "crash counter sanity" test that forces a renderer-side throw and asserts it is captured — guards against the new assertion passing vacuously.

Test plan

  • pnpm run typecheck — passes
  • pnpm run lint — no new errors/warnings introduced by these files
  • pnpm run test:unit — 550 tests pass
  • pnpm exec playwright test test/e2e/crash-*.spec.ts — 14 / 14 pass
  • pnpm exec playwright test test/e2e/editor-input.spec.ts test/e2e/launch.spec.ts test/e2e/tabs.spec.ts — existing specs unaffected by the new env var / IPC listener

Out of scope

Issues that are not user-reproducible at all stay open for now:

🤖 Generated with Claude Code

…r" crashes

Adds three Playwright specs that exercise the user-described recipes from
~50 "Unexpected error" issues filed against shipped versions (v0.16.x through
v0.18.4). All recipes pass on current develop — the cumulative guards added
during the electron-vite refactor and subsequent fixes closed the original
crash surfaces. These specs become regression guards: if any of the
historical paths reopens, CI fails before another user files the same issue.

Covered categories (with issue numbers):
- "Failed to execute 'setStart' on 'Range': There is no child at offset N"
  via setCursorRange / inputHandler / enterHandler / paste
  (#4159 #4111 #4101 #4091 #4052 #4035 #4006 #4005 #4004 #3996 #3954
   #3936 #3929 #3886 #3829 #3793 #3790 #3771 #3737 #3727 #3636 #3542
   #3378 #2627 #2526)
- "Cannot destructure property 'text' of {block} as it is null"
  via updateParagraph
  (#2099 #3571 #3663 #3667 #3879)
- "selectionChange: expected cursor but cursor is null"
  via paragraphCtrl
  (#4160 #3942)

Infra additions in test/e2e/helpers.ts:
- `MARKTEXT_ERROR_INTERACTION=1` in the launch env so the main process
  exception handler does not pop the modal "Unexpected error" dialog under
  Playwright (which would deadlock the test worker).
- A parallel `mt::handle-renderer-error` IPC listener stored in a shared
  global, with `getRendererErrors` / `clearRendererErrors` /
  `expectNoRendererErrors` helpers.
- A "crash counter sanity" test that forces a renderer-side throw and
  asserts it is captured, so the assertion above never passes vacuously.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings May 23, 2026 07:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds Playwright E2E regression tests to guard against several historically frequent “Unexpected error” crash signatures by asserting that specific user-action recipes no longer trigger renderer error handling, and it extends the E2E helper infrastructure to suppress the blocking modal error dialog while capturing renderer errors via IPC.

Changes:

  • Add a renderer-error capture mechanism in test/e2e/helpers.ts (IPC listener + helpers) and disable the modal error dialog via MARKTEXT_ERROR_INTERACTION=1.
  • Add 3 new E2E spec files covering historical crash recipes around range offsets, paragraph updates, and selection-change edge cases.
  • Add a sanity test intended to verify that the new crash counter is actually wired up.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.

File Description
test/e2e/helpers.ts Sets an env var to suppress modal error dialogs during tests and installs IPC-based renderer error capture helpers.
test/e2e/crash-range-offset.spec.ts Adds regression recipes for historical Range.setStart offset crashes + a crash-counter sanity check.
test/e2e/crash-update-paragraph.spec.ts Adds regression recipes for updateParagraph-related null block crashes (quick insert + menu paths).
test/e2e/crash-selection-change.spec.ts Adds regression recipes for selectionChange null-cursor crashes + a white-box behavior probe.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/e2e/crash-update-paragraph.spec.ts Resolved
Comment thread test/e2e/crash-update-paragraph.spec.ts Resolved
Comment thread test/e2e/crash-update-paragraph.spec.ts Resolved
Comment thread test/e2e/crash-selection-change.spec.ts Resolved
Comment thread test/e2e/crash-selection-change.spec.ts Resolved
Comment thread test/e2e/crash-selection-change.spec.ts Resolved
Comment thread test/e2e/crash-range-offset.spec.ts Resolved
@github-actions

github-actions Bot commented May 23, 2026 •

Copy link
Copy Markdown
Contributor

Build artifacts for PR #4268:

Run: https://github.com/marktext/marktext/actions/runs/26328824786

Artifact Size Link
marktext-linux 594.6 MB Download
marktext-macos-x64 272.5 MB Download
marktext-windows 272.8 MB Download
marktext-macos-arm64 262.3 MB Download

Quick-insert + menu paths:
- Fix the quick-insert selector to use data-label="heading 1" (lowercase, per
  src/muya/lib/ui/quickInsert/config.js) instead of the made-up "header 1" /
  localized text fallback. The previous selector silently no-op'd in any
  non-English locale and let the spec pass without exercising the recipe.
- Stop swallowing clickMenuById failures in three places. If the menu id is
  wrong or the menu is not ready, the test must fail loudly rather than
  vacuously asserting "no crash" without invoking the crash surface.
- Add a waitForMenuReady() call to every beforeEach so menu invocation is
  deterministic.
- The "rapid alternation" test now asserts that some heading/paragraph
  structure exists, proving the menu clicks actually mutated state.

Selection-change paths:
- The "Blur the editor..." test now actually blurs document.activeElement
  in addition to clearing the DOM selection (the original recipe).
- The "Repeated focus/blur" test no longer references "menu invocation" in
  its name — renamed to "Repeated DOM selection clear + refocus" so the
  name matches the surface it exercises.
- The white-box selectionChange probe now uses test.skip() when muya is not
  reachable on `window`, and a hard `expect('threw')` when it is. Replaces
  the previous toContain([allValues]) which was a no-op.

Sanity helper:
- Replace fixed waitForTimeout(300) in the sanity test with a new
  waitForRendererError() helper that polls getRendererErrors with a 5s
  deadline. Slower CI runners no longer race the timeout.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
This was referenced May 23, 2026
This was referenced May 23, 2026
@Jocs
Jocs requested a review from Copilot May 23, 2026 08:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

Comment thread test/e2e/helpers.ts Outdated
Comment on lines 66 to 70
// MARKTEXT_ERROR_INTERACTION suppresses the modal "Unexpected error" dialog
// (see src/main/exceptionHandler.ts) — we instead capture errors via the
// mt::handle-renderer-error IPC below so specs can assert on them.
env: { ...process.env, PERF_TESTING: 'true', MARKTEXT_ERROR_INTERACTION: '1' },
timeout: 30000
Comment thread test/e2e/crash-range-offset.spec.ts Outdated
// The user then clicks on / highlights the text. We simulate by selecting
// all and then collapsing to caret to provoke setCursorRange across the
// mixed inline DOM the paragraph now contains.
await page.keyboard.press('Control+A')
Comment thread test/e2e/crash-range-offset.spec.ts Outdated
await placeCaretInEditor(page)
await clearRendererErrors(app)

await page.keyboard.press('Control+A')
Comment thread test/e2e/crash-update-paragraph.spec.ts Outdated
Comment on lines +32 to +38
test.beforeEach(async() => {
const launched = await launchWithMarkdown('# Doc\n\nFirst para.\n\nSecond para.\n')
app = launched.app
page = launched.page
await waitForMenuReady(app)
await placeCaretInEditor(page)
await clearRendererErrors(app)
Comment thread test/e2e/crash-selection-change.spec.ts Outdated
Comment on lines +75 to +113

// White-box probe: directly invoke the contentState API to confirm the throw
// is still present in the source. The renderer does not expose `muya` on
// `window`, so we walk the editor component to find the instance via a known
// property if available. If the probe cannot reach muya in this environment
// it is skipped rather than passing vacuously.
test.describe('selectionChange API behaviour', () => {
test('Calling selectionChange with null cursor throws (white-box)', async() => {
const { app, page } = await launchWithMarkdown('# Doc\n')
try {
await page.waitForTimeout(300)
const outcome = await page.evaluate(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const anyWin = window as any
const muya = anyWin.__MUYA__ || anyWin.muya
if (!muya || !muya.contentState) return 'no-muya'
try {
muya.contentState.cursor = null
muya.contentState.selectionChange(null)
return 'did-not-throw'
} catch {
return 'threw'
}
})
// Muya is not currently exposed on `window` — this probe is
// informational and is skipped when it cannot reach the instance.
// If a future change starts exposing muya, this guard switches from
// skip to a hard assertion that null-cursor selectionChange still
// throws (which is the current documented behaviour).
if (outcome === 'no-muya') {
test.skip(true, 'muya instance is not exposed on window in this build')
} else {
expect(outcome).toBe('threw')
}
} finally {
await app.close()
}
})
})
helpers.ts:
- Make MARKTEXT_ERROR_INTERACTION opt-in via a new LaunchOptions
  { suppressErrorDialog?: boolean } parameter on launchElectron,
  launchWithDoc, launchWithMarkdown. Default behaviour is unchanged for
  existing specs — they keep getting the dialog if a renderer error
  leaks. Only the 3 crash-guard specs opt in (they install the IPC
  counter and assert on it explicitly). Removes the silent-regression
  risk Copilot flagged: existing specs would otherwise silently ignore
  renderer exceptions that previously surfaced as a dialog.

crash-range-offset.spec.ts:
- Replace `Control+A` with `ControlOrMeta+A` in both Select-All recipes
  so the spec is portable to macOS (where Select All is Cmd+A).
- Opt into suppressErrorDialog for the two describe blocks and the
  sanity test.

crash-update-paragraph.spec.ts:
- The #2099 recipe is "type @ in a *fresh* file" but beforeEach was
  seeding with `# Doc\n\nFirst para.\n\nSecond para.\n`. Refactor each
  test to own its own launch via a small launchAndReady() helper so
  the #2099 test can use the empty seed the report actually describes.

crash-selection-change.spec.ts:
- Remove the always-skipped white-box probe — it always hit the
  `no-muya` branch (Muya is not exposed on `window` anywhere in src/)
  and was effectively dead code that added noise without guarding
  any behaviour.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@Jocs
Jocs merged commit b104cfa into develop May 23, 2026
9 checks passed
@Jocs
Jocs deleted the test/regression-unexpected-error-crashes branch May 23, 2026 09:37
thimbleberrysystems pushed a commit to thimbleberrysystems/WordBird that referenced this pull request Jun 21, 2026
…r" crashes (marktext#4268)

* test(e2e): regression guards for previously-reported "Unexpected error" crashes

Adds three Playwright specs that exercise the user-described recipes from
~50 "Unexpected error" issues filed against shipped versions (v0.16.x through
v0.18.4). All recipes pass on current develop — the cumulative guards added
during the electron-vite refactor and subsequent fixes closed the original
crash surfaces. These specs become regression guards: if any of the
historical paths reopens, CI fails before another user files the same issue.

Covered categories (with issue numbers):
- "Failed to execute 'setStart' on 'Range': There is no child at offset N"
  via setCursorRange / inputHandler / enterHandler / paste
  (marktext#4159 marktext#4111 marktext#4101 marktext#4091 marktext#4052 marktext#4035 marktext#4006 marktext#4005 marktext#4004 marktext#3996 marktext#3954
   marktext#3936 marktext#3929 marktext#3886 marktext#3829 marktext#3793 marktext#3790 marktext#3771 marktext#3737 marktext#3727 marktext#3636 marktext#3542
   marktext#3378 marktext#2627 marktext#2526)
- "Cannot destructure property 'text' of {block} as it is null"
  via updateParagraph
  (marktext#2099 marktext#3571 marktext#3663 marktext#3667 marktext#3879)
- "selectionChange: expected cursor but cursor is null"
  via paragraphCtrl
  (marktext#4160 marktext#3942)

Infra additions in test/e2e/helpers.ts:
- `MARKTEXT_ERROR_INTERACTION=1` in the launch env so the main process
  exception handler does not pop the modal "Unexpected error" dialog under
  Playwright (which would deadlock the test worker).
- A parallel `mt::handle-renderer-error` IPC listener stored in a shared
  global, with `getRendererErrors` / `clearRendererErrors` /
  `expectNoRendererErrors` helpers.
- A "crash counter sanity" test that forces a renderer-side throw and
  asserts it is captured, so the assertion above never passes vacuously.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* test(e2e): address Copilot review on PR marktext#4268

Quick-insert + menu paths:
- Fix the quick-insert selector to use data-label="heading 1" (lowercase, per
  src/muya/lib/ui/quickInsert/config.js) instead of the made-up "header 1" /
  localized text fallback. The previous selector silently no-op'd in any
  non-English locale and let the spec pass without exercising the recipe.
- Stop swallowing clickMenuById failures in three places. If the menu id is
  wrong or the menu is not ready, the test must fail loudly rather than
  vacuously asserting "no crash" without invoking the crash surface.
- Add a waitForMenuReady() call to every beforeEach so menu invocation is
  deterministic.
- The "rapid alternation" test now asserts that some heading/paragraph
  structure exists, proving the menu clicks actually mutated state.

Selection-change paths:
- The "Blur the editor..." test now actually blurs document.activeElement
  in addition to clearing the DOM selection (the original recipe).
- The "Repeated focus/blur" test no longer references "menu invocation" in
  its name — renamed to "Repeated DOM selection clear + refocus" so the
  name matches the surface it exercises.
- The white-box selectionChange probe now uses test.skip() when muya is not
  reachable on `window`, and a hard `expect('threw')` when it is. Replaces
  the previous toContain([allValues]) which was a no-op.

Sanity helper:
- Replace fixed waitForTimeout(300) in the sanity test with a new
  waitForRendererError() helper that polls getRendererErrors with a 5s
  deadline. Slower CI runners no longer race the timeout.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* test(e2e): address second-round Copilot review on PR marktext#4268

helpers.ts:
- Make MARKTEXT_ERROR_INTERACTION opt-in via a new LaunchOptions
  { suppressErrorDialog?: boolean } parameter on launchElectron,
  launchWithDoc, launchWithMarkdown. Default behaviour is unchanged for
  existing specs — they keep getting the dialog if a renderer error
  leaks. Only the 3 crash-guard specs opt in (they install the IPC
  counter and assert on it explicitly). Removes the silent-regression
  risk Copilot flagged: existing specs would otherwise silently ignore
  renderer exceptions that previously surfaced as a dialog.

crash-range-offset.spec.ts:
- Replace `Control+A` with `ControlOrMeta+A` in both Select-All recipes
  so the spec is portable to macOS (where Select All is Cmd+A).
- Opt into suppressErrorDialog for the two describe blocks and the
  sanity test.

crash-update-paragraph.spec.ts:
- The marktext#2099 recipe is "type @ in a *fresh* file" but beforeEach was
  seeding with `# Doc\n\nFirst para.\n\nSecond para.\n`. Refactor each
  test to own its own launch via a small launchAndReady() helper so
  the marktext#2099 test can use the empty seed the report actually describes.

crash-selection-change.spec.ts:
- Remove the always-skipped white-box probe — it always hit the
  `no-muya` branch (Muya is not exposed on `window` anywhere in src/)
  and was effectively dead code that added noise without guarding
  any behaviour.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants