TidGi-Desktop/features/talkWithAI.feature
lin onetwo 2a5eb349f2
Fix/misc bug (#686)
* refactor: replace time-window echo prevention with mtime+size and content checks

FileSystemWatcher: use recorded mtime+size (lastWriteStats) and saving-state
flag (titlesBeingSaved) to skip own-write echoes, plus content identity fallback.
Removes unreliable setTimeout-based exclude/scheduleFileInclusion.

ipcServerRoutes: replace TTL Map with synchronous Set (ipcPendingTitles),
attach changeCount as revision to change Observable.

ipc-syncadaptor: use lastSavedRevisions for revision-based echo prevention,
batch large syncs via requestIdleCallback.

Also: EditWorkspace controlled input fixes, port field local state, agent
framework config setConfig/persistConfig split, misc value ?? '' fixes.

* Enhance e2e screenshots and test logging

Improve end-to-end test reliability and diagnostics:

- Capture window screenshots for non-BrowserView steps: add captureWindowScreenshot and use it in AfterStep to capture the Electron BrowserWindow via webContents.capturePage() when appropriate; keep existing captureScreenshot for BrowserView steps. (features/supports/webContentsViewHelper.ts, features/stepDefinitions/application.ts)
- Prevent wiki restart race: wait for the wiki worker '[test-id-WIKI_WORKER_STARTED]' marker before restarting to avoid DoubleWikiInstanceError. (features/stepDefinitions/wiki.ts)
- Make git-related test markers more visible by switching logger.debug → logger.info for git init/commit/sync/checkout/revert and git-log rendering markers, ensuring e2e tests can reliably detect these events. (src/services/git/index.ts, src/windows/GitLog/useGitLogData.ts)
- Minor feature test tweak: wait for page to load in defaultWiki.feature before interacting with the editWorkspace window.

These changes reduce flaky screenshots and timing races in tests and improve test marker visibility for e2e detection.

* Enhance e2e screenshots and test logging

Improve end-to-end test reliability and diagnostics:

- Capture window screenshots for non-BrowserView steps: add captureWindowScreenshot and use it in AfterStep to capture the Electron BrowserWindow via webContents.capturePage() when appropriate; keep existing captureScreenshot for BrowserView steps. (features/supports/webContentsViewHelper.ts, features/stepDefinitions/application.ts)
- Prevent wiki restart race: wait for the wiki worker '[test-id-WIKI_WORKER_STARTED]' marker before restarting to avoid DoubleWikiInstanceError. (features/stepDefinitions/wiki.ts)
- Make git-related test markers more visible by switching logger.debug → logger.info for git init/commit/sync/checkout/revert and git-log rendering markers, ensuring e2e tests can reliably detect these events. (src/services/git/index.ts, src/windows/GitLog/useGitLogData.ts)
- Minor feature test tweak: wait for page to load in defaultWiki.feature before interacting with the editWorkspace window.

These changes reduce flaky screenshots and timing races in tests and improve test marker visibility for e2e detection.

* Use startTransition to update port on change

Import startTransition from React and update the port input handler to capture the raw value, update local display state, and defer parsing/setting the workspace port inside startTransition. The handler now parses an empty value as 0, validates the number (non-NaN and >= 0) before calling workspaceSetter, improving responsiveness by marking the workspace update as non-urgent. Also removed an obsolete inline comment.

* Improve E2E, view sync, and wiki IPC robustness

Multiple fixes and improvements across E2E tests, view management, wiki IPC, and workspace handling:

- Docs: add SHOW_E2E_WINDOW env var note to allow visible Electron windows during manual E2E runs.
- Features: simplify/adjust cross-window sync scenario and refine hibernation workspace selectors to avoid collisions with similarly named workspaces.
- E2E steps: make AI-request assertion resilient by polling (backOff); replace brittle DOM-driven tiddler creation with direct TiddlyWiki API calls in browserView step definitions for reliability; add small UI click pause and longer click timeout in ui steps.
- Timeouts: unify global timeout to 25s and derive Playwright short/log wait timeouts from it.
- WebContents helpers: prefer the last child wiki view (active one) when multiple views exist and update comments.
- WikiEmbedTabContent: wake workspace on mount and simplify cleanup to always clear custom bounds on unmount; handle errors.
- Chat UI: hide TabListDropdown in split view.
- Agent instances: default missing agentFrameworkID to 'basicPromptConcatHandler' for older definitions.
- View service: add activelyShownViews set to avoid hiding views that were explicitly shown via showView(); clear stale custom bounds when showing views; avoid moving views offscreen when actively shown; ensure cleanup removes active flag.
- IPC server routes: prefer URL-based workspace ID (resolve correct casing via workspace service) to handle cross-session routing; pass effective workspace ID to route handlers.
- Wiki service: add startup timeout to avoid indefinite hangs waiting for worker boot message.
- IPC sync adaptor: implement titlesBeingSaved/titlesBeingLoaded sets to prevent save-back and SSE echo issues; mark titles when saving/loading and suppress spurious saves.
- Wiki worker IPC routes: replace per-subscriber addEventListener with a shared Subject and single change listener to avoid missed events and cross-window sync bugs; forward subject events to subscribers and log readiness.
- Windows: keep windows hidden during tests by default but allow showing them when SHOW_E2E_WINDOW=1.
- Workspaces: compute next insert order so new wiki workspaces appear at the top of regular workspaces (shift others down).

Overall these changes reduce flakiness in tests, prevent cross-window echo and routing bugs, make view lifecycle handling safer, and improve developer ergonomics for debugging E2E runs.

* fix(e2e): bypass system proxy for git HTTP operations in sync test

Git clone/push/fetch to localhost was routed through the system proxy (port 1080),
which returned 502 Bad Gateway. Add -c http.proxy= to disable proxy for all
HTTP git commands in the sync test step definitions.

* fix: address CI lint errors and Copilot review comments

Lint fixes:
- useOptimisticField: rename *Ref/*Fn vars to *Reference/*Function (unicorn/prevent-abbreviations)
- wiki/index: rename args to arguments_ (unicorn/prevent-abbreviations)
- FileSystemWatcher: use specific type assertion for tiddler fields to avoid no-base-to-string warning
- webContentsViewHelper: rename loop var i to index
- interface.ts: merge duplicate imports from same module (dprint)
- ipc-syncadaptor: expand queueMicrotask callback (dprint)

Copilot review fixes:
- ipcServerRoutes: move subscription to outer scope so Observable teardown
  is returned synchronously from the constructor, preventing subscription leaks
  when observers are disposed before the async IIFE resolves
- useOptimisticField: capture localValue at focus time; on blur only commit
  when user actually changed the value (not when serverValue updated while focused)
- FileSystemWatcher.markSaveComplete: guard scheduleGitNotification behind
  non-empty absoluteFilePath to avoid spurious git notifications on error paths

* fix(e2e): handle corrupt settings.json in cleanup and use filechooser intercept for image upload

- tidgiMiniWindow cleanup: wrap readJson with try/catch so truncated/empty
  settings.json (race between app shutdown write and After hook read) is
  handled gracefully instead of throwing SyntaxError

- application.ts: add 'I prepare to select file ... for file chooser' step
  that registers a Playwright one-shot filechooser handler BEFORE the click
  that triggers fileInput.click(). This prevents the native OS dialog from
  appearing entirely (the chooser is resolved directly with the supplied file).

- talkWithAI.feature: replace two-step 'click then setInputFiles' with the
  new 'prepare filechooser  click' pattern so no OS dialog is shown during
  the AI image attachment test
2026-03-12 21:19:10 +08:00

170 lines
12 KiB
Gherkin
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Feature: Talk with AI from Wiki Selection
As a user
I want to select text in wiki and talk with AI about it
So that I can get AI-powered explanations in a split view
Background:
Given I add test ai settings
@talkWithAI @mockOpenAI
Scenario: Talk with AI - complete workflow
Given I have started the mock OpenAI server
| response | stream |
| | false |
| | false |
| 访 | false |
| | false |
# Launch application after mock server is ready
Then I launch the TidGi application
And I wait for the page to load completely
And I should see a "page body" element with selector "body"
# Part 1: Create new split view from wiki selection
When I click on a "wiki workspace button" element with selector "div[data-testid^='workspace-']:has-text('wiki')"
# Wait for agent workspace to be created and activate it to ensure React components are mounted
Then I should see a "agent workspace button" element with selector "[data-testid='workspace-agent']"
When I click on "agent workspace button and wiki workspace button" elements with selectors:
| element description | selector |
| agent workspace button | [data-testid='workspace-agent'] |
| wiki workspace button | div[data-testid^='workspace-']:has-text('wiki') |
# Trigger "Talk with AI" - should create new split view
When I send ask AI with selection message with text "Click button on top-right of this card to start edit." and workspace "wiki"
Then I should see "split view container and wiki embed and chat input" elements with selectors:
| element description | selector |
| split view container | [data-testid='split-view-container'] |
| wiki embed | [data-testid='wiki-embed-view'] |
| chat input | [data-testid='agent-message-input'] |
And I confirm the "main" window browser view is positioned within visible window bounds
And I should see 2 messages in chat history
# Part 2: Reuse active split view - messages should accumulate (not reset)
When I send ask AI with selection message with text "How to edit?" and workspace "wiki"
Then I should see "split view container with 4 messages" elements with selectors:
| element description | selector |
| split view container with 4 messages| [data-testid='split-view-container'] |
# Should see 4 messages now (2 from part 1 + 2 new ones) - proves tab was reused
And I should see 4 messages in chat history
# Part 3: Create new tab when starting from regular chat
When I click on a "agent workspace button" element with selector "[data-testid='workspace-agent']"
When I click on "new tab button and search input and agent suggestion" elements with selectors:
| element description | selector |
| new tab button | [data-tab-id='new-tab-button'] |
| search input | .aa-Input |
| agent suggestion | [data-autocomplete-source-id="agentsSource"] .aa-ItemWrapper |
When I send ask AI with selection message with text "First question" and workspace "wiki"
Then I should see a "split view container" element with selector "[data-testid='split-view-container']"
# Should see only 2 messages (new tab was created, not reused)
And I should see 2 messages in chat history
# Part 4: Verify split view doesn't interfere with regular chat
# Create another regular chat tab
When I click on "new tab button and search input and agent suggestion" elements with selectors:
| element description | selector |
| new tab button | [data-tab-id='new-tab-button'] |
| search input | .aa-Input |
| agent suggestion | [data-autocomplete-source-id="agentsSource"] .aa-ItemWrapper |
# Now in regular chat tab - split view and browser view should not be visible
Then I should not see a "split view container" element with selector "[data-testid='split-view-container']"
And I confirm the "main" window browser view is not positioned within visible window bounds
# Switch to wiki workspace - browser view should exist for wiki
When I click on a "wiki workspace button" element with selector "div[data-testid^='workspace-']:has-text('wiki')"
And I wait for 1 seconds for "browser view repositioning after workspace switch"
Then I confirm the "main" window browser view is positioned within visible window bounds
@agent @mockOpenAI
Scenario: Wiki tiddler attachment with rendered content
# Start mock server and launch app (not in Background for this feature)
Given I have started the mock OpenAI server
| response | stream |
| wiki | false |
Then I launch the TidGi application
And I wait for the page to load completely
And I should see a "page body" element with selector "body"
# First, navigate to wiki workspace and create a test tiddler with wikitext content
When I click on a "default wiki workspace button" element with selector "div[data-testid^='workspace-']:has-text('wiki')"
Then the browser view should be loaded and visible
And I wait for "SSE backend ready" log marker "[test-id-SSE_READY]"
# Create a new tiddler with wikitext syntax
When I execute TiddlyWiki code in browser view: "$tw.wiki.addTiddler(new $tw.Tiddler({title: 'TestAttachmentTiddler', text: '!!WikiTestHeader\\n\\nThis is a test with WikiTestContentMarker123', type: 'text/vnd.tiddlywiki'}))"
# Navigate to agent workspace and create agent
When I click on "agent workspace button and create default agent button" elements with selectors:
| element description | selector |
| agent workspace button | [data-testid='workspace-agent'] |
| create default agent button | [data-testid='create-default-agent-button'] |
And I should see a "message input box" element with selector "[data-testid='agent-message-input']"
# Click attachment button to open autocomplete
When I click on a "attach button" element with selector "[data-testid='agent-attach-button']"
# Autocomplete should open showing image option + tiddler options
And I should see a "attachment autocomplete input" element with selector "[data-testid='attachment-autocomplete-input']"
And I should see a "attachment listbox" element with selector "[data-testid='attachment-listbox']"
# Click on our test tiddler option
When I click on a "test tiddler option" element with selector "[data-testid='attachment-option-tiddler-TestAttachmentTiddler']"
# Verify the chip is displayed
Then I should see a "wiki tiddler chip" element with selector "[data-testid='wiki-tiddler-chip-0']"
# Type message and send
When I click on a "message input textarea" element with selector "[data-testid='agent-message-input']"
When I type "" in "chat input" element with selector "[data-testid='agent-message-input']"
And I press "Enter" key
# Verify the mock server received the rendered content (wikitext converted to plain text)
Then the last AI request user message should contain "WikiTestContentMarker123"
And the last AI request user message should contain "Wiki Entry from"
And the last AI request user message should contain "TestAttachmentTiddler"
# Verify wikitext was converted to plain text (!! becomes "Header", not raw !!)
And the last AI request user message should contain "WikiTestHeader"
And the last AI request user message should not contain "!!"
@agent @mockOpenAI @streamingStatus @imageUpload
Scenario: Image upload streaming status and history verification
# Start mock server and launch app
Given I have started the mock OpenAI server
| response | stream |
| Received image and text | false |
| Received second message | false |
Then I launch the TidGi application
And I wait for the page to load completely
And I should see a "page body" element with selector "body"
# Navigate to agent workspace and open agent chat
When I click on an "agent workspace button" element with selector "[data-testid='workspace-agent']"
When I click on a "new tab button" element with selector "[data-tab-id='new-tab-button']"
And I should see a "search interface" element with selector ".aa-Autocomplete"
When I click on a "search input box" element with selector ".aa-Input"
And I should see an "autocomplete panel" element with selector ".aa-Panel"
When I click on an "agent suggestion" element with selector '[data-autocomplete-source-id="agentsSource"] .aa-ItemWrapper'
And I should see a "message input box" element with selector "[data-testid='agent-message-input']"
# Click attachment button to open autocomplete
When I click on a "attach button" element with selector "[data-testid='agent-attach-button']"
# Wait for autocomplete to open
And I should see a "attachment autocomplete input" element with selector "[data-testid='attachment-autocomplete-input']"
# Register Playwright filechooser intercept BEFORE clicking Add Image so the
# native OS dialog never appears; the chooser is resolved directly with the file.
When I prepare to select file "template/wiki/files/TiddlyWikiIconBlack.png" for file chooser
# Click on "Add Image" option — triggers fileInput.click() which fires filechooser
When I click on a "add image option" element with selector "[data-testid='attachment-option-image-AddImage']"
# Verify image preview appears
Then I should see an "attachment preview" element with selector "[data-testid='attachment-preview']"
# Send message with image
When I click on a "message input textarea" element with selector "[data-testid='agent-message-input']"
When I type "Describe this image" in "chat input" element with selector "[data-testid='agent-message-input']"
And I press "Enter" key
Then I should see 2 messages in chat history
# Verify image appears in chat history
And I should see a "message image attachment" element with selector "[data-testid='message-image-attachment']"
# Verify send button returned to normal after first message
And I should see a "send button icon" element with selector "[data-testid='send-icon']"
And I should not see a "cancel button icon" element with selector "[data-testid='cancel-icon']"
# Send second message to check history includes image
When I type "Continue" in "chat input" element with selector "[data-testid='agent-message-input']"
And I press "Enter" key
Then I should see 4 messages in chat history
# Verify send button is still normal after second message
And I should see a "send button icon" element with selector "[data-testid='send-icon']"
And I should not see a "cancel button icon" element with selector "[data-testid='cancel-icon']"