TidGi-Desktop/features/stepDefinitions/application.ts
lin onetwo 7edb132d32
Fix/watch fs and ai commit (#674)
* fix: missing return

* feat: showApiKey

* feat: undo commit

* feat: amend commit

* fix: file name quoted in git log

* fix: wikiWorkspaceDefaultValues

* fix: no ai commit message sometimes

* Persist only non-default preferences to storage

Added a utility to store only preferences that differ from defaults, reducing storage size and improving config readability. Updated the setPreferences method to use this utility before saving preferences.

* fix: External Attachment Handling in fs plugin instead of ext-attachment-plugin to handle direct tag update case which won't trigger  th-saving-tiddler hook

* feat: api for plugin to create base64 file

* Show all untracked files and recreate Git history window

Updated git status commands to use '-uall' for displaying all untracked files, not just directories. Modified windowService.open calls for Git history to include the { recreate: true } option, ensuring the window is refreshed when opened from various menus.

* fix: handling of external attachments with _canonical_uri

Ensure tiddlers with _canonical_uri are always saved as .tid files, not as binary files, by forcing the .tid extension in FileSystemAdaptor. Update tests to verify this behavior. Also, skip loading files from the external attachments folder in loadWikiTiddlersWithSubWikis to prevent them from being loaded as separate tiddlers.

* Refactor external attachment utilities to module exports

Refactored externalAttachmentUtilities to use ES module exports instead of attaching functions to $tw.utils. Updated imports and mocks accordingly, removed related type definitions from ExtendedUtilities, and cleaned up obsolete meta file.

* disable enableFileSystemWatch to prevent bug for innocent users

* fix: test that requires enableFileSystemWatch use new step set to true

* Fix extension filter usage and sync workspace state after save

Refactored variable naming for extension filters in FileSystemAdaptor to improve clarity and fixed their usage in generateTiddlerFileInfo calls. Removed an unused import in routingUtilities.type.ts. Added a useEffect in useForm to sync workspace state with originalWorkspace after save, ensuring the save button disappears as expected.

* fix: review

* lint

* feat: unify AI commit entry points and add availability check  - Unified all AI commit message generation to use syncService.syncWikiIfNeeded() for consistent business logic handling - Added externalAPI.isAIAvailable() method to check if AI provider and model are properly configured - Updated gitService.isAIGenerateBackupTitleEnabled() to use the new availability check - Removed redundant logging code since generateFromAI() automatically logs to database when externalAPIDebug is enabled - Simplified menu item creation logic in menuItems.ts - Ensured AI menu options only appear when both API credentials and free model are configured - Updated documentation to reflect the unified architecture

* Improve AI commit message diff filtering and API checks

Renamed the AI commit message entry points doc for clarity. Enhanced the AI availability check to better handle provider API key requirements, including support for providers that do not require keys. Improved plugin diff filtering to retain small config file diffs while omitting large plugin file contents, optimizing AI token usage.

* Update wiki

* Refactor and enhance Tidgi mini window initialization and sync

Refactors Tidgi mini window startup to use a new initializeTidgiMiniWindow method, improving workspace selection logic and view management. Adds concurrency locks to prevent race conditions during open/close operations. Enhances workspace sync/fixed mode handling, view cleanup, and error logging. Updates interfaces and utilities to support new behaviors and improves robustness of tray icon creation and view realignment.

* Refactor file system sync to use $tw.syncer.syncFromServer()

Introduces FileSystemWatcher to monitor file changes and collect updates for the syncer, replacing direct wiki updates in WatchFileSystemAdaptor. Updates documentation to describe the new syncer-driven architecture, echo prevention, and event handling. WatchFileSystemAdaptor now delegates file change detection and lazy loading to FileSystemWatcher, improving batch change handling and eliminating echo loops.

* Improve logging and cleanup in file system watcher and git ops

Added detailed logging to WatchFileSystemAdaptor and FileSystemWatcher for better traceability during initialization and test stabilization. Introduced a constant for the temporary git index prefix in gitOperations. Removed the unused comparison.ts utility for tiddler comparison. Enhanced comments and logging for AI commit message generation context.

* Improve GitLog i18n test and config refresh logic

Updated gitLog.feature to use only Chinese selectors for actions, revert, and discard buttons, improving i18n test reliability. In FileSystemWatcher, re-fetch workspace config before checking enableFileSystemWatch to ensure latest settings are respected. In useGitLogData, prevent file-change events from overriding commit/undo events to maintain correct auto-selection behavior.

* Improve Git log selection and test stability

Refines auto-selection logic in the Git log window to better handle uncommitted changes, commits, reverts, and undos. Updates the feature test to explicitly verify selection and UI state after each operation, improving reliability. Removes unnecessary config re-fetch in FileSystemWatcher and enhances logging for more accurate DOM update detection.

* Implement workspace config sync via tidgi.config.json

Adds support for syncing workspace configuration to tidgi.config.json in the wiki folder, enabling settings persistence and migration across devices. Introduces new documentation, feature tests, and supporting utilities for config file reading, writing, migration, and validation. Updates step definitions and test helpers to support config sync scenarios, and refactors database config utilities for modularity.

* Improve workspace config handling and sync logic

Enhances workspace lookup in step definitions to check both settings.json and tidgi.config.json, ensuring properties are found even if moved. Updates tidgiConfig write logic to remove the config file if all values are default. Refactors workspace save logic to always write syncable config to tidgi.config.json for all wiki workspaces before removing those fields from settings.json, preventing config loss.

* Update .gitignore

* Update wiki.ts

* Add delay before waiting for git log render after revert

- Add 1 second wait after clearing git-log-data-rendered markers following revert
- This gives UI time to start refreshing before we check for the new marker
- Fixes CI timing issue where revert operation needs more time to trigger UI refresh

* Update test log markers for git log refresh events

Replaces '[test-id-git-log-data-rendered]' with '[test-id-git-log-refreshed]' in gitLog.feature to better reflect UI refresh events after commit and revert actions. Adds a debug log marker '[test-id-git-revert-complete]' in revertCommit for improved test synchronization.

* Fix git revert refresh timing - remove intermediate step and rely on git-log-refreshed

* Add detailed logging to handleRevert for CI debugging

* Fix git log refresh by adding manual triggerRefresh fallback

- Add triggerRefresh function to useGitLogData hook for manual refresh
- Call triggerRefresh in handleCommitSuccess, handleRevertSuccess, and handleUndoSuccess
- This fixes cross-process IPC observable subscription issues where gitStateChange$
  notifications from main process may not reach renderer process reliably
- Add detailed logging to handleRevert for CI debugging

* Update index.tsx
2026-01-10 23:57:59 +08:00

358 lines
14 KiB
TypeScript

import { AfterStep, setDefaultTimeout, setWorldConstructor, When } from '@cucumber/cucumber';
import { backOff } from 'exponential-backoff';
import fs from 'fs-extra';
import path from 'path';
import { _electron as electron } from 'playwright';
import type { ElectronApplication, Page } from 'playwright';
import { windowDimension, WindowNames } from '../../src/services/windows/WindowProperties';
import { MockOAuthServer } from '../supports/mockOAuthServer';
import { MockOpenAIServer } from '../supports/mockOpenAI';
import { makeSlugPath, screenshotsDirectory } from '../supports/paths';
import { getPackedAppPath } from '../supports/paths';
import { captureScreenshot } from '../supports/webContentsViewHelper';
// Backoff configuration for retries
const BACKOFF_OPTIONS = {
numOfAttempts: 8,
startingDelay: 100,
timeMultiple: 2,
};
// Helper function to check if window type is valid and return the corresponding WindowNames
export function checkWindowName(windowType: string): WindowNames {
// Exact match - windowType must be a valid WindowNames enum key
if (windowType in WindowNames) {
return (WindowNames as Record<string, WindowNames>)[windowType];
}
throw new Error(`Window type "${windowType}" is not a valid WindowNames. Check the WindowNames enum in WindowProperties.ts. Available: ${Object.keys(WindowNames).join(', ')}`);
}
// Helper function to get window dimensions and ensure they are valid
export function checkWindowDimension(windowName: WindowNames): { width: number; height: number } {
const targetDimensions = windowDimension[windowName];
if (!targetDimensions.width || !targetDimensions.height) {
throw new Error(`Window "${windowName}" does not have valid dimensions defined in windowDimension`);
}
return targetDimensions as { width: number; height: number };
}
export class ApplicationWorld {
app: ElectronApplication | undefined;
mainWindow: Page | undefined; // Keep for compatibility during transition
currentWindow: Page | undefined; // New state-managed current window
mockOpenAIServer: MockOpenAIServer | undefined;
mockOAuthServer: MockOAuthServer | undefined;
savedWorkspaceId: string | undefined; // For storing workspace ID between steps
// Helper method to check if window is visible
async isWindowVisible(page: Page): Promise<boolean> {
if (!this.app) return false;
try {
const browserWindow = await this.app.browserWindow(page);
return await browserWindow.evaluate((win: Electron.BrowserWindow) => win.isVisible());
} catch {
return false;
}
}
// Helper method to wait for window with retry logic
async waitForWindowCondition(
windowType: string,
condition: (window: Page | undefined, isVisible: boolean) => boolean,
): Promise<boolean> {
if (!this.app) return false;
try {
await backOff(
async () => {
const targetWindow = await this.findWindowByType(windowType);
const visible = targetWindow ? await this.isWindowVisible(targetWindow) : false;
if (!condition(targetWindow, visible)) {
throw new Error('Condition not met');
}
},
BACKOFF_OPTIONS,
);
return true;
} catch {
return false;
}
}
// Helper method to find window by type - strict WindowNames matching
async findWindowByType(windowType: string): Promise<Page | undefined> {
if (!this.app) return undefined;
// Validate window type first
const windowName = checkWindowName(windowType);
const pages = this.app.windows();
if (windowName === WindowNames.main) {
// Main window is the first/primary window, typically showing guide, agent, help, or wiki pages
// It's the window that opens on app launch
const allWindows = pages.filter(page => !page.isClosed());
if (allWindows.length > 0) {
// Return the first window (main window is always the first one created)
return allWindows[0];
}
return undefined;
} else if (windowName === WindowNames.tidgiMiniWindow) {
// Special handling for tidgi mini window
// First try to find by Electron window dimensions (more reliable than title)
const windowDimensions = checkWindowDimension(windowName);
try {
const electronWindowInfo = await this.app.evaluate(
async ({ BrowserWindow }, size: { width: number; height: number }) => {
const allWindows = BrowserWindow.getAllWindows();
const tidgiMiniWindow = allWindows.find(win => {
const bounds = win.getBounds();
return bounds.width === size.width && bounds.height === size.height;
});
return tidgiMiniWindow ? { id: tidgiMiniWindow.id } : null;
},
windowDimensions,
);
if (electronWindowInfo) {
// Found by dimensions, now match with Playwright page
const allWindows = pages.filter(page => !page.isClosed());
for (const page of allWindows) {
try {
// Try to match by checking if this page belongs to the found electron window
// For now, use title as fallback verification
const title = await page.title();
if (title.includes('太记小窗') || title.includes('TidGi Mini Window') || title.includes('TidGiMiniWindow')) {
return page;
}
} catch {
continue;
}
}
}
} catch {
// If Electron API fails, fallback to title matching
}
// Fallback: Match by window title
const allWindows = pages.filter(page => !page.isClosed());
for (const page of allWindows) {
try {
const title = await page.title();
if (title.includes('太记小窗') || title.includes('TidGi Mini Window') || title.includes('TidGiMiniWindow')) {
return page;
}
} catch {
// Page might be closed or not ready, continue to next
continue;
}
}
return undefined;
} else {
// For regular windows (preferences, about, addWorkspace, etc.)
return pages.find(page => {
if (page.isClosed()) return false;
const url = page.url() || '';
// Match exact route paths: /#/windowType or ending with /windowType
return url.includes(`#/${windowType}`) || url.endsWith(`/${windowType}`);
});
}
}
async getWindow(windowType: string = 'main'): Promise<Page | undefined> {
if (!this.app) return undefined;
// Special case for 'current' window
if (windowType === 'current') {
return this.currentWindow;
}
// Use the findWindowByType method with retry logic using backoff
try {
return await backOff(
async () => {
const window = await this.findWindowByType(windowType);
if (!window) {
throw new Error(`Window ${windowType} not found`);
}
return window;
},
BACKOFF_OPTIONS,
);
} catch (error) {
// If it's an invalid window type error, re-throw it
if (error instanceof Error && error.message.includes('is not a valid WindowNames')) {
throw error;
}
return undefined;
}
}
}
setWorldConstructor(ApplicationWorld);
if (process.env.CI) {
setDefaultTimeout(50000);
}
AfterStep(async function(this: ApplicationWorld, { pickle, pickleStep, result }) {
// Only take screenshots in CI environment
// if (!process.env.CI) return;
try {
const stepText = pickleStep.text;
// Skip screenshots for wait steps to avoid too many screenshots
if (stepText.match(/^I wait for/i)) {
return;
}
// Prefer an existing currentWindow if it's still open
let pageToUse: Page | undefined;
if (this.currentWindow && !this.currentWindow.isClosed()) {
pageToUse = this.currentWindow;
}
// If currentWindow is not available, try to re-acquire any open window from the app
if ((!pageToUse || pageToUse.isClosed()) && this.app) {
const openPages = this.app.windows().filter(p => !p.isClosed());
if (openPages.length > 0) {
pageToUse = openPages[0];
}
}
const scenarioName = pickle.name;
// Limit scenario slug to avoid extremely long directory names
const cleanScenarioName = makeSlugPath(scenarioName, 60);
// Limit step text slug to avoid excessively long filenames which can trigger ENAMETOOLONG
const cleanStepText = makeSlugPath(stepText, 80);
const stepStatus = result && typeof result.status === 'string' ? result.status : 'unknown-status';
const featureDirectory = path.resolve(screenshotsDirectory, cleanScenarioName);
// Create directory asynchronously to avoid blocking the event loop in CI
await fs.ensureDir(featureDirectory);
// Sometimes window close and don't wait for use to take picture, or window haven't open in this step, never mind, just skip.
/**
* Typical steps like:
* - I add test ai settings
* - I cleanup test wiki so it could create a new one on start
* - I clear test ai settings
*/
if (!pageToUse || pageToUse.isClosed()) {
// console.warn(`Skipping screenshot: ${cleanStepText}`);
return;
}
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
// Try to capture both WebContentsView and Page screenshots
let webViewCaptured = false;
if (this.app) {
const webViewScreenshotPath = path.resolve(featureDirectory, `${timestamp}-${cleanStepText}-${stepStatus}-webview.png`);
webViewCaptured = await captureScreenshot(this.app, webViewScreenshotPath);
}
// Always capture page screenshot (UI chrome/window)
const pageScreenshotPath = path.resolve(featureDirectory, `${timestamp}-${cleanStepText}-${stepStatus}${webViewCaptured ? '-page' : ''}.png`);
await pageToUse.screenshot({ path: pageScreenshotPath, fullPage: true, type: 'png' });
} catch (screenshotError) {
console.warn('Failed to take screenshot:', screenshotError);
}
});
When('I launch the TidGi application', async function(this: ApplicationWorld) {
// For E2E tests on dev mode, use the packaged test version with NODE_ENV environment variable baked in
const packedAppPath = getPackedAppPath();
try {
this.app = await electron.launch({
executablePath: packedAppPath,
// Add debugging options to prevent app from closing and CI-specific args
args: [
'--no-sandbox',
'--disable-dev-shm-usage',
'--disable-gpu',
'--disable-software-rasterizer',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-renderer-backgrounding',
'--disable-features=TranslateUI',
'--disable-ipc-flooding-protection',
'--force-device-scale-factor=1',
'--high-dpi-support=1',
'--force-color-profile=srgb',
'--disable-extensions',
'--disable-plugins',
'--disable-default-apps',
'--virtual-time-budget=1000',
'--run-all-compositor-stages-before-draw',
'--disable-checker-imaging',
// Linux CI specific arguments
...(process.env.CI && process.platform === 'linux'
? [
'--disable-background-mode',
'--disable-features=VizDisplayCompositor',
'--use-gl=swiftshader',
'--disable-accelerated-2d-canvas',
'--disable-accelerated-jpeg-decoding',
'--disable-accelerated-mjpeg-decode',
'--disable-accelerated-video-decode',
]
: []),
],
env: {
...process.env,
NODE_ENV: 'test',
E2E_TEST: 'true',
// Ensure tests run in Chinese locale so i18n UI strings match expectations
// set multiple variables for cross-platform coverage
LANG: process.env.LANG || 'zh-Hans.UTF-8',
LANGUAGE: process.env.LANGUAGE || 'zh-Hans:zh',
LC_ALL: process.env.LC_ALL || 'zh-Hans.UTF-8',
// Force display settings for CI
ELECTRON_DISABLE_SECURITY_WARNINGS: 'true',
...(process.env.CI && {
ELECTRON_ENABLE_LOGGING: 'true',
ELECTRON_DISABLE_HARDWARE_ACCELERATION: 'true',
}),
},
// Set cwd to repo root so process.cwd() in app returns the correct path for userData-test
cwd: process.cwd(),
timeout: 30000, // Increase timeout to 30 seconds for CI
});
// Wait longer for window in CI environment
const windowTimeout = process.env.CI ? 45000 : 10000;
this.mainWindow = await this.app.firstWindow({ timeout: windowTimeout });
this.currentWindow = this.mainWindow;
} catch (error) {
throw new Error(
`Failed to launch TidGi application: ${error as Error}. You should run \`pnpm run test:prepare-e2e\` before running the tests to ensure the app is built, and build with binaries like "dugite" and "tiddlywiki", see scripts/afterPack.js for more details.`,
);
}
});
When('I prepare to select directory in dialog {string}', async function(this: ApplicationWorld, directoryName: string) {
if (!this.app) {
throw new Error('Application is not launched');
}
const targetPath = path.resolve(process.cwd(), directoryName);
// Setup one-time dialog handler that restores after use
await this.app.evaluate(({ dialog }, targetDirectory: string) => {
// Save original function with proper binding
const originalShowOpenDialog = dialog.showOpenDialog.bind(dialog);
// Override with one-time mock
dialog.showOpenDialog = async () => {
// Restore original immediately after first call
dialog.showOpenDialog = originalShowOpenDialog;
return {
canceled: false,
filePaths: [targetDirectory],
};
};
}, targetPath);
});