skillhub/plugins/dsh/test/integration/change-preview-component.tsx
XiaoSeS e99c2ce44e
feat(dsh): compact workbench and file browser (#1080)
Apply the reviewed compact DSH workbench, bounded file tree preview and lifecycle navigation fixes.

Preserve registry/session boundaries and managed installation semantics.

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
2026-11-10 14:35:47 +08:00

20 lines
4.2 KiB
TypeScript

import { createRoot } from 'react-dom/client';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ChangePreview } from '../../src/client/change-preview.js';
import { NativeControlStyles } from '../../src/client/native-control-styles.js';
import { UpgradeRequests } from '../../src/client/upgrade-requests.js';
import type { ChangesView, NativeOperation } from '../../src/host/types.js';
import type { UpgradeServices } from '../../src/client/upgrade-control.js';
const params = new URLSearchParams(location.search); const scenario = params.get('scenario');
const host = '22222222-2222-4222-8222-222222222222'; const plan = '11111111-1111-4111-8111-111111111111';
const audit: string[] = []; let files: ChangesView['changes'] = scenario === 'clean' ? [] : [{path: 'SKILL.md',kind:'modified'},{path:'templates/team.md',kind:'added'}];
let last: NativeOperation | undefined; let operationReads = 0;
const operation = (kind: 'upgrade' | 'reset-files'): NativeOperation => ({ operationId: crypto.randomUUID(),revision:0,kind,namespace:'demo',slug:'writer',version:kind==='upgrade'?'1.3.0':'1.2.0',targetLabel:'当前项目',phase:'completed',files:kind==='upgrade'?'upgraded':'files-reset',discovery:'unconfirmed',recovery:'not-needed',warningCount:0,context:{sourceIdentity:'https://example.test',sourceLabel:'测试来源',sourceRevision:'source',hostLabel:'Host',hostInstanceId:host,initiatedFrom:'plaza',createdAt:new Date().toISOString(),updatedAt:new Date().toISOString()}});
const view = (kind: 'local'|'version'): ChangesView => ({ status:'ready',reason:'',snapshotId:plan,expiresAt:new Date(Date.now()+300000).toISOString(),kind,version:'1.2.0',candidateVersion:kind==='version'?'1.3.0':null,changes:kind==='local'?files:[{path:'SKILL.md',kind:'modified'}] });
const ok = <T,>(value:T) => ({ok:true as const,value});
const services: UpgradeServices & {changes: NonNullable<UpgradeServices['changes']>} = {sessionId:'fixture',upgradeRequests:new UpgradeRequests(()=>sessionStorage),compatibility:async()=>ok({dependencyBaseline:'matched',candidateDshVersion:'0.2.1-rc.2',localWritesAllowed:scenario!=='readonly'}),
prepareUpgrade:async()=>{throw new Error('Legacy prepare must not run');},executePlan:async()=>{audit.push('upgrade');last=operation('upgrade');return ok(scenario === 'operation-null' ? { ...last, phase: 'accepted' as const, files: 'pending' as const } : last);},operation:async()=>{operationReads++; return ok(scenario === 'operation-null' && operationReads === 1 ? null : last??null);},recoverOperation:async()=>ok(last??null),
changes:{inspect:async()=>{audit.push('inspect');return ok(view('local'));},diff:async()=>{audit.push('diff');return ok({path:'SKILL.md',state:'text' as const,lines:Array.from({length:90},(_,i)=>({kind:'added' as const,text:`第 ${i} 行文件差异` })),truncated:false});},preview:async(_r,_i,_s,_v,backup)=>{audit.push(backup?'backup':'preview');if(scenario==='backup-fail'&&backup)throw new Error('备份失败,文件未修改。');return ok({...view('version'),backupSaved:backup,backupLocation:backup?'/fixture/dsh-home/skillhub/backups/verified-backup':undefined});},confirm:async()=>{audit.push('confirm');return ok({status:'ready',reason:'',planId:plan,expiresAt:new Date(Date.now()+300000).toISOString(),hostInstanceId:host,currentVersion:'1.2.0',candidateVersion:'1.3.0',targets:[]});},prepareReset:async(_r,_i,_s,paths)=>{audit.push('prepare-reset');return ok({planId:plan,expiresAt:new Date(Date.now()+300000).toISOString(),hostInstanceId:host,paths:files.filter(f=>paths.includes(f.path))});},reset:async()=>{audit.push('reset');files=[];last=operation('reset-files');return ok(last);}}};
Object.assign(window,{changeAudit:()=>audit});
const root=document.createElement('div');root.className='skillhub-controls';root.style.cssText='max-width:850px;margin:24px auto;padding:16px';document.body.append(root);
createRoot(root).render(<QueryClientProvider client={new QueryClient({defaultOptions:{queries:{retry:false}}})}><NativeControlStyles/><ChangePreview services={services} revision="source" itemKey="item" upgrade={scenario!=='local'} onCompleted={version=>{audit.push('completed:'+version);}} onClose={()=>audit.push('closed')}/></QueryClientProvider>);