@@ -0,0 +1,147 @@
import fs from 'node:fs' ;
import path from 'node:path' ;
import assert from 'node:assert/strict' ;
import { randomUUID } from 'node:crypto' ;
import { spawn } from 'node:child_process' ;
import { chromium , expect } from 'playwright/test' ;
import { Store } from '../src/server/store.mjs' ;
import { WorkspaceManager } from '../src/core/workspaces/git-workspaces.mjs' ;
import { createDeliveryProject } from '../tests/fixtures/delivery-project.mjs' ;
import { captureWorkspaceSource , assertWorkspaceSource } from '../tests/fixtures/workspace-project.mjs' ;
// Every host/project is synthetic and independently owned. Never uses 4318.
const root = path . resolve ( '.qa' , 'workspace-ui-' + randomUUID ( ) ) , data = path . join ( root , 'data' ) ;
fs . mkdirSync ( root , { recursive : true } ) ; fs . mkdirSync ( '.qa/screenshots' , { recursive : true } ) ;
const store = new Store ( data ) , cases = { } , passed = [ ] , pageErrors = [ ] , requestFailures = [ ] , screenshots = [ ] , createPosts = [ ] ;
for ( const name of [ 'normal' , 'mobile' , 'recovery' , 'late-success' , 'late-failure' ] ) {
const projectRoot = path . join ( root , '独立工作区验收-' + name ) ; fs . mkdirSync ( projectRoot ) ; fs . writeFileSync ( path . join ( projectRoot , 'README.md' ) , '# Workspace UI fixture\n' ) ;
const project = store . addProject ( projectRoot ) , session = store . createSession ( project . id ) , f = { root , data , projectRoot , project , session , store } ;
createDeliveryProject ( f ) ; session . title = '工作区验收 ' + name ; store . save ( session ) ;
const before = captureWorkspaceSource ( f ) ; cases [ name ] = { ... f , before } ;
if ( name === 'recovery' ) {
const manager = new WorkspaceManager ( store , { faultInjector : ( phase , record ) => {
if ( phase === 'git_finished' ) fs . writeFileSync ( path . join ( record . root , 'src/add.mjs' ) , 'Owner registration recovery content\n' ) ;
} } ) ;
const record = await manager . prepare ( session , '创建中断核对' ) ; await ( await manager . start ( session , record . id , record . fingerprint ) ) . promise ;
assert . equal ( record . status , 'unknown' ) ; cases [ name ] . record = structuredClone ( record ) ;
}
}
const peer = store . createSession ( cases . normal . project . id , '同项目其他会话' ) ; store . close ( ) ;
const env = { ... process . env , AGENT _WORKBENCH _APP _DIR : process . cwd ( ) , AGENT _WORKBENCH _DATA _DIR : data , AGENT _WORKBENCH _PORT : '0' } ; delete env . AGENT _WORKBENCH _API _KEY ;
const host = spawn ( process . execPath , [ 'dist-server/server.mjs' ] , { cwd : process . cwd ( ) , env , windowsHide : true , stdio : [ 'ignore' , 'pipe' , 'pipe' ] } ) ;
let url = '' , output = '' , errors = '' ;
host . stdout . on ( 'data' , bytes => { output += bytes . toString ( 'utf8' ) ; for ( const line of output . split ( '\n' ) ) { try { const value = JSON . parse ( line ) ; if ( value . ready ) url = value . url ; } catch { } } } ) ;
host . stderr . on ( 'data' , bytes => { errors += bytes . toString ( 'utf8' ) ; } ) ;
const wait = async predicate => { const deadline = Date . now ( ) + 30000 ; while ( ! ( await predicate ( ) ) ) { if ( Date . now ( ) > deadline ) throw new Error ( 'Workspace UI state timed out: ' + errors ) ; await new Promise ( resolve => setTimeout ( resolve , 30 ) ) ; } } ;
await wait ( ( ) => url || host . exitCode !== null ) ; assert . ok ( url , errors ) ; assert . notEqual ( new URL ( url ) . port , '4318' ) ;
const bootstrap = await ( await fetch ( url + '/api/bootstrap' ) ) . json ( ) ;
assert . equal ( bootstrap . config . hasKey , false ) ; assert . equal ( bootstrap . config . mode , 'demo' ) ;
const api = async ( route , input ) => {
const response = await fetch ( url + '/api' + route , { method : input === undefined ? 'GET' : 'POST' , headers : { 'Content-Type' : 'application/json' , 'X-Workbench-Token' : bootstrap . token , Connection : 'close' } , ... ( input === undefined ? { } : { body : JSON . stringify ( input ) } ) } ) ;
const value = await response . json ( ) ; assert . ok ( response . ok , value . error ) ; return value ;
} ;
const browser = await chromium . launch ( { executablePath : 'C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe' , headless : true } ) ;
const page = await browser . newPage ( { viewport : { width : 1440 , height : 1000 } } ) ;
page . on ( 'pageerror' , error => pageErrors . push ( error . message ) ) ;
page . on ( 'requestfailed' , request => { if ( request . resourceType ( ) !== 'eventsource' ) requestFailures . push ( request . url ( ) + ': ' + request . failure ( ) ? . errorText ) ; } ) ;
page . on ( 'request' , request => { if ( request . method ( ) === 'POST' && /\/workspaces\/[^/]+\/create$/ . test ( new URL ( request . url ( ) ) . pathname ) ) createPosts . push ( new URL ( request . url ( ) ) . pathname ) ; } ) ;
const panel = ( ) => page . getByLabel ( '独立开发工作区' , { exact : true } ) ;
const review = ( ) => page . getByRole ( 'dialog' , { name : '创建独立工作区' , exact : true } ) ;
const mark = value => { passed . push ( value ) ; console . log ( 'PASS ' + value ) ; } ;
const records = f => api ( '/sessions/' + f . session . id + '/workspaces' ) ;
const latest = async f => ( await records ( f ) ) . at ( - 1 ) ;
async function choose ( name ) {
const f = cases [ name ] ;
if ( ! await page . locator ( '.project-sidebar' ) . isVisible ( ) ) await page . getByRole ( 'button' , { name : '展开项目栏' , exact : true } ) . click ( ) ;
await page . getByLabel ( '当前项目' , { exact : true } ) . selectOption ( f . project . id ) ;
if ( ! await page . locator ( '.session-item.selected' ) . filter ( { hasText : f . session . title } ) . count ( ) ) await page . locator ( '.session-select' ) . filter ( { hasText : f . session . title } ) . click ( ) ;
if ( page . viewportSize ( ) . width <= 620 && await page . locator ( '.project-sidebar' ) . isVisible ( ) ) await page . getByRole ( 'button' , { name : '收起项目栏' , exact : true } ) . click ( ) ;
await page . getByRole ( 'button' , { name : '查看任务记录' , exact : true } ) . click ( ) ; await expect ( panel ( ) ) . toBeVisible ( ) ; return f ;
}
async function fit ( name ) {
const size = await page . evaluate ( ( ) => {
const modal = document . querySelector ( '[role="dialog"]' ) , close = modal ? . querySelector ( '[aria-label="关闭窗口"]' ) ;
const rect = modal ? . getBoundingClientRect ( ) , closeRect = close ? . getBoundingClientRect ( ) ;
return { width : innerWidth , height : innerHeight , scrollWidth : document . documentElement . scrollWidth , top : rect ? . top , bottom : rect ? . bottom , left : rect ? . left , right : rect ? . right , closeBottom : closeRect ? . bottom } ;
} ) ;
assert . ok ( size . scrollWidth <= size . width + 1 , name + ' page horizontal overflow' ) ;
if ( size . top !== undefined ) { assert . ok ( size . top >= 0 && size . left >= 0 && size . right <= size . width + 1 && size . bottom <= size . height + 1 , name + ' modal bounds' ) ; assert . ok ( size . closeBottom <= size . height ) ; }
const file = '.qa/screenshots/1.6-workspace-' + name + '.png' ; await page . screenshot ( { path : file , scale : 'css' } ) ; screenshots . push ( file ) ;
}
try {
await page . goto ( url ) ; await page . waitForLoadState ( 'networkidle' ) ;
const normal = await choose ( 'normal' ) ;
await panel ( ) . getByLabel ( '工作区名称' ) . fill ( '桌面独立开发' ) ; await panel ( ) . getByRole ( 'button' , { name : '准备独立工作区' , exact : true } ) . click ( ) ;
await expect ( review ( ) ) . toBeVisible ( ) ; await expect ( review ( ) ) . toContainText ( '3 项未提交改动' ) ; await review ( ) . getByText ( '查看来源已有改动' , { exact : true } ) . click ( ) ;
await expect ( review ( ) ) . toContainText ( 'owner-staged.txt' ) ; const planned = await latest ( normal ) ; assert . equal ( planned . status , 'prepared' ) ; assert . equal ( fs . existsSync ( planned . root ) , false ) ;
await fit ( 'review-desktop' ) ; mark ( 'desktop preview seals the committed version and shows existing staged/unstaged/untracked source changes' ) ;
await review ( ) . getByRole ( 'button' , { name : '确认创建工作区' , exact : true } ) . evaluate ( button => { button . click ( ) ; button . click ( ) ; } ) ;
await expect ( review ( ) ) . toBeHidden ( ) ; await wait ( async ( ) => ( await latest ( normal ) ) . status === 'ready' ) ;
await expect ( panel ( ) ) . toContainText ( '可以开始开发' ) ; assert . equal ( createPosts . filter ( route => route . includes ( planned . id ) ) . length , 1 ) ;
assertWorkspaceSource ( assert , normal , normal . before , planned . branch ) ; await fit ( 'ready-desktop' ) ; mark ( 'repeated create clicks execute once and preserve the exact source index and owner files' ) ;
await panel ( ) . getByRole ( 'button' , { name : '打开此工作区' , exact : true } ) . click ( ) ;
await expect ( page . getByLabel ( '当前项目' , { exact : true } ) ) . toHaveValue ( planned . childProjectId ) ; await expect ( panel ( ) ) . toContainText ( '当前独立分支' ) ;
await expect ( panel ( ) ) . toContainText ( planned . branch ) ; await panel ( ) . getByRole ( 'button' , { name : '返回来源会话' , exact : true } ) . click ( ) ;
await expect ( page . getByLabel ( '当前项目' , { exact : true } ) ) . toHaveValue ( normal . project . id ) ; await expect ( panel ( ) ) . toContainText ( '桌面独立开发' ) ;
await page . reload ( ) ; await page . waitForLoadState ( 'networkidle' ) ; await page . getByRole ( 'button' , { name : '查看任务记录' , exact : true } ) . click ( ) ;
await expect ( panel ( ) ) . toContainText ( '可以开始开发' ) ; mark ( 'opening the child, returning to the source and reloading retain stable project/session identity' ) ;
await page . locator ( '.session-select' ) . filter ( { hasText : peer . title } ) . click ( ) ; await expect ( panel ( ) ) . toContainText ( '转到所属会话' ) ;
await panel ( ) . getByRole ( 'button' , { name : '转到所属会话' , exact : true } ) . click ( ) ; await expect ( page . locator ( '.session-item.selected' ) ) . toContainText ( normal . session . title ) ;
mark ( 'a record owned by another session navigates to that saved owner instead of accepting its actions' ) ;
const recovery = await choose ( 'recovery' ) ; await expect ( panel ( ) ) . toContainText ( '创建结果需要核对' ) ;
await panel ( ) . getByRole ( 'button' , { name : '核对创建结果' , exact : true } ) . click ( ) ;
await expect ( panel ( ) . getByRole ( 'button' , { name : '核对创建结果' , exact : true } ) ) . toBeEnabled ( ) ;
assert . equal ( ( await latest ( recovery ) ) . status , 'unknown' ) ; await fit ( 'recovery-desktop' ) ;
await panel ( ) . getByRole ( 'button' , { name : '保留当前外部状态' , exact : true } ) . click ( ) ;
await page . getByRole ( 'dialog' , { name : '保留当前外部状态' , exact : true } ) . getByRole ( 'button' , { name : '确认保留当前状态' , exact : true } ) . click ( ) ;
await expect ( panel ( ) ) . toContainText ( '已保留外部状态' ) ; assert . equal ( ( await latest ( recovery ) ) . status , 'resolved_external' ) ;
assert . equal ( fs . readFileSync ( path . join ( recovery . record . root , 'src/add.mjs' ) , 'utf8' ) , 'Owner registration recovery content\n' ) ;
mark ( 'explicit recovery preserves outside content and acknowledgement does not report successful creation' ) ;
await page . setViewportSize ( { width : 390 , height : 844 } ) ; await expect ( page . locator ( '.project-sidebar' ) ) . toBeHidden ( ) ; const mobile = await choose ( 'mobile' ) ;
await panel ( ) . getByLabel ( '工作区名称' ) . fill ( '手机审查工作区' ) ; await panel ( ) . getByRole ( 'button' , { name : '准备独立工作区' , exact : true } ) . click ( ) ;
await expect ( review ( ) ) . toBeVisible ( ) ; await fit ( 'review-mobile' ) ;
await review ( ) . getByRole ( 'button' , { name : '确认创建工作区' , exact : true } ) . click ( ) ; await expect ( review ( ) ) . toBeHidden ( ) ;
await wait ( async ( ) => ( await latest ( mobile ) ) . status === 'ready' ) ; const mobileRecord = await latest ( mobile ) ;
await panel ( ) . getByRole ( 'button' , { name : '打开此工作区' , exact : true } ) . click ( ) ; await expect ( panel ( ) ) . toContainText ( mobileRecord . branch ) ; await fit ( 'child-mobile' ) ;
await panel ( ) . getByRole ( 'button' , { name : '返回来源会话' , exact : true } ) . click ( ) ; await expect ( panel ( ) ) . toContainText ( '手机审查工作区' ) ;
assertWorkspaceSource ( assert , mobile , mobile . before , mobileRecord . branch ) ; mark ( '390× 844 preview/create/open/return stays within viewport and preserves source ownership' ) ;
await page . setViewportSize ( { width : 1440 , height : 1000 } ) ;
for ( const kind of [ 'late-failure' , 'late-success' ] ) {
const f = await choose ( kind ) , pattern = '**/api/sessions/' + f . session . id + '/workspaces' ;
let release , held , recorded ;
const heldPromise = new Promise ( resolve => { held = resolve ; } ) , releasePromise = new Promise ( resolve => { release = resolve ; } ) ;
await page . route ( pattern , async route => {
if ( route . request ( ) . method ( ) !== 'POST' ) return route . continue ( ) ;
const response = kind === 'late-success' ? await route . fetch ( ) : null ;
if ( response ) recorded = await response . json ( ) ; held ( ) ; await releasePromise ;
if ( response ) await route . fulfill ( { response } ) ; else await route . fulfill ( { status : 500 , contentType : 'application/json' , body : JSON . stringify ( { error : 'OLD-SOURCE-ONLY delayed failure' } ) } ) ;
} ) ;
await panel ( ) . getByLabel ( '工作区名称' ) . fill ( kind ) ; await panel ( ) . getByRole ( 'button' , { name : '准备独立工作区' , exact : true } ) . click ( ) ; await heldPromise ;
await choose ( 'normal' ) ; release ( ) ; await page . waitForResponse ( response => response . url ( ) . endsWith ( '/sessions/' + f . session . id + '/workspaces' ) && response . request ( ) . method ( ) === 'POST' ) ;
await expect ( review ( ) ) . toBeHidden ( ) ; await expect ( page . locator ( '.toast' ) . filter ( { hasText : 'OLD-SOURCE-ONLY' } ) ) . toHaveCount ( 0 ) ;
await expect ( page . getByLabel ( '当前项目' , { exact : true } ) ) . toHaveValue ( normal . project . id ) ;
if ( recorded ) { assert . equal ( ( await latest ( f ) ) . id , recorded . id ) ; assert . equal ( ( await latest ( f ) ) . status , 'prepared' ) ; assert . equal ( fs . existsSync ( recorded . root ) , false ) ; }
await page . unroute ( pattern ) ; mark ( kind + ' stays with the original session after project switching' ) ;
}
await choose ( 'normal' ) ; let releaseNavigation , heldNavigation ;
const navHeld = new Promise ( resolve => { heldNavigation = resolve ; } ) , navRelease = new Promise ( resolve => { releaseNavigation = resolve ; } ) ;
await page . route ( '**/api/bootstrap' , async route => { const response = await route . fetch ( ) ; heldNavigation ( ) ; await navRelease ; await route . fulfill ( { response } ) ; } ) ;
await panel ( ) . getByRole ( 'button' , { name : '打开此工作区' , exact : true } ) . click ( ) ; await navHeld ;
await page . locator ( '.inspector-tabs' ) . getByRole ( 'button' , { name : /^文件/ } ) . click ( ) ; releaseNavigation ( ) ;
await page . waitForResponse ( response => response . url ( ) . endsWith ( '/api/bootstrap' ) ) ;
await expect ( page . getByLabel ( '当前项目' , { exact : true } ) ) . toHaveValue ( normal . project . id ) ; await expect ( panel ( ) ) . toBeHidden ( ) ;
await page . unroute ( '**/api/bootstrap' ) ; mark ( 'a delayed bootstrap cannot navigate after the task panel has unmounted' ) ;
assert . equal ( pageErrors . length , 0 , pageErrors . join ( '\n' ) ) ; assert . equal ( requestFailures . length , 0 , requestFailures . join ( '\n' ) ) ;
const result = { success : true , version : bootstrap . version , at : new Date ( ) . toISOString ( ) , browser : browser . version ( ) , passed , pageErrors , requestFailures , screenshots , projectRoot : root ,
scope : 'Independent synthetic Git projects and real browser flows. Desktop/390× 844, actual worktree creation, source preservation, explicit external recovery, ownership navigation, delayed success/error/navigation. No production service or real model credential used.' } ;
fs . writeFileSync ( 'docs/workspaces-ui-validation.json' , JSON . stringify ( result , null , 2 ) + '\n' ) ; console . log ( JSON . stringify ( result , null , 2 ) ) ;
} catch ( error ) {
await page . screenshot ( { path : '.qa/screenshots/1.6-workspace-failure.png' , scale : 'css' } ) ;
fs . writeFileSync ( '.qa/workspaces-ui-failure.json' , JSON . stringify ( { success : false , message : error . message , passed , pageErrors , requestFailures } , null , 2 ) ) ; throw error ;
} finally {
await browser . close ( ) ; try { await api ( '/shutdown' , { } ) ; await wait ( ( ) => host . exitCode !== null ) ; } catch { if ( host . exitCode === null ) host . kill ( ) ; }
}