fix(suite): isolate restored bundle operations

Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
This commit is contained in:
XiaoSeS 2026-09-14 12:29:39 +08:00
parent 26c8938f72
commit eec7d14cfa
6 changed files with 83 additions and 9 deletions

View file

@ -18,6 +18,7 @@ const mocks = vi.hoisted(() => ({
packageFolder: vi.fn(),
toast: { error: vi.fn() },
navigate: vi.fn(),
requestedOperationIds: [] as Array<string | undefined>,
}))
vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) }))
@ -44,7 +45,10 @@ vi.mock('@/shared/hooks/use-suite-queries', () => ({
useConfirmSuiteBundle: () => mocks.confirm,
useCancelSuiteBundleOperation: () => mocks.cancel,
useRetrySuiteBundleOperation: () => mocks.retry,
useSuiteBundleOperation: () => mocks.operation,
useSuiteBundleOperation: (operationId?: string) => {
mocks.requestedOperationIds.push(operationId)
return mocks.operation
},
}))
function folderFile(path: string): File {
@ -83,6 +87,7 @@ describe('SuiteBundleImport', () => {
mocks.operation.data = undefined
mocks.operation.error = null
mocks.preview.isPending = false
mocks.requestedOperationIds = []
})
it('uploads one archive, requires explicit warning acceptance and confirms the exact preview', async () => {
@ -278,6 +283,7 @@ describe('SuiteBundleImport', () => {
mocks.operation.data = {
operationId: 'operation-1',
status: 'BLOCKED_RETRYABLE',
mode: 'CREATE',
members: [{ position: 0, status: 'FAILED_RETRYABLE', redacted: true }],
}
render(<SuiteBundleImport expectedMode="CREATE" />)
@ -329,6 +335,7 @@ describe('SuiteBundleImport', () => {
mocks.operation.data = {
operationId: 'operation-waiting',
status: 'WAITING_FOR_MEMBERS',
mode: 'CREATE',
members: [{
position: 0,
status: 'WAITING_FOR_MEMBER',
@ -360,6 +367,7 @@ describe('SuiteBundleImport', () => {
mocks.operation.data = {
operationId: 'operation-complete',
status: 'SUITE_DRAFT_CREATED',
mode: 'CREATE',
targetCoordinate: '@global/generated-suite',
targetVersion: '1.2.0',
members: [],
@ -373,4 +381,54 @@ describe('SuiteBundleImport', () => {
search: { version: '1.2.0' },
})
})
it('reads the new Suite recovery key when an UPDATE route changes without a full page reload', async () => {
window.sessionStorage.setItem(
'skillhub:suite-bundle-operation:UPDATE:@global/suite-a',
'operation-a',
)
window.sessionStorage.setItem(
'skillhub:suite-bundle-operation:UPDATE:@global/suite-b',
'operation-b',
)
const Harness = ({ coordinate }: { coordinate: string }) => (
<SuiteBundleImport
key={`UPDATE:${coordinate}`}
expectedMode="UPDATE"
expectedCoordinate={coordinate}
/>
)
const { rerender } = render(<Harness coordinate="@global/suite-a" />)
expect(mocks.requestedOperationIds[mocks.requestedOperationIds.length - 1]).toBe('operation-a')
rerender(<Harness coordinate="@global/suite-b" />)
await waitFor(() => expect(
mocks.requestedOperationIds[mocks.requestedOperationIds.length - 1]
).toBe('operation-b'))
expect(window.sessionStorage.getItem(
'skillhub:suite-bundle-operation:UPDATE:@global/suite-b'
)).toBe('operation-b')
})
it('clears a restored UPDATE operation that belongs to another Suite without exposing actions', async () => {
const key = 'skillhub:suite-bundle-operation:UPDATE:@global/suite-b'
window.sessionStorage.setItem(key, 'operation-a')
mocks.operation.data = {
operationId: 'operation-a',
status: 'BLOCKED_RETRYABLE',
mode: 'UPDATE',
targetCoordinate: '@global/suite-a',
members: [{ position: 0, status: 'FAILED_RETRYABLE', redacted: true }],
}
render(<SuiteBundleImport expectedMode="UPDATE" expectedCoordinate="@global/suite-b" />)
expect(screen.queryByText('suite.bundle.redactedMember')).toBeNull()
expect(screen.queryByRole('button', { name: /suite.bundle.retry/ })).toBeNull()
expect(screen.queryByRole('button', { name: 'suite.bundle.cancelOperation' })).toBeNull()
await waitFor(() => expect(window.sessionStorage.getItem(key)).toBeNull())
expect(screen.getByText('suite.bundle.uploadTitle')).not.toBeNull()
expect(mocks.toast.error).toHaveBeenCalledWith('suite.bundle.operationTargetMismatch')
})
})

View file

@ -71,12 +71,22 @@ export function SuiteBundleImport({ expectedMode, expectedCoordinate }: {
const selectionVersionRef = useRef(0)
const idempotencyKeyRef = useRef<string | null>(null)
const operationQuery = useSuiteBundleOperation(operationId)
const operation = operationQuery.data
const operationMatchesTarget = !operation
|| (operation.mode === expectedMode
&& (expectedCoordinate === undefined || operation.targetCoordinate === expectedCoordinate))
useEffect(() => () => {
selectionVersionRef.current += 1
requestRef.current?.abort()
}, [])
useEffect(() => writeStoredOperation(storageKey, operationId), [operationId, storageKey])
useEffect(() => {
if (!operationId || !operation || operationMatchesTarget) return
writeStoredOperation(storageKey)
setOperationId(undefined)
toast.error(t('suite.bundle.operationTargetMismatch'))
}, [operation, operationId, operationMatchesTarget, storageKey, t])
useEffect(() => {
if (!preview) return undefined
const timer = window.setInterval(() => setNow(Date.now()), 1_000)
@ -185,7 +195,9 @@ export function SuiteBundleImport({ expectedMode, expectedCoordinate }: {
}
if (operationId) {
const operation = operationQuery.data
if (!operationMatchesTarget) {
return <Card className="p-6 text-sm text-destructive" role="alert">{t('suite.bundle.operationTargetMismatch')}</Card>
}
const status = operation?.status
const draftCoordinate = splitCoordinate(operation?.targetCoordinate)
const resetOperation = () => {

View file

@ -2021,6 +2021,7 @@
"progressTitle": "Bundle publishing progress",
"loadingOperation": "Loading operation status",
"operationLoadFailed": "Unable to load the Bundle operation. Check your connection and try again.",
"operationTargetMismatch": "The restored Bundle operation does not match this Suite. Its recovery entry was cleared.",
"reloadOperation": "Reload",
"redactedMember": "Member hidden by access policy",
"viewMemberReview": "View Skill version and review status",

View file

@ -2018,6 +2018,7 @@
"progressTitle": "Ход публикации Bundle",
"loadingOperation": "Загрузка состояния операции",
"operationLoadFailed": "Не удалось загрузить операцию Bundle. Проверьте подключение и повторите попытку.",
"operationTargetMismatch": "Восстановленная операция Bundle не соответствует этому набору. Запись восстановления удалена.",
"reloadOperation": "Загрузить снова",
"redactedMember": "Участник скрыт политикой доступа",
"viewMemberReview": "Открыть версию Skill и состояние проверки",

View file

@ -2020,6 +2020,7 @@
"progressTitle": "Bundle 发布进度",
"loadingOperation": "正在读取操作状态",
"operationLoadFailed": "无法读取 Bundle 操作状态,请检查网络后重试。",
"operationTargetMismatch": "恢复的 Bundle 操作与当前套件不匹配,已清除该恢复记录。",
"reloadOperation": "重新读取",
"redactedMember": "无权查看的成员",
"viewMemberReview": "查看技能版本与审核状态",

View file

@ -39,6 +39,10 @@ export function SuiteEditor({ namespace: routeNamespace, slug: routeSlug, versio
}) {
const editing = mode === 'edit'
const creatingVersion = mode === 'new-version'
const bundleMode = creatingVersion ? 'UPDATE' : 'CREATE'
const bundleCoordinate = creatingVersion && routeNamespace && routeSlug
? `@${routeNamespace}/${routeSlug}`
: undefined
const loadingSource = editing || creatingVersion
const { t } = useTranslation()
const { hasRole } = useAuth()
@ -61,11 +65,7 @@ export function SuiteEditor({ namespace: routeNamespace, slug: routeSlug, versio
const [pendingVersionUpdate, setPendingVersionUpdate] = useState<SkillSuiteMemberCandidate | null>(null)
const [authoringMode, setAuthoringMode] = useState<'manual' | 'import'>(() => {
if (editing) return 'manual'
const bundleMode = creatingVersion ? 'UPDATE' : 'CREATE'
const coordinate = creatingVersion && routeNamespace && routeSlug
? `@${routeNamespace}/${routeSlug}`
: undefined
return hasStoredSuiteBundleOperation(bundleMode, coordinate) ? 'import' : 'manual'
return hasStoredSuiteBundleOperation(bundleMode, bundleCoordinate) ? 'import' : 'manual'
})
const debouncedQuery = useDebounce(candidateQuery.trim(), 250)
const { data: candidates, isLoading: isLoadingCandidates } = useSuiteMemberCandidates(
@ -250,8 +250,9 @@ export function SuiteEditor({ namespace: routeNamespace, slug: routeSlug, versio
{!editing && authoringMode === 'import' ? (
<SuiteBundleImport
expectedMode={creatingVersion ? 'UPDATE' : 'CREATE'}
expectedCoordinate={creatingVersion && existing ? `@${existing.namespace}/${existing.slug}` : undefined}
key={`${bundleMode}:${bundleCoordinate ?? 'new'}`}
expectedMode={bundleMode}
expectedCoordinate={creatingVersion && existing ? bundleCoordinate : undefined}
/>
) : (
<>