Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:AFK surf Open agent E2E Deterministic Id Generator

From Leeroopedia
Knowledge Sources
Domains Testing, Debugging
Last Updated 2026-10-25 00:00 GMT

Overview

Use a deterministic auto-increment id generator for Playwright E2E rooms (`room=playwright*`) and nanoid otherwise, so snapshot/order assertions stay stable across test runs while production runs get collision-resistant random ids.

Description

BlockSuite lets you supply an `idGenerator` when constructing a `TestWorkspace`. The playground starter detects the E2E case via the `?room=` URL param: when it starts with `playwright`, it switches from the default `nanoid` (random, non-deterministic) to `createAutoIncrementIdGenerator()` (1, 2, 3, …). Deterministic ids make snapshot comparisons and DOM selectors stable in Playwright suites; production gets the safer random ids.

Usage

Apply this heuristic when authoring E2E tests against the editor and you need stable block ids for snapshot matching or selectors. Use a `?room=playwright*` URL so the playground switches to auto-increment ids automatically.

The Insight (Rule of Thumb)

  • Action: Branch on the E2E marker: `const idGenerator = isE2E ? createAutoIncrementIdGenerator() : nanoid;`
  • Value: E2E → `createAutoIncrementIdGenerator()`; otherwise → `nanoid`.
  • Trade-off: Auto-increment ids are predictable (bad for security/multi-client collision) — only safe in single-client test contexts.
  • Compatibility: `createAutoIncrementIdGenerator` and `nanoid` both satisfy the `idGenerator` option type on `DocCollectionOptions`.

Reasoning

Random ids (`nanoid`) change every run, so any snapshot test or selector that embeds an id will flake. Auto-increment ids are stable within a fresh store, so snapshots match across runs and tests can target blocks by predictable ids. The switch is gated on the E2E room marker so production users always get the secure, collision-resistant `nanoid`.

Code Evidence

E2E detection and id generator selection from `blocksuite/playground/apps/starter/utils/collection.ts:21-32`:

const params = new URLSearchParams(location.search);
const room = params.get('room');
const isE2E = room?.startsWith('playwright');

export function createStarterDocCollection(
  storeExtensionManager: StoreExtensionManager
) {
  ...
  const idGenerator = isE2E ? createAutoIncrementIdGenerator() : nanoid;

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment