TidGi-Desktop/features/stepDefinitions/browserView.ts
lin onetwo ee5b48c32a
Fix/misc bug1 (#697)
* fix: move useState/useEffect before conditional early return to fix hooks order error in search mode

* feat: hide friendLinks section from preferences

* feat: add search entry in preferences sidebar that focuses the search input

* feat: remove friendLinks section entirely and split Preference.Search i18n key

* feat: add search entry in workspace sidebar, fix search section title key

* fix: include custom items in search and render info cards in search results

* feat: add missing items to sync/externalAPI/aiAgent schemas for search coverage

* feat: add EmbeddingSection, wire workspace search section, remove old search result views

* fix: apply dark/light palette before serving getIndex to fix startup theme race

* feat: add receiveBundleAndFetch for bundle-based mobile push

* track latest tiddlywiki5 prerelease

* Revert "fix: apply dark/light palette before serving getIndex to fix startup theme race"

This reverts commit e1c096439d.

* Reimplement "fix: apply dark/light palette before serving getIndex to fix startup theme race"

This reverts commit e1c096439d.

* feat: expose runGitCommand, writeTempGitFile, deleteTempGitFile for plugins

Allow TiddlyWiki plugins (like tw-mobile-sync) to run arbitrary git commands
and manage temp files in the .git directory. This reduces the need to rebuild
TidGi Desktop when changing git-related plugin behavior.

Methods include path traversal protection for writeTempGitFile/deleteTempGitFile.

* refactor: replace receiveBundleAndFetch/mergeAfterPush with generic methods

- Removed receiveBundleAndFetch (logic moved to tw-mobile-sync plugin)
- Removed mergeAfterPush (logic moved to tw-mobile-sync plugin)
- Added readWorkspaceFile and writeWorkspaceFile for plugin file I/O
- runGitCommand, writeTempGitFile, deleteTempGitFile remain for plugin git operations
- All mobile-sync-specific logic now lives in the tw-mobile-sync plugin

* test(e2e): deduplicate browser-view palette steps and merge palette scenarios

* test(e2e): isolate palette outline examples by name

* test(e2e): decouple smoke logging assertion from worker lifecycle

* fix: persist workspace immediately on create to survive fast app close

Workspace.create() now calls set() with immediate=true so the new
workspace record is flushed to settings.json synchronously, before any
before-quit handler runs. Previously, if the app was closed while git
init was still running (but after workspace.create had returned), the
workspace record would only be in memory and would be lost on the next
launch, causing Find 0 existing wiki workspaces and a duplicate
default-wiki creation attempt.

Also move applyInitialPaletteBeforeIndexRender from startNodeJSWiki to
IpcServerRoutes.getIndex so the palette is applied on every index
render instead of only at server startup, and thread shouldUseDarkColors
through ipcServerRoutes.setConfig for the same reason.

Rename args param to gitArguments in GitServerService.runGitCommand to
avoid shadowing the outer variable and fix the ESLint no-shadow warning.

* test(e2e): fix browser-view step and wiki-worker restart timing

browserView.ts: replace fixed 10-attempt backoff with a polling loop
that fills the full 25 s Cucumber step budget (210 fast polls), because
WebContentsView creation is async and each failed poll returns in < 1 ms
(no webContents yet), making the old 10-attempt window too narrow.

wiki.ts: replace WIKI_WORKER_STARTED wait before restart with
VIEW_LOADED wait. WIKI_WORKER_STARTED is only written on the direct
startWiki() path; when the app starts via restartWorkspaceViewService
the marker is never emitted. VIEW_LOADED fires after did-stop-loading
and is a reliable proxy that the wiki worker is running and the view is
ready.

application.ts: add launchEnvOverrides map to ApplicationWorld and
thread it into the Electron launch env so scenarios can inject test-only
env vars (e.g. TIDGI_E2E_MOCK_SYSTEM_PALETTE) without modifying the
shared process environment.

* test(e2e): fix @wiki scenario timing and assertions

defaultWiki.feature:
- Explicitly click workspace button before asserting browser-view in
  Background; without a click the WebContentsView is never activated.
- Remove the VIEW_LOADED wait after workspace creation (redundant now
  that browser-view step polls for the full step budget).
- Replace the fragile 'Test content for lazy-all' text assertion with a
  structural check (open tiddler + confirm element exists); in lazy-all
  mode TiddlyWiki renders tiddlers lazily so the body text is often the
  sidebar/chrome rather than the tiddler body at assertion time.
- Remove the 'move back to wiki-test' tail of the move-workspace
  scenario; test-artifacts are wiped before every run so there is no
  need to restore the original location.

simplifiedWiki.feature:
- Wait for WORKSPACE_CREATED log marker before closing the app on first
  launch; git-init inside initWikiGitTransaction can take several
  seconds, and closing too early meant workspace.create() never ran,
  leaving settings.json with no wiki workspace on the second launch.

windowRestore.feature:
- Replace the unreliable WIKI_WORKER_STARTED + VIEW_LOADED wait in
  Background with WORKSPACE_CREATED + 'browser view should be loaded
  and visible'; the former marker is not emitted on the
  restartWorkspaceViewService path used at app startup.

* chore: update pnpm-lock and template/wiki submodule

* Update pnpm-lock.yaml

* test(e2e): remove unused browser view retry constants

* test(e2e): stabilize subwiki update and worker readiness wait

* fix: enforce LF line endings in .gitattributes to match .editorconfig

* feat(e2e): implement CPU-based dynamic timeout scaling

Add cpuBenchmark.ts to measure CPU performance at suite startup and derive
a performance multiplier (≥1.0) that scales all E2E timeouts. Fast machines
get tight timeouts for quick bug detection; slow machines get the room they
need without false timeout failures.

- cpuBenchmark.ts: Run ~200ms pure CPU workload, compare to reference (120ms)
- Local dev: enforce ≥1.8× floor (git I/O overhead not reflected in CPU score)
- CI: keep 1.0× (dedicated runners with known performance)
- timeouts.ts: Apply multiplier to CUCUMBER_GLOBAL_TIMEOUT and derived values
- Print diagnostic: [Timeout Config] multiplier=1.80× step budget=45000 ms

* fix(e2e): fix WorkerServicesReady marker search pattern

The log marker is written as 'test-id-WorkerServicesReady' (without brackets)
but tests were searching for '[test-id-WorkerServicesReady]' (with brackets).
ANSI color codes in logs further broke the pattern match.

Also implement proper marker clearing: wait for initial readiness, restart,
clear stale markers, then wait for fresh markers after restart.

Fixes 3 failing scenarios:
- AI button in Git Log window uses AI-generated commit message
- Plain backup button in Git Log window uses default message
- Move workspace to a new location

* fix(e2e): simplify isWikiRunning check to avoid TypeScript assertion in executeJavaScript

Remove TypeScript type assertion from executeJavaScript string code,
use runtime property checks instead to avoid script execution failure.

* Revert "fix(e2e): fix WorkerServicesReady marker search pattern"

This reverts commit 39f91256092d4ce402eb87b9b70d08c40a6d13fb.

* feat(e2e): increase MIN_MULTIPLIER to 2.0× for local dev

Increase from 1.8× to 2.0× to provide 100% timing cushion for slow
machines. This gives 50s timeout budget (25s base × 2.0) which should
be sufficient for browser view loading and wiki worker operations.

* fix: restore lazy AI deleted test. don't delete test, otherwise I will add it back

* feat(e2e): auto-calibrate timeout using @smoke scenario

Remove MIN_MULTIPLIER hack and synthetic CPU benchmark. Instead:
- @smoke scenario automatically measures real E2E performance
- Hooks record duration and calculate multiplier vs CI baseline
- Pure measurement-based scaling, no hardcoded floors
- Fallback to 3.0× only if smoke hasn't run yet

This captures the full stack (CPU, I/O, Electron startup, rendering) in
one real-world measurement, eliminating guesswork. No separate calibration
command needed - just run tests normally.

* fix(e2e): exclude test files from cucumber require pattern

* fix: should save $:/layout

* chore: update 5.4.0 prerelease

* Update test.yml

* fix: Some repo is clone from github, use https to avoid ssh key issue

delete the lock file and pnpm i regenerate fix this

* lint

* fix(ci): restore search section and precompute E2E timeout calibration

Restore the missing search preferences section so schema validation passes in CI.
Also move E2E timeout calibration ahead of cucumber startup so timeout values
are computed before step modules freeze their constants. Use the smoke scenario
as a real-world baseline and derive a heavier timeout family for restart and
browser-view operations that are dominated by I/O and Electron lifecycle work
on slow machines.

* fix(sync): restore mobile merge-after-push hook

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* test(e2e): stabilize selector waits and calibration state

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* test(e2e): remove invalid pre-restart worker wait

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* fix(preferences): remove global embedding section

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* test(e2e): move vector search flow to workspace settings

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* fix(vector-search): validate sqlite row ids before storing embeddings

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* feat(git-log): add multi-select batch actions

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>

* fix(git-log): support multi-commit undo, fix path traversal, fix search l10n, fix sidebar divider

* fix(test): use data-testid for undo button selector in E2E test

UndoCommit translates to '撤回此提交' in zh-Hans, not '撤销'.
Use data-testid='undo-commit-button' for reliable matching.

* fix(git-log): batch undo via undoCommits, notify git log on tiddler save

- Add undoCommits() to git service: undoes N commits sequentially and fires
  only ONE gitStateChange notification at the end, avoiding the race where
  rapid-fire events drop refreshes due to loadGitLogInProgress guard
- CommitDetailsPanel.handleUndo now uses undoCommits() instead of looping undoCommit()
- FileSystemAdaptor.saveTiddler now calls git.notifyFileChange after every save
  so the git log window auto-refreshes when tiddlers are created/modified, even
  when enableFileSystemWatch is false (the default)

* fix(test): mock git.notifyFileChange in FileSystemAdaptor unit tests

* fix(e2e): use BrowserWindow.id to match tidgiMiniWindow Playwright page

The previous code matched the mini window's Playwright page by document.title,
checking for '太记小窗', 'TidGi Mini Window', or 'TidGiMiniWindow'. But the
mini window loads the same index.html as the main window (title='TidGi'), so the
title-based match fails whenever React hasn't yet updated document.title.

Fix: after finding the BrowserWindow by title (getTitle() contains 'Mini Window')
or by dimensions as a fallback, compare each Playwright Page's underlying
BrowserWindow.id to the found window's id. This is reliable regardless of
document.title state.

* fix(lint): remove unnecessary non-null assertion in application.ts

* fix(lint): rename args to searchParams to satisfy unicorn/prevent-abbreviations

* fix(lint): rename searchParams to searchParameters

* fix(window): wait for isVisible() after showWindow() in test mode

In E2E tests, BrowserWindow.show() is asynchronous with respect to
isVisible() returning true. The IPC call to toggleTidgiMiniWindow()
resolves before the OS has actually marked the window as visible,
causing the subsequent 'confirm visible' step to fail.

Add a poll-until-visible loop (50ms interval) after showWindow()
when isTest is true, so the IPC only resolves once isVisible() is
actually true.

---------

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-04-20 18:42:29 +08:00

595 lines
20 KiB
TypeScript

import { DataTable, Then, When } from '@cucumber/cucumber';
import { backOff } from 'exponential-backoff';
import { parseDataTableRows } from '../supports/dataTable';
import { HEAVY_OPERATION_TIMEOUT } from '../supports/timeouts';
import {
clickElement,
clickElementWithText,
elementExists,
executeTiddlyWikiCode,
getDOMContent,
getTextContent,
isLoaded,
pressKey,
typeText,
} from '../supports/webContentsViewHelper';
import type { ApplicationWorld } from './application';
// Backoff configuration for retries
const BACKOFF_OPTIONS = {
numOfAttempts: 8,
startingDelay: 200,
timeMultiple: 1,
maxDelay: 200,
};
const BROWSER_VIEW_RETRY_DELAY_MS = 100;
type BrowserViewBackgroundMode = 'dark' | 'light';
function parseRgbColor(backgroundColor: string): { red: number; green: number; blue: number } | undefined {
const match = backgroundColor.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/i);
if (!match) {
return undefined;
}
return {
red: Number(match[1]),
green: Number(match[2]),
blue: Number(match[3]),
};
}
async function assertBrowserViewBodyBackground(
world: ApplicationWorld,
expectedMode: BrowserViewBackgroundMode,
): Promise<void> {
if (!world.app) {
throw new Error('Application not launched');
}
const colorInfo = await executeTiddlyWikiCode<{ backgroundColor: string; paletteTitle: string }>(
world.app,
`(function() {
const backgroundColor = window.getComputedStyle(document.body).backgroundColor || '';
let paletteTitle = '';
try {
if (typeof $tw !== 'undefined' && $tw.wiki) {
paletteTitle = $tw.wiki.getTiddlerText('$:/palette', '') || '';
}
} catch {
paletteTitle = '';
}
return { backgroundColor, paletteTitle };
})()`,
world.currentWindow,
200,
);
if (!colorInfo) {
throw new Error('Failed to read browser view background color');
}
const rgb = parseRgbColor(colorInfo.backgroundColor);
if (!rgb) {
throw new Error(`Unexpected background color format: ${colorInfo.backgroundColor}; palette=${colorInfo.paletteTitle}`);
}
const { red, green, blue } = rgb;
const isDark = red < 128 && green < 128 && blue < 128;
const isLight = red > 200 && green > 200 && blue > 200;
if (expectedMode === 'dark' && !isDark) {
throw new Error(`Expected dark background in browser view, got ${colorInfo.backgroundColor}; palette=${colorInfo.paletteTitle}`);
}
if (expectedMode === 'light' && !isLight) {
throw new Error(`Expected light background in browser view, got ${colorInfo.backgroundColor}; palette=${colorInfo.paletteTitle}`);
}
}
Then('I should see {string} in the browser view content', async function(this: ApplicationWorld, expectedText: string) {
if (!this.app) {
throw new Error('Application not launched');
}
if (!this.currentWindow) {
throw new Error('No current window available');
}
await backOff(
async () => {
const content = await getTextContent(this.app!, this.currentWindow);
if (!content || !content.includes(expectedText)) {
throw new Error(`Expected text "${expectedText}" not found`);
}
},
BACKOFF_OPTIONS,
).catch(async () => {
const finalContent = await getTextContent(this.app!, this.currentWindow);
throw new Error(
`Expected text "${expectedText}" not found in browser view content. Actual content: ${finalContent ? finalContent.substring(0, 200) + '...' : 'null'}`,
);
});
});
Then('I should see {string} in the browser view DOM', async function(this: ApplicationWorld, expectedText: string) {
if (!this.app) {
throw new Error('Application not launched');
}
if (!this.currentWindow) {
throw new Error('No current window available');
}
await backOff(
async () => {
const domContent = await getDOMContent(this.app!, this.currentWindow);
if (!domContent || !domContent.includes(expectedText)) {
throw new Error(`Expected text "${expectedText}" not found in DOM`);
}
},
BACKOFF_OPTIONS,
).catch(async () => {
const finalDomContent = await getDOMContent(this.app!, this.currentWindow);
throw new Error(
`Expected text "${expectedText}" not found in browser view DOM. Actual DOM: ${finalDomContent ? finalDomContent.substring(0, 200) + '...' : 'null'}`,
);
});
});
Then('the browser view should be loaded and visible', async function(this: ApplicationWorld) {
if (!this.app) {
throw new Error('Application not launched');
}
if (!this.currentWindow) {
throw new Error('No current window available');
}
// Use a longer retry window because WebContentsView creation is async and can take
// several seconds after workspace activation. Each attempt is fast (< 10ms) when the
// view doesn't exist yet, so we need many more attempts to cover the full wait budget.
const longRetryAttempts = Math.floor((HEAVY_OPERATION_TIMEOUT - 4000) / BROWSER_VIEW_RETRY_DELAY_MS);
await backOff(
async () => {
const content = await getTextContent(this.app!, this.currentWindow);
if (!content || content.trim().length === 0) {
throw new Error('Browser view content not available yet');
}
},
{
...BACKOFF_OPTIONS,
numOfAttempts: longRetryAttempts,
startingDelay: BROWSER_VIEW_RETRY_DELAY_MS,
maxDelay: BROWSER_VIEW_RETRY_DELAY_MS,
},
).catch(async () => {
// Gather diagnostics for failure analysis
let diagnostics = '';
try {
const loaded = await isLoaded(this.app!, this.currentWindow);
const content = await getTextContent(this.app!, this.currentWindow);
diagnostics = `isLoaded=${loaded}, textContent=${content ? `"${content.substring(0, 100)}..."` : 'null'}`;
} catch (diagError) {
diagnostics = `diagnostics failed: ${String(diagError)}`;
}
throw new Error(
`Browser view is not loaded or visible after ${longRetryAttempts} attempts ` +
`(budget: ${Math.round(HEAVY_OPERATION_TIMEOUT / 1000)}s). ${diagnostics}`,
);
});
});
When('I click on {string} element in browser view with selector {string}', async function(this: ApplicationWorld, elementComment: string, selector: string) {
if (!this.app) {
throw new Error('Application not launched');
}
try {
// Check if selector contains :has-text() pseudo-selector
const hasTextMatch = selector.match(/^(.+):has-text\(['"](.+)['"]\)$/);
if (hasTextMatch) {
// Extract base selector and text content
const baseSelector = hasTextMatch[1];
const textContent = hasTextMatch[2];
await clickElementWithText(this.app, baseSelector, textContent, this.currentWindow);
} else {
// Use regular selector
await clickElement(this.app, selector, this.currentWindow);
}
} catch (error) {
throw new Error(`Failed to click ${elementComment} element with selector "${selector}" in browser view: ${error as Error}`);
}
});
When('I click on {string} elements in browser view with selectors:', async function(this: ApplicationWorld, _elementDescriptions: string, dataTable: DataTable) {
if (!this.app) {
throw new Error('Application not launched');
}
const rows = dataTable.raw();
const dataRows = parseDataTableRows(rows, 2);
const errors: string[] = [];
if (dataRows[0]?.length !== 2) {
throw new Error('Table must have exactly 2 columns: | element description | selector |');
}
for (const [elementComment, selector] of dataRows) {
try {
const hasTextMatch = selector.match(/^(.+):has-text\(['"](.+)['"]\)$/);
if (hasTextMatch) {
const baseSelector = hasTextMatch[1];
const textContent = hasTextMatch[2];
await clickElementWithText(this.app, baseSelector, textContent, this.currentWindow);
} else {
await clickElement(this.app, selector, this.currentWindow);
}
} catch (error) {
errors.push(`Failed to click ${elementComment} element with selector "${selector}" in browser view: ${error as Error}`);
}
}
if (errors.length > 0) {
throw new Error(`Failed to click elements in browser view:\n${errors.join('\n')}`);
}
});
Then('I wait for {string} element in browser view with selector {string}', async function(
this: ApplicationWorld,
elementComment: string,
selector: string,
) {
if (!this.app) {
throw new Error('Application not launched');
}
await backOff(
async () => {
const exists = await elementExists(this.app!, selector, this.currentWindow);
if (!exists) {
throw new Error(`Element "${elementComment}" with selector "${selector}" not found yet`);
}
},
{ ...BACKOFF_OPTIONS, numOfAttempts: 20, startingDelay: 200 },
).catch(() => {
throw new Error(`Element "${elementComment}" with selector "${selector}" did not appear in browser view after multiple attempts`);
});
});
When('I type {string} in {string} element in browser view with selector {string}', async function(this: ApplicationWorld, text: string, elementComment: string, selector: string) {
if (!this.app) {
throw new Error('Application not launched');
}
try {
await typeText(this.app, selector, text, this.currentWindow);
} catch (error) {
throw new Error(`Failed to type in ${elementComment} element with selector "${selector}" in browser view: ${error as Error}`);
}
});
When('I press {string} in browser view', async function(this: ApplicationWorld, key: string) {
if (!this.app) {
throw new Error('Application not launched');
}
try {
await pressKey(this.app, key, this.currentWindow);
} catch (error) {
throw new Error(`Failed to press key "${key}" in browser view: ${error as Error}`);
}
});
Then('I should not see {string} in the browser view content', async function(this: ApplicationWorld, unexpectedText: string) {
if (!this.app) {
throw new Error('Application not launched');
}
if (!this.currentWindow) {
throw new Error('No current window available');
}
// Wait a bit for UI to update
await new Promise(resolve => setTimeout(resolve, 500));
// Check that text does not exist in content
const content = await getTextContent(this.app, this.currentWindow);
if (content && content.includes(unexpectedText)) {
throw new Error(`Unexpected text "${unexpectedText}" found in browser view content`);
}
});
Then('I should not see a(n) {string} element in browser view with selector {string}', async function(this: ApplicationWorld, elementComment: string, selector: string) {
if (!this.app) {
throw new Error('Application not launched');
}
if (!this.currentWindow) {
throw new Error('No current window available');
}
await backOff(
async () => {
const exists: boolean = await elementExists(this.app!, selector, this.currentWindow);
if (exists) {
throw new Error('Element still exists');
}
},
BACKOFF_OPTIONS,
).catch(() => {
throw new Error(`Element "${elementComment}" with selector "${selector}" was found in browser view after multiple attempts, but should not be visible`);
});
});
Then('I should see a(n) {string} element in browser view with selector {string}', async function(this: ApplicationWorld, elementComment: string, selector: string) {
if (!this.app) {
throw new Error('Application not launched');
}
if (!this.currentWindow) {
throw new Error('No current window available');
}
await backOff(
async () => {
const exists: boolean = await elementExists(this.app!, selector, this.currentWindow);
if (!exists) {
throw new Error('Element does not exist yet');
}
},
BACKOFF_OPTIONS,
).catch(() => {
throw new Error(`Element "${elementComment}" with selector "${selector}" not found in browser view after multiple attempts`);
});
});
Then('I should see {string} elements in browser view with selectors:', async function(this: ApplicationWorld, _elementDescriptions: string, dataTable: DataTable) {
if (!this.app) {
throw new Error('Application not launched');
}
if (!this.currentWindow) {
throw new Error('No current window available');
}
const rows = dataTable.raw();
const dataRows = parseDataTableRows(rows, 2);
const errors: string[] = [];
if (dataRows[0]?.length !== 2) {
throw new Error('Table must have exactly 2 columns: | element description | selector |');
}
await Promise.all(dataRows.map(async ([elementComment, selector]) => {
try {
await backOff(
async () => {
const exists: boolean = await elementExists(this.app!, selector, this.currentWindow);
if (!exists) {
throw new Error('Element does not exist yet');
}
},
BACKOFF_OPTIONS,
);
} catch (error) {
errors.push(`Element "${elementComment}" with selector "${selector}" not found in browser view: ${error as Error}`);
}
}));
if (errors.length > 0) {
throw new Error(`Failed to find elements in browser view:\n${errors.join('\n')}`);
}
});
When('I open tiddler {string} in browser view', async function(this: ApplicationWorld, tiddlerTitle: string) {
if (!this.app) {
throw new Error('Application not launched');
}
/**
* Use flat 200 ms retries instead of exponential back-off.
* During a wiki restart, executeTiddlyWikiCode hangs for ~200 ms per attempt
* (webContents navigating), then needs a delay before the next try.
* Flat 200 ms gives ~12 attempts in the 5 s Cucumber step budget, which is
* enough to bridge the gap when the wiki becomes ready late into the step.
*/
await backOff(
async () => {
await executeTiddlyWikiCode(
this.app!,
`(function() {
const title = "${tiddlerTitle.replace(/"/g, '\\"')}";
try { if ($tw?.wiki?.removeFromStory) $tw.wiki.removeFromStory(title); } catch {}
$tw.wiki.addToStory(title);
return true;
})()`,
this.currentWindow,
);
},
{ ...BACKOFF_OPTIONS, numOfAttempts: 8, startingDelay: 200, timeMultiple: 1, maxDelay: 200 },
).catch((error: unknown) => {
throw new Error(`Failed to open tiddler "${tiddlerTitle}" in browser view: ${error as Error}`);
});
});
/**
* Create a new tiddler with title and optional tags via TiddlyWiki UI.
* This step handles all the UI interactions: click add button, set title, add tags, and confirm.
*/
When('I create a tiddler {string} with tag {string} in browser view', async function(
this: ApplicationWorld,
tiddlerTitle: string,
tagName: string,
) {
if (!this.app) {
throw new Error('Application not launched');
}
// Use TiddlyWiki API directly — the DOM-based approach (typeText / pressKey)
// doesn't reliably trigger TW5's custom input handling for the title editor.
const script = `(function() {
var now = $tw.utils.stringifyDate(new Date());
$tw.wiki.addTiddler(new $tw.Tiddler({
title: ${JSON.stringify(tiddlerTitle)},
tags: ${JSON.stringify(tagName)},
text: '',
type: 'text/vnd.tiddlywiki',
created: now,
modified: now
}));
return true;
})()`;
await executeTiddlyWikiCode(this.app, script, this.currentWindow);
// Give syncer time to pick up the change and write to disk
await new Promise(resolve => setTimeout(resolve, 1500));
});
/**
* Create a new tiddler with title and custom field via TiddlyWiki UI.
*/
When('I create a tiddler {string} with field {string} set to {string} in browser view', async function(
this: ApplicationWorld,
tiddlerTitle: string,
fieldName: string,
fieldValue: string,
) {
if (!this.app) {
throw new Error('Application not launched');
}
// Use TiddlyWiki API directly for reliability.
const script = `(function() {
var now = $tw.utils.stringifyDate(new Date());
var fields = {
title: ${JSON.stringify(tiddlerTitle)},
text: '',
type: 'text/vnd.tiddlywiki',
created: now,
modified: now
};
fields[${JSON.stringify(fieldName)}] = ${JSON.stringify(fieldValue)};
$tw.wiki.addTiddler(new $tw.Tiddler(fields));
return true;
})()`;
await executeTiddlyWikiCode(this.app, script, this.currentWindow);
// Give syncer time to pick up the change and write to disk
await new Promise(resolve => setTimeout(resolve, 1500));
});
/**
* Execute TiddlyWiki code in browser view
* Useful for programmatic wiki operations
*/
When('I execute TiddlyWiki code in browser view: {string}', async function(this: ApplicationWorld, code: string) {
if (!this.app) {
throw new Error('Application not launched');
}
try {
// Wrap the code to avoid returning non-serializable objects
const wrappedCode = `(function() { ${code}; return true; })()`;
await executeTiddlyWikiCode(this.app, wrappedCode, this.currentWindow);
} catch (error) {
throw new Error(`Failed to execute TiddlyWiki code in browser view: ${error as Error}`);
}
});
Then('image {string} should be loaded in browser view', async function(this: ApplicationWorld, imageName: string) {
if (!this.app) {
throw new Error('Application not launched');
}
const tiddlerTitle = imageName;
let lastDiagnostic = '';
await backOff(
async () => {
let isImageLoaded = false;
try {
const diagnostic = await executeTiddlyWikiCode<{
loaded: boolean;
hasContainer: boolean;
hasImage: boolean;
src: string;
complete: boolean;
naturalWidth: number;
naturalHeight: number;
canonicalUri: string;
}>(
this.app!,
`(function() {
const title = ${JSON.stringify(tiddlerTitle)};
const container = document.querySelector("[data-tiddler-title='" + title.replace(/'/g, "\\'") + "']");
if (!container) {
try { if (typeof $tw !== 'undefined' && $tw.wiki) $tw.wiki.addToStory(title); } catch {}
return {
loaded: false,
hasContainer: false,
hasImage: false,
src: '',
complete: false,
naturalWidth: 0,
naturalHeight: 0,
canonicalUri: (typeof $tw !== 'undefined' && $tw.wiki && $tw.wiki.getTiddler(title)?.fields?._canonical_uri) || '',
};
}
const image = container.querySelector('img');
if (!image) {
return {
loaded: false,
hasContainer: true,
hasImage: false,
src: '',
complete: false,
naturalWidth: 0,
naturalHeight: 0,
canonicalUri: (typeof $tw !== 'undefined' && $tw.wiki && $tw.wiki.getTiddler(title)?.fields?._canonical_uri) || '',
};
}
return {
loaded: Boolean(image.complete && image.naturalWidth > 0 && image.naturalHeight > 0),
hasContainer: true,
hasImage: true,
src: image.currentSrc || image.src || '',
complete: Boolean(image.complete),
naturalWidth: Number(image.naturalWidth || 0),
naturalHeight: Number(image.naturalHeight || 0),
canonicalUri: (typeof $tw !== 'undefined' && $tw.wiki && $tw.wiki.getTiddler(title)?.fields?._canonical_uri) || '',
};
})()`,
this.currentWindow,
200,
);
lastDiagnostic = JSON.stringify(diagnostic);
isImageLoaded = Boolean(diagnostic?.loaded);
} catch {
isImageLoaded = false;
}
if (!isImageLoaded) {
throw new Error(`Image ${imageName} is not loaded yet`);
}
},
{ numOfAttempts: 100, startingDelay: 150, timeMultiple: 1, maxDelay: 150 },
).catch(() => {
throw new Error(`Image ${imageName} is not loaded correctly in browser view. Last diagnostic: ${lastDiagnostic}`);
});
});
Then('browser view body background should be dark', async function(this: ApplicationWorld) {
await assertBrowserViewBodyBackground(this, 'dark');
});
Then('browser view body background should be light', async function(this: ApplicationWorld) {
await assertBrowserViewBodyBackground(this, 'light');
});
Then('browser view body background should be {string}', async function(this: ApplicationWorld, mode: string) {
if (mode !== 'dark' && mode !== 'light') {
throw new Error(`Unsupported browser view background mode: ${mode}. Use "dark" or "light".`);
}
await assertBrowserViewBodyBackground(this, mode);
});