mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-06 16:24:17 +00:00
Compare commits
6
Commits
v0.6.2
...
fbff404b9b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fbff404b9b | ||
|
|
c2348b0e68 | ||
|
|
ff3d71ed3d | ||
|
|
77bd1422d8 | ||
|
|
9529c63c0a | ||
|
|
3b00a42a89 |
@@ -8,10 +8,18 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
|
- **upload**: Allow users to cancel running upload tasks directly from the upload tasks sidebar with a confirmation dialog, preserving already completed files while discarding incomplete assets
|
||||||
|
- **upload**: Improve upload resilience during network drops, device sleep, and transient gateway errors by adding state-based exponential backoff retries and extending upload link expiration to 12 hours
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
- **upload**: Display a clear diagnostic error toast when remote file uploads fail due to an unconfigured or localhost S3 storage endpoint
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
- **upload**: Optimize file status polling during upload and transcoding by eliminating redundant polling during active uploads and batching transcoding status checks across multiple files
|
||||||
|
- **agent**: ⚠️ **Important Behavior Change**: Autofill and Embedding agents will no longer post completion comments when finishing successfully to prevent clutter in asset comment feeds. Comments are now only posted if a task encounters an error.
|
||||||
|
|
||||||
## [0.6.2] - 2026-10-29
|
## [0.6.2] - 2026-10-29
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
@@ -38,15 +38,16 @@ Shumai is configured via environment variables. You can define these variables i
|
|||||||
|
|
||||||
Shumai supports storing files locally or on S3-compatible endpoints.
|
Shumai supports storing files locally or on S3-compatible endpoints.
|
||||||
|
|
||||||
| Variable | Description | Example / Default |
|
| Variable | Description | Example / Default |
|
||||||
| :------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------------------------------ |
|
| :-------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------ | :------------------------------------------------------------------------ |
|
||||||
| `STORAGE_BACKEND` | Storage type. Use `local` for local file system storage, or `s3` for S3-compatible cloud storage. | `local` (default) or `s3` |
|
| `STORAGE_BACKEND` | Storage type. Use `local` for local file system storage, or `s3` for S3-compatible cloud storage. | `local` (default) or `s3` |
|
||||||
| `AWS_ENDPOINT_URL_S3` | Endpoint for S3 storage or local storage. Used by the browser to build file upload URLs. Must include port if deploying remotely or using custom ports. | `http://localhost:3000` (respects `SHUMAI_SERVER_PORT` fallback if unset) |
|
| `AWS_ENDPOINT_URL_S3` | Endpoint for S3 storage or local storage. Used by the browser to build file upload URLs. Must include port if deploying remotely or using custom ports. | `http://localhost:3000` (respects `SHUMAI_SERVER_PORT` fallback if unset) |
|
||||||
| `S3_BUCKET` | S3 bucket name. | `shumai` (default) |
|
| `S3_BUCKET` | S3 bucket name. | `shumai` (default) |
|
||||||
| `S3_ACCESS_KEY_ID` | Access key credentials for S3. | `minioadmin` (default) |
|
| `S3_ACCESS_KEY_ID` | Access key credentials for S3. | `minioadmin` (default) |
|
||||||
| `S3_SECRET_ACCESS_KEY` | Secret key credentials for S3. | `minioadmin` (default) |
|
| `S3_SECRET_ACCESS_KEY` | Secret key credentials for S3. | `minioadmin` (default) |
|
||||||
| `S3_REGION` | S3 storage region. | `auto` (default) |
|
| `S3_REGION` | S3 storage region. | `auto` (default) |
|
||||||
| `PRESIGNED_URL_EXPIRES_IN` | Number of hours for which generated presigned download/upload links are valid. | `5` (default) |
|
| `PRESIGNED_URL_EXPIRES_IN` | Number of hours for which generated presigned download links are valid. | `5` (default) |
|
||||||
|
| `PRESIGNED_UPLOAD_URL_EXPIRES_IN` | Number of hours for which generated presigned upload links are valid. Falls back to `PRESIGNED_URL_EXPIRES_IN` if unset. | `12` (default) |
|
||||||
|
|
||||||
### Local Storage Port Configuration
|
### Local Storage Port Configuration
|
||||||
|
|
||||||
|
|||||||
@@ -13,15 +13,16 @@ Using external S3 storage is highly recommended for production teams. It allows
|
|||||||
|
|
||||||
To enable S3-compatible storage, update the following environment variables in your `.env` file or Docker Compose configuration:
|
To enable S3-compatible storage, update the following environment variables in your `.env` file or Docker Compose configuration:
|
||||||
|
|
||||||
| Variable | Description | Required | Default / Example |
|
| Variable | Description | Required | Default / Example |
|
||||||
| :------------------------- | :------------------------------------------------------------------------ | :------- | :----------------------------------- |
|
| :-------------------------------- | :------------------------------------------------------------------------------------------------------------------ | :------- | :----------------------------------- |
|
||||||
| `STORAGE_BACKEND` | Set to `s3` to enable S3 compatible storage. | Yes | `s3` |
|
| `STORAGE_BACKEND` | Set to `s3` to enable S3 compatible storage. | Yes | `s3` |
|
||||||
| `S3_BUCKET` | The name of the S3 bucket where files will be stored. | Yes | `shumai-bucket` |
|
| `S3_BUCKET` | The name of the S3 bucket where files will be stored. | Yes | `shumai-bucket` |
|
||||||
| `S3_REGION` | The region of your S3 bucket. | No | `auto` |
|
| `S3_REGION` | The region of your S3 bucket. | No | `auto` |
|
||||||
| `S3_ACCESS_KEY_ID` | The access key ID for your storage service. | Yes | `your-access-key-id` |
|
| `S3_ACCESS_KEY_ID` | The access key ID for your storage service. | Yes | `your-access-key-id` |
|
||||||
| `S3_SECRET_ACCESS_KEY` | The secret access key for your storage service. | Yes | `your-secret-access-key` |
|
| `S3_SECRET_ACCESS_KEY` | The secret access key for your storage service. | Yes | `your-secret-access-key` |
|
||||||
| `AWS_ENDPOINT_URL_S3` | The endpoint URL of your S3 compatible storage provider. | Yes | `https://s3.us-east-1.amazonaws.com` |
|
| `AWS_ENDPOINT_URL_S3` | The endpoint URL of your S3 compatible storage provider. | Yes | `https://s3.us-east-1.amazonaws.com` |
|
||||||
| `PRESIGNED_URL_EXPIRES_IN` | Duration in hours before generated presigned download/upload URLs expire. | No | `5` |
|
| `PRESIGNED_URL_EXPIRES_IN` | Duration in hours before generated presigned download URLs expire. | No | `5` |
|
||||||
|
| `PRESIGNED_UPLOAD_URL_EXPIRES_IN` | Duration in hours before generated presigned upload URLs expire. Falls back to `PRESIGNED_URL_EXPIRES_IN` if unset. | No | `12` |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import {
|
|||||||
import * as piAgent from '../index'
|
import * as piAgent from '../index'
|
||||||
import { type AgentHarness, type Session } from '@earendil-works/pi-agent-core'
|
import { type AgentHarness, type Session } from '@earendil-works/pi-agent-core'
|
||||||
import { DatabaseSessionStorage, type DatabaseSessionMetadata } from '../database-session-storage'
|
import { DatabaseSessionStorage, type DatabaseSessionMetadata } from '../database-session-storage'
|
||||||
import { triggerLocalCancel, runInLocalTaskContext } from '@shumai/workflow-core'
|
import { triggerLocalCancel, runInLocalTaskContext, DBOS, DBOSError } from '@shumai/workflow-core'
|
||||||
import { logger } from '@shumai/core/src/logger'
|
import { logger } from '@shumai/core/src/logger'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -1240,6 +1240,28 @@ describe('Agent Database Activities Integration', () => {
|
|||||||
const deleted = await prisma.assetComment.findUnique({ where: { id: comment.id } })
|
const deleted = await prisma.assetComment.findUnique({ where: { id: comment.id } })
|
||||||
expect(deleted).toBeNull()
|
expect(deleted).toBeNull()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should create comment even if cancelled with DBOSWorkflowCancelledError (runOnCancel: true)', async () => {
|
||||||
|
const realCancelErr = new DBOSError.DBOSWorkflowCancelledError('test-workflow-id')
|
||||||
|
const isWithinWorkflowSpy = vi.spyOn(DBOS, 'isWithinWorkflow').mockReturnValue(true)
|
||||||
|
const runStepSpy = vi.spyOn(DBOS, 'runStep').mockRejectedValue(realCancelErr)
|
||||||
|
|
||||||
|
const comment = await createCommentActivity({
|
||||||
|
assetId: asset.id,
|
||||||
|
message: 'Autofill failed: workflow cancelled',
|
||||||
|
agentId: 'default',
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(comment.id).toBeDefined()
|
||||||
|
expect(comment.message).toBe('Autofill failed: workflow cancelled')
|
||||||
|
|
||||||
|
const savedComment = await prisma.assetComment.findUnique({ where: { id: comment.id } })
|
||||||
|
expect(savedComment).not.toBeNull()
|
||||||
|
expect(savedComment?.message).toBe('Autofill failed: workflow cancelled')
|
||||||
|
|
||||||
|
isWithinWorkflowSpy.mockRestore()
|
||||||
|
runStepSpy.mockRestore()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Context Retrievals', () => {
|
describe('Context Retrievals', () => {
|
||||||
|
|||||||
@@ -969,78 +969,86 @@ export const deleteCommentActivity = defineStep(async function deleteCommentActi
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
export const createCommentActivity = defineStep(async function createCommentActivity(params: {
|
export const createCommentActivity = defineStep(
|
||||||
assetId: string
|
async function createCommentActivity(params: {
|
||||||
message: string
|
assetId: string
|
||||||
sessionId?: string | null
|
message: string
|
||||||
agentId?: string | null
|
sessionId?: string | null
|
||||||
replyToId?: string | null
|
agentId?: string | null
|
||||||
}) {
|
replyToId?: string | null
|
||||||
if (params.sessionId) {
|
}) {
|
||||||
const agentId = params.agentId && params.agentId !== 'default' ? params.agentId : 'default'
|
if (params.sessionId) {
|
||||||
|
const agentId = params.agentId && params.agentId !== 'default' ? params.agentId : 'default'
|
||||||
|
|
||||||
// Fetch teamId from asset
|
// Fetch teamId from asset
|
||||||
const asset = await prisma.asset.findUnique({
|
const asset = await prisma.asset.findUnique({
|
||||||
where: { id: params.assetId },
|
where: { id: params.assetId },
|
||||||
include: { project: true },
|
include: { project: true },
|
||||||
})
|
|
||||||
if (!asset || !asset.project) {
|
|
||||||
throw ApplicationFailure.create({ message: 'asset or project not found', nonRetryable: true })
|
|
||||||
}
|
|
||||||
const teamId = asset.project.teamId
|
|
||||||
|
|
||||||
// Ensure user exists for agent
|
|
||||||
const userExists = await prisma.user.findUnique({ where: { id: agentId } })
|
|
||||||
if (!userExists) {
|
|
||||||
await prisma.user.create({
|
|
||||||
data: {
|
|
||||||
id: agentId,
|
|
||||||
name: 'Ai Agent',
|
|
||||||
email: `${agentId}@shumai.ai`,
|
|
||||||
type: 'agent',
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
if (!asset || !asset.project) {
|
||||||
|
throw ApplicationFailure.create({
|
||||||
|
message: 'asset or project not found',
|
||||||
|
nonRetryable: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
const teamId = asset.project.teamId
|
||||||
|
|
||||||
// Ensure agent exists
|
// Ensure user exists for agent
|
||||||
const agentExists = await prisma.agent.findUnique({ where: { id: agentId } })
|
const userExists = await prisma.user.findUnique({ where: { id: agentId } })
|
||||||
if (!agentExists) {
|
if (!userExists) {
|
||||||
await prisma.agent.create({
|
await prisma.user.create({
|
||||||
data: {
|
data: {
|
||||||
id: agentId,
|
id: agentId,
|
||||||
teamId,
|
name: 'Ai Agent',
|
||||||
type: 'chat',
|
email: `${agentId}@shumai.ai`,
|
||||||
config: {
|
type: 'agent',
|
||||||
provider: 'openai',
|
|
||||||
model: 'gpt-4',
|
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure agent exists
|
||||||
|
const agentExists = await prisma.agent.findUnique({ where: { id: agentId } })
|
||||||
|
if (!agentExists) {
|
||||||
|
await prisma.agent.create({
|
||||||
|
data: {
|
||||||
|
id: agentId,
|
||||||
|
teamId,
|
||||||
|
type: 'chat',
|
||||||
|
config: {
|
||||||
|
provider: 'openai',
|
||||||
|
model: 'gpt-4',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure agent session exists
|
||||||
|
const sessionExists = await prisma.agentSession.findUnique({
|
||||||
|
where: { id: params.sessionId },
|
||||||
})
|
})
|
||||||
|
if (!sessionExists) {
|
||||||
|
await prisma.agentSession.create({
|
||||||
|
data: {
|
||||||
|
id: params.sessionId,
|
||||||
|
agentId: agentId,
|
||||||
|
cwd: process.cwd(),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure agent session exists
|
return prisma.assetComment.create({
|
||||||
const sessionExists = await prisma.agentSession.findUnique({ where: { id: params.sessionId } })
|
data: {
|
||||||
if (!sessionExists) {
|
assetId: params.assetId,
|
||||||
await prisma.agentSession.create({
|
message: params.message,
|
||||||
data: {
|
sessionId: params.sessionId ?? null,
|
||||||
id: params.sessionId,
|
creatorId: params.agentId && params.agentId !== 'default' ? params.agentId : null,
|
||||||
agentId: agentId,
|
replyToId: params.replyToId,
|
||||||
cwd: process.cwd(),
|
},
|
||||||
},
|
})
|
||||||
})
|
},
|
||||||
}
|
{ runOnCancel: true },
|
||||||
}
|
)
|
||||||
|
|
||||||
return prisma.assetComment.create({
|
|
||||||
data: {
|
|
||||||
assetId: params.assetId,
|
|
||||||
message: params.message,
|
|
||||||
sessionId: params.sessionId ?? null,
|
|
||||||
creatorId: params.agentId && params.agentId !== 'default' ? params.agentId : null,
|
|
||||||
replyToId: params.replyToId,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
export const updateCommentActivity = defineStep(
|
export const updateCommentActivity = defineStep(
|
||||||
async function updateCommentActivity(params: {
|
async function updateCommentActivity(params: {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { agentAutofillMedia } from './agent-autofill'
|
|||||||
import { prisma } from '@shumai/db'
|
import { prisma } from '@shumai/db'
|
||||||
import { setupTestDbHooks } from '@shumai/db/test'
|
import { setupTestDbHooks } from '@shumai/db/test'
|
||||||
import * as workflowCore from '@shumai/workflow-core'
|
import * as workflowCore from '@shumai/workflow-core'
|
||||||
|
import { DBOSError } from '@shumai/workflow-core'
|
||||||
import * as agentActivities from '../activities/agent'
|
import * as agentActivities from '../activities/agent'
|
||||||
|
|
||||||
vi.mock('@shumai/workflow-core', async () => {
|
vi.mock('@shumai/workflow-core', async () => {
|
||||||
@@ -95,13 +96,9 @@ describe('Agent Autofill Workflow', () => {
|
|||||||
// Verify asset fetched
|
// Verify asset fetched
|
||||||
expect(mockActivities.getAssetActivity).toHaveBeenCalledWith('a1')
|
expect(mockActivities.getAssetActivity).toHaveBeenCalledWith('a1')
|
||||||
|
|
||||||
// Verify placeholder comment created
|
// Verify no success or placeholder comments created
|
||||||
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
|
expect(mockActivities.createCommentActivity).not.toHaveBeenCalled()
|
||||||
assetId: 'a1',
|
expect(mockActivities.updateCommentActivity).not.toHaveBeenCalled()
|
||||||
message: '__AUTOFILL__',
|
|
||||||
sessionId: 's1',
|
|
||||||
agentId: 'agent-1',
|
|
||||||
})
|
|
||||||
|
|
||||||
// Verify fields and context fetched
|
// Verify fields and context fetched
|
||||||
expect(mockActivities.getProjectAutofillFieldsActivity).toHaveBeenCalledWith('p1')
|
expect(mockActivities.getProjectAutofillFieldsActivity).toHaveBeenCalledWith('p1')
|
||||||
@@ -147,13 +144,6 @@ describe('Agent Autofill Workflow', () => {
|
|||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|
||||||
// Verify placeholder comment updated
|
|
||||||
expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({
|
|
||||||
commentId: 'comment-placeholder-id',
|
|
||||||
message: 'Autofill completed successfully.',
|
|
||||||
sessionId: 'session-123',
|
|
||||||
})
|
|
||||||
|
|
||||||
// Verify completed task status
|
// Verify completed task status
|
||||||
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
|
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
|
||||||
taskId: task.id,
|
taskId: task.id,
|
||||||
@@ -205,11 +195,8 @@ describe('Agent Autofill Workflow', () => {
|
|||||||
|
|
||||||
await agentAutofillMedia(task)
|
await agentAutofillMedia(task)
|
||||||
|
|
||||||
expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({
|
expect(mockActivities.createCommentActivity).not.toHaveBeenCalled()
|
||||||
commentId: 'comment-placeholder-id',
|
expect(mockActivities.updateCommentActivity).not.toHaveBeenCalled()
|
||||||
message: 'Autofill completed: No autofill fields defined in project.',
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(mockActivities.autofillAiActivity).not.toHaveBeenCalled()
|
expect(mockActivities.autofillAiActivity).not.toHaveBeenCalled()
|
||||||
expect(mockActivities.updateAssetMetadataActivity).not.toHaveBeenCalled()
|
expect(mockActivities.updateAssetMetadataActivity).not.toHaveBeenCalled()
|
||||||
|
|
||||||
@@ -219,7 +206,7 @@ describe('Agent Autofill Workflow', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should handle failures, update placeholder with error, and set status to failed', async () => {
|
it('should handle failures, create comment with error, and set status to failed', async () => {
|
||||||
mockActivities.getProjectAutofillFieldsActivity.mockRejectedValue(new Error('DB failure'))
|
mockActivities.getProjectAutofillFieldsActivity.mockRejectedValue(new Error('DB failure'))
|
||||||
|
|
||||||
const task = await prisma.workflowTask.create({
|
const task = await prisma.workflowTask.create({
|
||||||
@@ -232,10 +219,12 @@ describe('Agent Autofill Workflow', () => {
|
|||||||
|
|
||||||
await expect(agentAutofillMedia(task)).rejects.toThrow('DB failure')
|
await expect(agentAutofillMedia(task)).rejects.toThrow('DB failure')
|
||||||
|
|
||||||
// Verify placeholder updated with error
|
// Verify error comment created
|
||||||
expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({
|
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
|
||||||
commentId: 'comment-placeholder-id',
|
assetId: 'a1',
|
||||||
message: 'Autofill failed: DB failure',
|
message: 'Autofill failed: DB failure',
|
||||||
|
sessionId: task.id,
|
||||||
|
agentId: 'default',
|
||||||
})
|
})
|
||||||
|
|
||||||
// Verify task failed status
|
// Verify task failed status
|
||||||
@@ -246,6 +235,33 @@ describe('Agent Autofill Workflow', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should post an error comment when workflow is cancelled with DBOSWorkflowCancelledError', async () => {
|
||||||
|
const cancelErr = new DBOSError.DBOSWorkflowCancelledError('wf-cancel')
|
||||||
|
mockActivities.getProjectAutofillFieldsActivity.mockRejectedValue(cancelErr)
|
||||||
|
|
||||||
|
const task = await prisma.workflowTask.create({
|
||||||
|
data: {
|
||||||
|
type: 'ai_metadata_autofill',
|
||||||
|
status: 'pending',
|
||||||
|
assetId: 'a1',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await expect(agentAutofillMedia(task)).rejects.toThrow()
|
||||||
|
|
||||||
|
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
|
||||||
|
assetId: 'a1',
|
||||||
|
message: expect.stringContaining('Autofill failed:'),
|
||||||
|
sessionId: task.id,
|
||||||
|
agentId: 'default',
|
||||||
|
})
|
||||||
|
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
|
||||||
|
taskId: task.id,
|
||||||
|
status: 'failed',
|
||||||
|
output: { error: expect.stringContaining('wf-cancel') },
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it('should fail with non-retryable ApplicationFailure when asset or project is not found', async () => {
|
it('should fail with non-retryable ApplicationFailure when asset or project is not found', async () => {
|
||||||
mockActivities.getAssetActivity.mockResolvedValue(null)
|
mockActivities.getAssetActivity.mockResolvedValue(null)
|
||||||
|
|
||||||
|
|||||||
@@ -12,12 +12,9 @@ import {
|
|||||||
getAssetActivity,
|
getAssetActivity,
|
||||||
getProjectAutofillFieldsActivity,
|
getProjectAutofillFieldsActivity,
|
||||||
updateAssetMetadataActivity,
|
updateAssetMetadataActivity,
|
||||||
updateCommentActivity,
|
|
||||||
} from '../activities/agent'
|
} from '../activities/agent'
|
||||||
|
|
||||||
export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
|
export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
|
||||||
let placeholderCommentId: string | undefined
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Update status to processing
|
// Update status to processing
|
||||||
await updateTaskStatusActivity({
|
await updateTaskStatusActivity({
|
||||||
@@ -25,16 +22,6 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
|
|||||||
status: 'processing',
|
status: 'processing',
|
||||||
})
|
})
|
||||||
|
|
||||||
// 0. Create Placeholder Comment
|
|
||||||
const payload = task.payload
|
|
||||||
const placeholder = await createCommentActivity({
|
|
||||||
assetId: task.assetId,
|
|
||||||
message: '__AUTOFILL__',
|
|
||||||
sessionId: payload?.agent?.sessionId || task.id,
|
|
||||||
agentId: payload?.agent?.agentId || 'default',
|
|
||||||
})
|
|
||||||
placeholderCommentId = placeholder.id
|
|
||||||
|
|
||||||
// 1. Get Asset
|
// 1. Get Asset
|
||||||
const asset = await getAssetActivity(task.assetId)
|
const asset = await getAssetActivity(task.assetId)
|
||||||
if (!asset || !asset.project) {
|
if (!asset || !asset.project) {
|
||||||
@@ -46,12 +33,6 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
|
|||||||
// 2. Get Project Autofill Fields
|
// 2. Get Project Autofill Fields
|
||||||
const fields = await getProjectAutofillFieldsActivity(projectId)
|
const fields = await getProjectAutofillFieldsActivity(projectId)
|
||||||
if (fields.length === 0) {
|
if (fields.length === 0) {
|
||||||
if (placeholderCommentId) {
|
|
||||||
await updateCommentActivity({
|
|
||||||
commentId: placeholderCommentId,
|
|
||||||
message: 'Autofill completed: No autofill fields defined in project.',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
await updateTaskStatusActivity({
|
await updateTaskStatusActivity({
|
||||||
taskId: task.id,
|
taskId: task.id,
|
||||||
status: 'completed',
|
status: 'completed',
|
||||||
@@ -124,15 +105,6 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7. Update Placeholder Comment
|
|
||||||
if (placeholderCommentId) {
|
|
||||||
await updateCommentActivity({
|
|
||||||
commentId: placeholderCommentId,
|
|
||||||
message: 'Autofill completed successfully.',
|
|
||||||
sessionId: aiResult.sessionId,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update status to completed
|
// Update status to completed
|
||||||
await updateTaskStatusActivity({
|
await updateTaskStatusActivity({
|
||||||
taskId: task.id,
|
taskId: task.id,
|
||||||
@@ -141,16 +113,17 @@ export async function agentAutofillMedia(task: WorkflowTask): Promise<void> {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`AgentAutofillMedia failed for task ${task.id}:`, err)
|
console.error(`AgentAutofillMedia failed for task ${task.id}:`, err)
|
||||||
|
|
||||||
// Update placeholder comment with error message
|
// Create error comment
|
||||||
if (placeholderCommentId) {
|
try {
|
||||||
try {
|
const payload = task.payload
|
||||||
await updateCommentActivity({
|
await createCommentActivity({
|
||||||
commentId: placeholderCommentId,
|
assetId: task.assetId,
|
||||||
message: `Autofill failed: ${err instanceof Error ? err.message : String(err)}`,
|
message: `Autofill failed: ${err instanceof Error ? err.message : String(err)}`,
|
||||||
})
|
sessionId: payload?.agent?.sessionId || task.id,
|
||||||
} catch (commentErr) {
|
agentId: payload?.agent?.agentId || 'default',
|
||||||
console.error('Failed to update error comment:', commentErr)
|
})
|
||||||
}
|
} catch (commentErr) {
|
||||||
|
console.error('Failed to create error comment:', commentErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update status to failed
|
// Update status to failed
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { agentEmbeddingMedia } from './agent-embedding'
|
|||||||
import { prisma } from '@shumai/db'
|
import { prisma } from '@shumai/db'
|
||||||
import { setupTestDbHooks } from '@shumai/db/test'
|
import { setupTestDbHooks } from '@shumai/db/test'
|
||||||
import * as workflowCore from '@shumai/workflow-core'
|
import * as workflowCore from '@shumai/workflow-core'
|
||||||
|
import { DBOSError } from '@shumai/workflow-core'
|
||||||
import * as agentActivities from '../activities/agent'
|
import * as agentActivities from '../activities/agent'
|
||||||
import * as aiActivities from '../activities/ai'
|
import * as aiActivities from '../activities/ai'
|
||||||
|
|
||||||
@@ -122,13 +123,9 @@ describe('Agent Embedding Workflow', () => {
|
|||||||
status: 'processing',
|
status: 'processing',
|
||||||
})
|
})
|
||||||
|
|
||||||
// Verify placeholder comment created
|
// Verify no comments created on success
|
||||||
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
|
expect(mockActivities.createCommentActivity).not.toHaveBeenCalled()
|
||||||
assetId: 'a1',
|
expect(mockActivities.updateCommentActivity).not.toHaveBeenCalled()
|
||||||
message: '__EMBEDDING__',
|
|
||||||
sessionId: 's1',
|
|
||||||
agentId: 'agent-1',
|
|
||||||
})
|
|
||||||
|
|
||||||
// Verify context fetching
|
// Verify context fetching
|
||||||
expect(mockActivities.getEmbeddingContextActivity).toHaveBeenCalledWith({
|
expect(mockActivities.getEmbeddingContextActivity).toHaveBeenCalledWith({
|
||||||
@@ -157,12 +154,6 @@ describe('Agent Embedding Workflow', () => {
|
|||||||
model: 'gemini-embedding-2',
|
model: 'gemini-embedding-2',
|
||||||
})
|
})
|
||||||
|
|
||||||
// Verify placeholder comment updated
|
|
||||||
expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({
|
|
||||||
commentId: 'comment-placeholder-id',
|
|
||||||
message: 'Embedding completed successfully.',
|
|
||||||
})
|
|
||||||
|
|
||||||
// Verify completed task status
|
// Verify completed task status
|
||||||
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
|
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
|
||||||
taskId: task.id,
|
taskId: task.id,
|
||||||
@@ -417,7 +408,7 @@ describe('Agent Embedding Workflow', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should handle failures, update placeholder with error, and set status to failed', async () => {
|
it('should handle failures, create comment with error, and set status to failed', async () => {
|
||||||
mockActivities.generateImageEmbeddingActivity.mockRejectedValue(
|
mockActivities.generateImageEmbeddingActivity.mockRejectedValue(
|
||||||
new Error('AI Service Unavailable'),
|
new Error('AI Service Unavailable'),
|
||||||
)
|
)
|
||||||
@@ -433,11 +424,14 @@ describe('Agent Embedding Workflow', () => {
|
|||||||
|
|
||||||
await expect(agentEmbeddingMedia(task)).rejects.toThrow('AI Service Unavailable')
|
await expect(agentEmbeddingMedia(task)).rejects.toThrow('AI Service Unavailable')
|
||||||
|
|
||||||
// Verify placeholder updated with error
|
// Verify error comment created
|
||||||
expect(mockActivities.updateCommentActivity).toHaveBeenCalledWith({
|
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
|
||||||
commentId: 'comment-placeholder-id',
|
assetId: 'a1',
|
||||||
message: 'Embedding failed: AI Service Unavailable',
|
message: 'Embedding failed: AI Service Unavailable',
|
||||||
|
sessionId: task.id,
|
||||||
|
agentId: 'default',
|
||||||
})
|
})
|
||||||
|
expect(mockActivities.updateCommentActivity).not.toHaveBeenCalled()
|
||||||
|
|
||||||
// Verify status updated to failed
|
// Verify status updated to failed
|
||||||
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
|
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
|
||||||
@@ -447,6 +441,34 @@ describe('Agent Embedding Workflow', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should post an error comment when workflow is cancelled with DBOSWorkflowCancelledError', async () => {
|
||||||
|
const cancelErr = new DBOSError.DBOSWorkflowCancelledError('wf-cancel')
|
||||||
|
mockActivities.generateImageEmbeddingActivity.mockRejectedValue(cancelErr)
|
||||||
|
|
||||||
|
const task = await prisma.workflowTask.create({
|
||||||
|
data: {
|
||||||
|
type: 'ai_embedding',
|
||||||
|
status: 'pending',
|
||||||
|
assetId: 'a1',
|
||||||
|
teamId: 't1',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await expect(agentEmbeddingMedia(task)).rejects.toThrow()
|
||||||
|
|
||||||
|
expect(mockActivities.createCommentActivity).toHaveBeenCalledWith({
|
||||||
|
assetId: 'a1',
|
||||||
|
message: expect.stringContaining('Embedding failed:'),
|
||||||
|
sessionId: task.id,
|
||||||
|
agentId: 'default',
|
||||||
|
})
|
||||||
|
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
|
||||||
|
taskId: task.id,
|
||||||
|
status: 'failed',
|
||||||
|
output: { error: expect.stringContaining('wf-cancel') },
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it('should save completed chunks to DB immediately before subsequent chunk failure', async () => {
|
it('should save completed chunks to DB immediately before subsequent chunk failure', async () => {
|
||||||
mockActivities.getEmbeddingContextActivity.mockResolvedValue({
|
mockActivities.getEmbeddingContextActivity.mockResolvedValue({
|
||||||
agent: { id: 'b1' },
|
agent: { id: 'b1' },
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import {
|
|||||||
updateTaskStatusActivity,
|
updateTaskStatusActivity,
|
||||||
updateTaskUsageActivity,
|
updateTaskUsageActivity,
|
||||||
} from '@shumai/workflow-core'
|
} from '@shumai/workflow-core'
|
||||||
import { createCommentActivity, updateCommentActivity } from '../activities/agent'
|
import { createCommentActivity } from '../activities/agent'
|
||||||
import {
|
import {
|
||||||
deleteAssetEmbeddingsActivity,
|
deleteAssetEmbeddingsActivity,
|
||||||
deleteS3ObjectActivity,
|
deleteS3ObjectActivity,
|
||||||
@@ -25,8 +25,6 @@ export interface GeneratedEmbedding {
|
|||||||
export type { ExistingAssetEmbeddingInfo }
|
export type { ExistingAssetEmbeddingInfo }
|
||||||
|
|
||||||
export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
|
export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
|
||||||
let placeholderCommentId: string | undefined
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Update status to processing
|
// Update status to processing
|
||||||
await updateTaskStatusActivity({
|
await updateTaskStatusActivity({
|
||||||
@@ -34,16 +32,6 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
|
|||||||
status: 'processing',
|
status: 'processing',
|
||||||
})
|
})
|
||||||
|
|
||||||
// 0. Create Placeholder Comment
|
|
||||||
const payload = task.payload
|
|
||||||
const placeholder = await createCommentActivity({
|
|
||||||
assetId: task.assetId,
|
|
||||||
message: '__EMBEDDING__',
|
|
||||||
sessionId: payload?.agent?.sessionId || task.id,
|
|
||||||
agentId: payload?.agent?.agentId || 'default',
|
|
||||||
})
|
|
||||||
placeholderCommentId = placeholder.id
|
|
||||||
|
|
||||||
if (!task.teamId) {
|
if (!task.teamId) {
|
||||||
throw ApplicationFailure.create({ message: 'Task has no teamId', nonRetryable: true })
|
throw ApplicationFailure.create({ message: 'Task has no teamId', nonRetryable: true })
|
||||||
}
|
}
|
||||||
@@ -238,14 +226,6 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
|
|||||||
model: usage.model,
|
model: usage.model,
|
||||||
})
|
})
|
||||||
|
|
||||||
// 7. Update Placeholder Comment
|
|
||||||
if (placeholderCommentId) {
|
|
||||||
await updateCommentActivity({
|
|
||||||
commentId: placeholderCommentId,
|
|
||||||
message: 'Embedding completed successfully.',
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update status to completed
|
// Update status to completed
|
||||||
await updateTaskStatusActivity({
|
await updateTaskStatusActivity({
|
||||||
taskId: task.id,
|
taskId: task.id,
|
||||||
@@ -254,16 +234,17 @@ export async function agentEmbeddingMedia(task: WorkflowTask): Promise<void> {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`AgentEmbeddingMedia failed for task ${task.id}:`, err)
|
console.error(`AgentEmbeddingMedia failed for task ${task.id}:`, err)
|
||||||
|
|
||||||
// Update placeholder comment with error message
|
// Create error comment
|
||||||
if (placeholderCommentId) {
|
try {
|
||||||
try {
|
const payload = task.payload
|
||||||
await updateCommentActivity({
|
await createCommentActivity({
|
||||||
commentId: placeholderCommentId,
|
assetId: task.assetId,
|
||||||
message: `Embedding failed: ${err instanceof Error ? err.message : String(err)}`,
|
message: `Embedding failed: ${err instanceof Error ? err.message : String(err)}`,
|
||||||
})
|
sessionId: payload?.agent?.sessionId || task.id,
|
||||||
} catch (commentErr) {
|
agentId: payload?.agent?.agentId || 'default',
|
||||||
console.error('Failed to update error comment:', commentErr)
|
})
|
||||||
}
|
} catch (commentErr) {
|
||||||
|
console.error('Failed to create error comment:', commentErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update status to failed
|
// Update status to failed
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import { s3Service } from '@shumai/core/src/s3/s3'
|
|||||||
import { transcodeService } from '@shumai/core'
|
import { transcodeService } from '@shumai/core'
|
||||||
import { authMiddleware } from './middleware/auth'
|
import { authMiddleware } from './middleware/auth'
|
||||||
import { authzService, ResourceType, Permission } from '@shumai/core/src/authz/authz'
|
import { authzService, ResourceType, Permission } from '@shumai/core/src/authz/authz'
|
||||||
|
import type { AssetInfo } from '@shumai/dtos'
|
||||||
import {
|
import {
|
||||||
renameAssetOp,
|
renameAssetOp,
|
||||||
resolveCommentOp,
|
resolveCommentOp,
|
||||||
@@ -72,6 +73,7 @@ describe('file api', () => {
|
|||||||
vi.mocked(authzService.hasPermission).mockClear()
|
vi.mocked(authzService.hasPermission).mockClear()
|
||||||
|
|
||||||
vi.spyOn(assetService, 'getAsset').mockImplementation(vi.fn())
|
vi.spyOn(assetService, 'getAsset').mockImplementation(vi.fn())
|
||||||
|
vi.spyOn(assetService, 'listAssetsByIds').mockImplementation(vi.fn())
|
||||||
vi.spyOn(assetService, 'getAssetContext').mockResolvedValue({ teamId: 'test-team' })
|
vi.spyOn(assetService, 'getAssetContext').mockResolvedValue({ teamId: 'test-team' })
|
||||||
vi.spyOn(assetService, 'updateAssetName').mockImplementation(vi.fn())
|
vi.spyOn(assetService, 'updateAssetName').mockImplementation(vi.fn())
|
||||||
vi.spyOn(assetService, 'deleteAssets').mockImplementation(vi.fn())
|
vi.spyOn(assetService, 'deleteAssets').mockImplementation(vi.fn())
|
||||||
@@ -126,6 +128,78 @@ describe('file api', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('POST /files/batch', () => {
|
||||||
|
it('returns empty array when ids is empty', async () => {
|
||||||
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||||
|
const res = await app.request('/files/batch', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ ids: [] }),
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(res.status).toBe(200)
|
||||||
|
const json = await res.json()
|
||||||
|
expect(json).toEqual([])
|
||||||
|
expect(assetService.listAssetsByIds).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns asset list for accessible IDs', async () => {
|
||||||
|
const assets = [
|
||||||
|
{ id: 'f1', name: 'file-1.mp4', status: 'processing' },
|
||||||
|
{ id: 'f2', name: 'file-2.mp4', status: 'processed' },
|
||||||
|
]
|
||||||
|
vi.mocked(assetService.listAssetsByIds).mockResolvedValue(assets as unknown as AssetInfo[])
|
||||||
|
|
||||||
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||||
|
const res = await app.request('/files/batch', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ ids: ['f1', 'f2'] }),
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(res.status).toBe(200)
|
||||||
|
const json = await res.json()
|
||||||
|
expect(json).toEqual(assets)
|
||||||
|
expect(assetService.listAssetsByIds).toHaveBeenCalledWith(['f1', 'f2'])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('gracefully omits unauthorized or missing assets without failing the batch', async () => {
|
||||||
|
vi.mocked(authzService.hasPermission).mockImplementation(async (req) => {
|
||||||
|
if (req.id === 'unauthorized-id') {
|
||||||
|
throw new HTTPException(403, { message: 'Permission denied' })
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const accessibleAssets = [{ id: 'f1', name: 'file-1.mp4', status: 'processing' }]
|
||||||
|
vi.mocked(assetService.listAssetsByIds).mockResolvedValue(
|
||||||
|
accessibleAssets as unknown as AssetInfo[],
|
||||||
|
)
|
||||||
|
|
||||||
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||||
|
const res = await app.request('/files/batch', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ ids: ['f1', 'unauthorized-id'] }),
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(res.status).toBe(200)
|
||||||
|
const json = await res.json()
|
||||||
|
expect(json).toEqual(accessibleAssets)
|
||||||
|
expect(assetService.listAssetsByIds).toHaveBeenCalledWith(['f1'])
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects with 400 when more than 100 IDs are requested', async () => {
|
||||||
|
const tooManyIds = Array.from({ length: 101 }, (_, i) => `id-${i}`)
|
||||||
|
const app = new Hono().use('*', authMiddleware).route('/', fileRoute)
|
||||||
|
const res = await app.request('/files/batch', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ ids: tooManyIds }),
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(res.status).toBe(400)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it('PUT /files/:fileId runs rename_asset', async () => {
|
it('PUT /files/:fileId runs rename_asset', async () => {
|
||||||
const summary = { id: 'test-id', name: 'new-name', type: 'file', size: 10, projectId: 'p1' }
|
const summary = { id: 'test-id', name: 'new-name', type: 'file', size: 10, projectId: 'p1' }
|
||||||
vi.mocked(runOperation).mockResolvedValue(summary)
|
vi.mocked(runOperation).mockResolvedValue(summary)
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import {
|
|||||||
import {
|
import {
|
||||||
createCommentRequestSchema,
|
createCommentRequestSchema,
|
||||||
deleteFilesRequestSchema,
|
deleteFilesRequestSchema,
|
||||||
|
batchGetFilesRequestSchema,
|
||||||
paginationParamsSchema,
|
paginationParamsSchema,
|
||||||
restoreFilesRequestSchema,
|
restoreFilesRequestSchema,
|
||||||
updateAssetMetadataRequestSchema,
|
updateAssetMetadataRequestSchema,
|
||||||
@@ -39,6 +40,40 @@ import { z } from 'zod'
|
|||||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||||
|
|
||||||
const route = new Hono<{ Variables: { user: User } }>()
|
const route = new Hono<{ Variables: { user: User } }>()
|
||||||
|
.post('/files/batch', zValidator('json', batchGetFilesRequestSchema), async (c) => {
|
||||||
|
const user = c.get('user')
|
||||||
|
const req = c.req.valid('json')
|
||||||
|
|
||||||
|
if (req.ids.length === 0) {
|
||||||
|
return c.json([])
|
||||||
|
}
|
||||||
|
|
||||||
|
const accessibleIds: string[] = []
|
||||||
|
await Promise.all(
|
||||||
|
req.ids.map(async (id) => {
|
||||||
|
try {
|
||||||
|
await authzService.hasPermission({
|
||||||
|
user,
|
||||||
|
permission: Permission.Read,
|
||||||
|
type: ResourceType.Asset,
|
||||||
|
id,
|
||||||
|
})
|
||||||
|
accessibleIds.push(id)
|
||||||
|
} catch {
|
||||||
|
// Permissive: silently ignore missing or unauthorized IDs
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
if (accessibleIds.length === 0) {
|
||||||
|
return c.json([])
|
||||||
|
}
|
||||||
|
|
||||||
|
const accessibleSet = new Set(accessibleIds)
|
||||||
|
const orderedAccessibleIds = req.ids.filter((id) => accessibleSet.has(id))
|
||||||
|
const assets = await assetService.listAssetsByIds(orderedAccessibleIds)
|
||||||
|
return c.json(assets)
|
||||||
|
})
|
||||||
.get('/files/:fileId', async (c) => {
|
.get('/files/:fileId', async (c) => {
|
||||||
const fileId = c.req.param('fileId')
|
const fileId = c.req.param('fileId')
|
||||||
const user = c.get('user')
|
const user = c.get('user')
|
||||||
|
|||||||
@@ -220,4 +220,27 @@ describe('Upload API', () => {
|
|||||||
key: 'files/test.mp4',
|
key: 'files/test.mp4',
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('POST /teams/:teamId/upload/tasks/:taskId/cancel', async () => {
|
||||||
|
vi.spyOn(uploadService, 'cancelUploadTask').mockResolvedValue({
|
||||||
|
success: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
const res = await app.request('/teams/team1/upload/tasks/task1/cancel', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { Authorization: 'Bearer test' },
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(res.status).toBe(200)
|
||||||
|
const json = await res.json()
|
||||||
|
expect(json).toEqual({ success: true })
|
||||||
|
expect(authzService.hasPermission).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
type: ResourceType.Team,
|
||||||
|
id: 'team1',
|
||||||
|
permission: Permission.Edit,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
expect(uploadService.cancelUploadTask).toHaveBeenCalledWith('team1', 'user1', 'task1')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -163,5 +163,20 @@ const route = new Hono<{ Variables: { user: User } }>()
|
|||||||
return c.json(resp)
|
return c.json(resp)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
.post('/teams/:teamId/upload/tasks/:taskId/cancel', async (c) => {
|
||||||
|
const teamId = c.req.param('teamId')
|
||||||
|
const taskId = c.req.param('taskId')
|
||||||
|
const user = c.get('user')
|
||||||
|
|
||||||
|
await authzService.hasPermission({
|
||||||
|
user,
|
||||||
|
permission: Permission.Edit,
|
||||||
|
type: ResourceType.Team,
|
||||||
|
id: teamId,
|
||||||
|
})
|
||||||
|
|
||||||
|
const resp = await uploadService.cancelUploadTask(teamId, user.id, taskId)
|
||||||
|
return c.json(resp)
|
||||||
|
})
|
||||||
|
|
||||||
export default route
|
export default route
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ describe('S3Service implementations', () => {
|
|||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
getSignedUrlSpy.mockReset()
|
getSignedUrlSpy.mockReset()
|
||||||
delete process.env.PRESIGNED_URL_EXPIRES_IN
|
delete process.env.PRESIGNED_URL_EXPIRES_IN
|
||||||
|
delete process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN
|
||||||
getSignedUrlSpy.mockResolvedValue('http://presigned-url')
|
getSignedUrlSpy.mockResolvedValue('http://presigned-url')
|
||||||
s3SendSpy.mockResolvedValue({})
|
s3SendSpy.mockResolvedValue({})
|
||||||
})
|
})
|
||||||
@@ -146,7 +147,7 @@ describe('S3Service implementations', () => {
|
|||||||
expect(getSignedUrlSpy).toHaveBeenCalledTimes(2)
|
expect(getSignedUrlSpy).toHaveBeenCalledTimes(2)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should respect PRESIGNED_URL_EXPIRES_IN env', async () => {
|
it('should respect PRESIGNED_URL_EXPIRES_IN env for GET', async () => {
|
||||||
process.env.PRESIGNED_URL_EXPIRES_IN = '10'
|
process.env.PRESIGNED_URL_EXPIRES_IN = '10'
|
||||||
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
|
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
|
||||||
|
|
||||||
@@ -159,6 +160,77 @@ describe('S3Service implementations', () => {
|
|||||||
expect.objectContaining({ expiresIn: 10 * 3600 }),
|
expect.objectContaining({ expiresIn: 10 * 3600 }),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
delete process.env.PRESIGNED_URL_EXPIRES_IN
|
||||||
|
delete process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should default PUT presign and multipart to 12 hours (43200 seconds)', async () => {
|
||||||
|
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
|
||||||
|
|
||||||
|
getSignedUrlSpy.mockClear()
|
||||||
|
await s3.presign('bucket', 'key', 'PUT')
|
||||||
|
expect(getSignedUrlSpy).toHaveBeenCalledWith(
|
||||||
|
expect.anything(),
|
||||||
|
expect.any(PutObjectCommand),
|
||||||
|
expect.objectContaining({ expiresIn: 12 * 3600 }),
|
||||||
|
)
|
||||||
|
|
||||||
|
getSignedUrlSpy.mockClear()
|
||||||
|
await s3.presignMultipart('bucket', 'key', {
|
||||||
|
method: 'PUT',
|
||||||
|
key: 'key',
|
||||||
|
fileId: 'file-1',
|
||||||
|
uploadId: 'up-1',
|
||||||
|
partNumber: 1,
|
||||||
|
})
|
||||||
|
expect(getSignedUrlSpy).toHaveBeenCalledWith(
|
||||||
|
expect.anything(),
|
||||||
|
expect.any(UploadPartCommand),
|
||||||
|
expect.objectContaining({ expiresIn: 12 * 3600 }),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should respect PRESIGNED_UPLOAD_URL_EXPIRES_IN env for PUT and multipart', async () => {
|
||||||
|
process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN = '24'
|
||||||
|
process.env.PRESIGNED_URL_EXPIRES_IN = '5'
|
||||||
|
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
|
||||||
|
|
||||||
|
getSignedUrlSpy.mockClear()
|
||||||
|
await s3.presign('bucket', 'key', 'PUT')
|
||||||
|
expect(getSignedUrlSpy).toHaveBeenCalledWith(
|
||||||
|
expect.anything(),
|
||||||
|
expect.any(PutObjectCommand),
|
||||||
|
expect.objectContaining({ expiresIn: 24 * 3600 }),
|
||||||
|
)
|
||||||
|
|
||||||
|
getSignedUrlSpy.mockClear()
|
||||||
|
await s3.presignMultipart('bucket', 'key', {
|
||||||
|
method: 'PUT',
|
||||||
|
key: 'key',
|
||||||
|
fileId: 'file-1',
|
||||||
|
})
|
||||||
|
expect(getSignedUrlSpy).toHaveBeenCalledWith(
|
||||||
|
expect.anything(),
|
||||||
|
expect.any(PutObjectCommand),
|
||||||
|
expect.objectContaining({ expiresIn: 24 * 3600 }),
|
||||||
|
)
|
||||||
|
|
||||||
|
delete process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN
|
||||||
|
delete process.env.PRESIGNED_URL_EXPIRES_IN
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should allow PRESIGNED_URL_EXPIRES_IN to override PUT if PRESIGNED_UPLOAD_URL_EXPIRES_IN is unset', async () => {
|
||||||
|
process.env.PRESIGNED_URL_EXPIRES_IN = '8'
|
||||||
|
const s3 = new S3StorageService('http://localhost:9000', 'key', 'secret', 'test-bucket')
|
||||||
|
|
||||||
|
getSignedUrlSpy.mockClear()
|
||||||
|
await s3.presign('bucket', 'key', 'PUT')
|
||||||
|
expect(getSignedUrlSpy).toHaveBeenCalledWith(
|
||||||
|
expect.anything(),
|
||||||
|
expect.any(PutObjectCommand),
|
||||||
|
expect.objectContaining({ expiresIn: 8 * 3600 }),
|
||||||
|
)
|
||||||
|
|
||||||
delete process.env.PRESIGNED_URL_EXPIRES_IN
|
delete process.env.PRESIGNED_URL_EXPIRES_IN
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ export interface S3Service {
|
|||||||
download?: boolean,
|
download?: boolean,
|
||||||
filename?: string,
|
filename?: string,
|
||||||
internal?: boolean,
|
internal?: boolean,
|
||||||
/** URL lifetime; defaults to PRESIGNED_URL_EXPIRES_IN hours. Ignored by local storage. */
|
/** URL lifetime; defaults to PRESIGNED_UPLOAD_URL_EXPIRES_IN (12h) for PUT, or PRESIGNED_URL_EXPIRES_IN (5h) for GET. Ignored by local storage. */
|
||||||
expiresInSeconds?: number,
|
expiresInSeconds?: number,
|
||||||
) => Promise<string>
|
) => Promise<string>
|
||||||
presignMultipart: (
|
presignMultipart: (
|
||||||
@@ -364,8 +364,16 @@ export class S3StorageService implements S3Service {
|
|||||||
internal?: boolean,
|
internal?: boolean,
|
||||||
expiresInSeconds?: number,
|
expiresInSeconds?: number,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const expireHours = parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10)
|
const defaultExpireHours =
|
||||||
const lifetimeSeconds = expiresInSeconds ?? expireHours * 3600
|
method === 'PUT'
|
||||||
|
? parseInt(
|
||||||
|
process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN ||
|
||||||
|
process.env.PRESIGNED_URL_EXPIRES_IN ||
|
||||||
|
'12',
|
||||||
|
10,
|
||||||
|
)
|
||||||
|
: parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10)
|
||||||
|
const lifetimeSeconds = expiresInSeconds ?? defaultExpireHours * 3600
|
||||||
// With an explicit (usually short) lifetime, only reuse a cached URL while it has at
|
// With an explicit (usually short) lifetime, only reuse a cached URL while it has at
|
||||||
// least half of its lifetime left, so callers never receive a nearly expired link.
|
// least half of its lifetime left, so callers never receive a nearly expired link.
|
||||||
const cacheTtlMs = expiresInSeconds ? (lifetimeSeconds * 1000) / 2 : lifetimeSeconds * 1000
|
const cacheTtlMs = expiresInSeconds ? (lifetimeSeconds * 1000) / 2 : lifetimeSeconds * 1000
|
||||||
@@ -416,7 +424,10 @@ export class S3StorageService implements S3Service {
|
|||||||
key: string,
|
key: string,
|
||||||
request: S3SignRequest,
|
request: S3SignRequest,
|
||||||
): Promise<{ url: string }> {
|
): Promise<{ url: string }> {
|
||||||
const expireHours = parseInt(process.env.PRESIGNED_URL_EXPIRES_IN || '5', 10)
|
const expireHours = parseInt(
|
||||||
|
process.env.PRESIGNED_UPLOAD_URL_EXPIRES_IN || process.env.PRESIGNED_URL_EXPIRES_IN || '12',
|
||||||
|
10,
|
||||||
|
)
|
||||||
const expiresInSeconds = expireHours * 3600
|
const expiresInSeconds = expireHours * 3600
|
||||||
|
|
||||||
let url: string
|
let url: string
|
||||||
|
|||||||
@@ -797,6 +797,7 @@ describe('UploadService', () => {
|
|||||||
expect(result.data).toHaveLength(1)
|
expect(result.data).toHaveLength(1)
|
||||||
expect(result.data[0].id).toBe(task.id)
|
expect(result.data[0].id).toBe(task.id)
|
||||||
expect(result.data[0].name).toBe('test-upload-task')
|
expect(result.data[0].name).toBe('test-upload-task')
|
||||||
|
expect(result.data[0].status).toBe(TaskStatus.pending)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should correct the mediaType for .wma files that browser incorrectly reports as video', async () => {
|
it('should correct the mediaType for .wma files that browser incorrectly reports as video', async () => {
|
||||||
@@ -1120,4 +1121,262 @@ describe('UploadService', () => {
|
|||||||
expect(deletedAsset).toBeNull()
|
expect(deletedAsset).toBeNull()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('cancelUploadTask', () => {
|
||||||
|
it('cancels uploading assets, leaves uploaded assets and folders intact, and marks task failed', async () => {
|
||||||
|
const task = await prisma.task.create({
|
||||||
|
data: {
|
||||||
|
creatorId: userId,
|
||||||
|
type: 'upload',
|
||||||
|
name: 'multi-file-task',
|
||||||
|
total: 3,
|
||||||
|
uploaded: 1,
|
||||||
|
status: TaskStatus.pending,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
// 1. A folder created by the task
|
||||||
|
const folder = await prisma.asset.create({
|
||||||
|
data: {
|
||||||
|
name: 'subfolder',
|
||||||
|
type: AssetType.folder,
|
||||||
|
projectId,
|
||||||
|
parentId,
|
||||||
|
taskId: task.id,
|
||||||
|
status: AssetStatus.uploaded,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
// 2. An asset already finished uploading
|
||||||
|
const finishedKey = await prisma.storageKey.create({ data: { key: 'files/test/done.mp4' } })
|
||||||
|
const finishedAsset = await prisma.asset.create({
|
||||||
|
data: {
|
||||||
|
name: 'done.mp4',
|
||||||
|
type: AssetType.file,
|
||||||
|
projectId,
|
||||||
|
parentId: folder.id,
|
||||||
|
storageKeyId: finishedKey.id,
|
||||||
|
taskId: task.id,
|
||||||
|
status: AssetStatus.uploaded,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
// 3. An incomplete multipart asset
|
||||||
|
const multipartKey = await prisma.storageKey.create({
|
||||||
|
data: { key: 'files/test/part.mp4' },
|
||||||
|
})
|
||||||
|
const multipartAsset = await prisma.asset.create({
|
||||||
|
data: {
|
||||||
|
name: 'part.mp4',
|
||||||
|
type: AssetType.file,
|
||||||
|
projectId,
|
||||||
|
parentId: folder.id,
|
||||||
|
storageKeyId: multipartKey.id,
|
||||||
|
taskId: task.id,
|
||||||
|
uploadId: 'upload-multipart-cancel',
|
||||||
|
status: AssetStatus.uploading,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
// 4. An incomplete single PUT asset
|
||||||
|
const singleKey = await prisma.storageKey.create({
|
||||||
|
data: { key: 'files/test/single.mp4' },
|
||||||
|
})
|
||||||
|
const singleAsset = await prisma.asset.create({
|
||||||
|
data: {
|
||||||
|
name: 'single.mp4',
|
||||||
|
type: AssetType.file,
|
||||||
|
projectId,
|
||||||
|
parentId: folder.id,
|
||||||
|
storageKeyId: singleKey.id,
|
||||||
|
taskId: task.id,
|
||||||
|
status: AssetStatus.uploading,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const res = await uploadService.cancelUploadTask(teamId, userId, task.id)
|
||||||
|
expect(res).toEqual({ success: true })
|
||||||
|
|
||||||
|
// Incomplete assets deleted
|
||||||
|
expect(await prisma.asset.findUnique({ where: { id: multipartAsset.id } })).toBeNull()
|
||||||
|
expect(await prisma.storageKey.findUnique({ where: { id: multipartKey.id } })).toBeNull()
|
||||||
|
expect(await prisma.asset.findUnique({ where: { id: singleAsset.id } })).toBeNull()
|
||||||
|
expect(await prisma.storageKey.findUnique({ where: { id: singleKey.id } })).toBeNull()
|
||||||
|
|
||||||
|
// S3 cleanup called
|
||||||
|
expect(s3Service.abortMultipartUpload).toHaveBeenCalledWith(
|
||||||
|
expect.anything(),
|
||||||
|
'files/test/part.mp4',
|
||||||
|
'upload-multipart-cancel',
|
||||||
|
)
|
||||||
|
expect(s3Service.deleteObject).toHaveBeenCalledWith(
|
||||||
|
expect.anything(),
|
||||||
|
'files/test/single.mp4',
|
||||||
|
)
|
||||||
|
|
||||||
|
// Completed asset and folder remain intact
|
||||||
|
expect(await prisma.asset.findUnique({ where: { id: finishedAsset.id } })).not.toBeNull()
|
||||||
|
expect(await prisma.storageKey.findUnique({ where: { id: finishedKey.id } })).not.toBeNull()
|
||||||
|
expect(await prisma.asset.findUnique({ where: { id: folder.id } })).not.toBeNull()
|
||||||
|
|
||||||
|
// Task status updated to failed
|
||||||
|
const updatedTask = await prisma.task.findUnique({ where: { id: task.id } })
|
||||||
|
expect(updatedTask?.status).toBe(TaskStatus.failed)
|
||||||
|
expect(updatedTask?.uploaded).toBe(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('rejects cancellation if the caller is not the task creator', async () => {
|
||||||
|
const otherUser = await prisma.user.create({
|
||||||
|
data: {
|
||||||
|
email: 'other@example.com',
|
||||||
|
name: 'Other User',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const task = await prisma.task.create({
|
||||||
|
data: {
|
||||||
|
creatorId: otherUser.id,
|
||||||
|
type: 'upload',
|
||||||
|
name: 'other-task',
|
||||||
|
total: 1,
|
||||||
|
status: TaskStatus.pending,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await expect(uploadService.cancelUploadTask(teamId, userId, task.id)).rejects.toThrow(
|
||||||
|
'Unauthorized to cancel this upload task',
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws when task does not exist', async () => {
|
||||||
|
await expect(
|
||||||
|
uploadService.cancelUploadTask(teamId, userId, 'non-existent-task'),
|
||||||
|
).rejects.toThrow('Upload task not found')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws when task assets belong to a different team', async () => {
|
||||||
|
const otherTeam = await prisma.team.create({
|
||||||
|
data: { name: 'Other Team' },
|
||||||
|
})
|
||||||
|
const otherProject = await prisma.project.create({
|
||||||
|
data: { name: 'Other Project', teamId: otherTeam.id },
|
||||||
|
})
|
||||||
|
|
||||||
|
const task = await prisma.task.create({
|
||||||
|
data: {
|
||||||
|
creatorId: userId,
|
||||||
|
type: 'upload',
|
||||||
|
name: 'wrong-team-task',
|
||||||
|
total: 1,
|
||||||
|
status: TaskStatus.pending,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await prisma.asset.create({
|
||||||
|
data: {
|
||||||
|
name: 'wrong-team-file.mp4',
|
||||||
|
type: AssetType.file,
|
||||||
|
projectId: otherProject.id,
|
||||||
|
taskId: task.id,
|
||||||
|
status: AssetStatus.uploading,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await expect(uploadService.cancelUploadTask(teamId, userId, task.id)).rejects.toThrow(
|
||||||
|
'Task does not belong to specified team',
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('throws when task is already completed and preserves completed status', async () => {
|
||||||
|
const task = await prisma.task.create({
|
||||||
|
data: {
|
||||||
|
creatorId: userId,
|
||||||
|
type: 'upload',
|
||||||
|
name: 'completed-task',
|
||||||
|
total: 1,
|
||||||
|
uploaded: 1,
|
||||||
|
status: TaskStatus.completed,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await expect(uploadService.cancelUploadTask(teamId, userId, task.id)).rejects.toThrow(
|
||||||
|
'Cannot cancel a completed upload task',
|
||||||
|
)
|
||||||
|
|
||||||
|
const refreshed = await prisma.task.findUnique({ where: { id: task.id } })
|
||||||
|
expect(refreshed?.status).toBe(TaskStatus.completed)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('preserves an asset and its storage if it was confirmed uploaded concurrently', async () => {
|
||||||
|
const task = await prisma.task.create({
|
||||||
|
data: {
|
||||||
|
creatorId: userId,
|
||||||
|
type: 'upload',
|
||||||
|
name: 'race-task',
|
||||||
|
total: 2,
|
||||||
|
uploaded: 0,
|
||||||
|
status: TaskStatus.uploading,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const confirmedAsset = await prisma.asset.create({
|
||||||
|
data: {
|
||||||
|
name: 'race-confirmed.mp4',
|
||||||
|
type: AssetType.file,
|
||||||
|
project: { connect: { id: projectId } },
|
||||||
|
task: { connect: { id: task.id } },
|
||||||
|
status: AssetStatus.uploading,
|
||||||
|
storageKey: {
|
||||||
|
create: { key: 'files/test/race-confirmed.mp4' },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
include: { storageKey: true },
|
||||||
|
})
|
||||||
|
|
||||||
|
const cancelledAsset = await prisma.asset.create({
|
||||||
|
data: {
|
||||||
|
name: 'race-cancelled.mp4',
|
||||||
|
type: AssetType.file,
|
||||||
|
project: { connect: { id: projectId } },
|
||||||
|
task: { connect: { id: task.id } },
|
||||||
|
status: AssetStatus.uploading,
|
||||||
|
storageKey: {
|
||||||
|
create: { key: 'files/test/race-cancelled.mp4' },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
include: { storageKey: true },
|
||||||
|
})
|
||||||
|
|
||||||
|
// Simulate concurrent confirm: update status to uploaded
|
||||||
|
await prisma.asset.update({
|
||||||
|
where: { id: confirmedAsset.id },
|
||||||
|
data: { status: AssetStatus.uploaded },
|
||||||
|
})
|
||||||
|
|
||||||
|
const deleteObjectSpy = vi.spyOn(s3Service, 'deleteObject').mockResolvedValue(1)
|
||||||
|
|
||||||
|
await uploadService.cancelUploadTask(teamId, userId, task.id)
|
||||||
|
|
||||||
|
// Confirmed asset must still exist in DB
|
||||||
|
const dbConfirmed = await prisma.asset.findUnique({ where: { id: confirmedAsset.id } })
|
||||||
|
expect(dbConfirmed).not.toBeNull()
|
||||||
|
expect(dbConfirmed?.status).toBe(AssetStatus.uploaded)
|
||||||
|
|
||||||
|
// Cancelled asset must be deleted
|
||||||
|
const dbCancelled = await prisma.asset.findUnique({ where: { id: cancelledAsset.id } })
|
||||||
|
expect(dbCancelled).toBeNull()
|
||||||
|
|
||||||
|
// S3 deleteObject was called for the cancelled asset, but NOT for the confirmed asset
|
||||||
|
expect(deleteObjectSpy).toHaveBeenCalledWith(
|
||||||
|
expect.anything(),
|
||||||
|
'files/test/race-cancelled.mp4',
|
||||||
|
)
|
||||||
|
expect(deleteObjectSpy).not.toHaveBeenCalledWith(
|
||||||
|
expect.anything(),
|
||||||
|
'files/test/race-confirmed.mp4',
|
||||||
|
)
|
||||||
|
|
||||||
|
deleteObjectSpy.mockRestore()
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { AssetStatus, AssetType, Prisma, TaskStatus, prisma } from '@shumai/db'
|
|||||||
import {
|
import {
|
||||||
AbortUploadRequest,
|
AbortUploadRequest,
|
||||||
AbortUploadResponse,
|
AbortUploadResponse,
|
||||||
|
CancelUploadTaskResponse,
|
||||||
ConfirmFileUploadRequest,
|
ConfirmFileUploadRequest,
|
||||||
CreateUploadTaskRequest,
|
CreateUploadTaskRequest,
|
||||||
CreateUploadTaskResponse,
|
CreateUploadTaskResponse,
|
||||||
@@ -210,30 +211,7 @@ export class UploadService {
|
|||||||
if (!key) throw new Error('Asset has no key')
|
if (!key) throw new Error('Asset has no key')
|
||||||
|
|
||||||
if (req.errorMessage) {
|
if (req.errorMessage) {
|
||||||
const bucket = process.env.S3_BUCKET || 'shumai'
|
await this.discardUploadingAsset(asset)
|
||||||
if (asset.uploadId && key) {
|
|
||||||
try {
|
|
||||||
await s3Service.abortMultipartUpload(bucket, key, asset.uploadId)
|
|
||||||
} catch (err) {
|
|
||||||
logger.warn(
|
|
||||||
{ err, key, uploadId: asset.uploadId },
|
|
||||||
'Failed to abort multipart upload on confirm error',
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} else if (key) {
|
|
||||||
try {
|
|
||||||
await s3Service.deleteObject(bucket, key)
|
|
||||||
} catch (err) {
|
|
||||||
logger.warn({ err, key }, 'Failed to delete storage object on confirm error')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.prismaClient.asset.delete({ where: { id: asset.id } }).catch(() => {})
|
|
||||||
if (asset.storageKeyId) {
|
|
||||||
await this.prismaClient.storageKey
|
|
||||||
.delete({ where: { id: asset.storageKeyId } })
|
|
||||||
.catch(() => {})
|
|
||||||
}
|
|
||||||
const t = await this.prismaClient.task.findUnique({ where: { id: taskId } })
|
const t = await this.prismaClient.task.findUnique({ where: { id: taskId } })
|
||||||
if (t) {
|
if (t) {
|
||||||
const updatedTask = await this.prismaClient.task.update({
|
const updatedTask = await this.prismaClient.task.update({
|
||||||
@@ -250,6 +228,10 @@ export class UploadService {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (asset.status !== AssetStatus.uploading) {
|
||||||
|
throw new Error('Asset is not in uploading status')
|
||||||
|
}
|
||||||
|
|
||||||
const size = await s3Service.getObjectSize(process.env.S3_BUCKET || 'shumai', key)
|
const size = await s3Service.getObjectSize(process.env.S3_BUCKET || 'shumai', key)
|
||||||
|
|
||||||
const project = asset.project
|
const project = asset.project
|
||||||
@@ -425,6 +407,7 @@ export class UploadService {
|
|||||||
total: t.total || 0,
|
total: t.total || 0,
|
||||||
uploaded: t.uploaded,
|
uploaded: t.uploaded,
|
||||||
createdAt: t.createdAt.toISOString(),
|
createdAt: t.createdAt.toISOString(),
|
||||||
|
status: t.status,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
return { data: infos, pageInfo }
|
return { data: infos, pageInfo }
|
||||||
@@ -490,7 +473,6 @@ export class UploadService {
|
|||||||
taskId: string,
|
taskId: string,
|
||||||
req: AbortUploadRequest,
|
req: AbortUploadRequest,
|
||||||
): Promise<AbortUploadResponse> {
|
): Promise<AbortUploadResponse> {
|
||||||
const bucket = process.env.S3_BUCKET || 'shumai'
|
|
||||||
const asset = await this.prismaClient.asset.findUnique({
|
const asset = await this.prismaClient.asset.findUnique({
|
||||||
where: { id: req.fileId },
|
where: { id: req.fileId },
|
||||||
include: {
|
include: {
|
||||||
@@ -520,33 +502,11 @@ export class UploadService {
|
|||||||
throw new Error('Provided key does not match asset storage key')
|
throw new Error('Provided key does not match asset storage key')
|
||||||
}
|
}
|
||||||
|
|
||||||
const uploadId = asset.uploadId || req.uploadId
|
|
||||||
if (asset.uploadId && req.uploadId && asset.uploadId !== req.uploadId) {
|
if (asset.uploadId && req.uploadId && asset.uploadId !== req.uploadId) {
|
||||||
throw new Error('Provided uploadId does not match asset uploadId')
|
throw new Error('Provided uploadId does not match asset uploadId')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uploadId && key) {
|
await this.discardUploadingAsset(asset, req.uploadId)
|
||||||
try {
|
|
||||||
await s3Service.abortMultipartUpload(bucket, key, uploadId)
|
|
||||||
} catch (err) {
|
|
||||||
logger.warn({ err, key, uploadId }, 'Failed to abort multipart upload in S3')
|
|
||||||
}
|
|
||||||
} else if (key) {
|
|
||||||
try {
|
|
||||||
await s3Service.deleteObject(bucket, key)
|
|
||||||
} catch (err) {
|
|
||||||
logger.warn({ err, key }, 'Failed to delete object from storage')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.prismaClient.asset.delete({ where: { id: asset.id } }).catch((err) => {
|
|
||||||
logger.warn({ err, assetId: asset.id }, 'Failed to delete asset on upload abort')
|
|
||||||
})
|
|
||||||
if (asset.storageKeyId) {
|
|
||||||
await this.prismaClient.storageKey
|
|
||||||
.delete({ where: { id: asset.storageKeyId } })
|
|
||||||
.catch(() => {})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (taskId) {
|
if (taskId) {
|
||||||
const remaining = await this.prismaClient.asset.count({
|
const remaining = await this.prismaClient.asset.count({
|
||||||
@@ -564,6 +524,104 @@ export class UploadService {
|
|||||||
|
|
||||||
return { success: true }
|
return { success: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async discardUploadingAsset(
|
||||||
|
asset: {
|
||||||
|
id: string
|
||||||
|
uploadId: string | null
|
||||||
|
storageKeyId: string | null
|
||||||
|
storageKey: { key: string } | null
|
||||||
|
},
|
||||||
|
requestedUploadId?: string,
|
||||||
|
): Promise<void> {
|
||||||
|
const deleteResult = await this.prismaClient.asset.deleteMany({
|
||||||
|
where: {
|
||||||
|
id: asset.id,
|
||||||
|
status: AssetStatus.uploading,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if (deleteResult.count === 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const bucket = process.env.S3_BUCKET || 'shumai'
|
||||||
|
const key = asset.storageKey?.key
|
||||||
|
const uploadId = asset.uploadId || requestedUploadId
|
||||||
|
|
||||||
|
if (uploadId && key) {
|
||||||
|
try {
|
||||||
|
await s3Service.abortMultipartUpload(bucket, key, uploadId)
|
||||||
|
} catch (err) {
|
||||||
|
logger.warn({ err, key, uploadId }, 'Failed to abort multipart upload in S3')
|
||||||
|
}
|
||||||
|
} else if (key) {
|
||||||
|
try {
|
||||||
|
await s3Service.deleteObject(bucket, key)
|
||||||
|
} catch (err) {
|
||||||
|
logger.warn({ err, key }, 'Failed to delete object from storage')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (asset.storageKeyId) {
|
||||||
|
await this.prismaClient.storageKey
|
||||||
|
.delete({ where: { id: asset.storageKeyId } })
|
||||||
|
.catch(() => {})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async cancelUploadTask(
|
||||||
|
teamId: string,
|
||||||
|
userId: string,
|
||||||
|
taskId: string,
|
||||||
|
): Promise<CancelUploadTaskResponse> {
|
||||||
|
const task = await this.prismaClient.task.findUnique({
|
||||||
|
where: { id: taskId },
|
||||||
|
include: {
|
||||||
|
assets: {
|
||||||
|
include: {
|
||||||
|
storageKey: true,
|
||||||
|
project: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!task) {
|
||||||
|
throw new Error('Upload task not found')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (task.creatorId !== userId) {
|
||||||
|
throw new Error('Unauthorized to cancel this upload task')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (task.status === TaskStatus.completed) {
|
||||||
|
throw new Error('Cannot cancel a completed upload task')
|
||||||
|
}
|
||||||
|
|
||||||
|
const invalidTeamAsset = task.assets.find(
|
||||||
|
(a) => a.project?.teamId && a.project.teamId !== teamId,
|
||||||
|
)
|
||||||
|
if (invalidTeamAsset) {
|
||||||
|
throw new Error('Task does not belong to specified team')
|
||||||
|
}
|
||||||
|
|
||||||
|
const uploadingAssets = task.assets.filter((a) => a.status === AssetStatus.uploading)
|
||||||
|
|
||||||
|
for (const asset of uploadingAssets) {
|
||||||
|
await this.discardUploadingAsset(asset)
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.prismaClient.task.updateMany({
|
||||||
|
where: {
|
||||||
|
id: taskId,
|
||||||
|
status: { not: TaskStatus.completed },
|
||||||
|
},
|
||||||
|
data: { status: TaskStatus.failed },
|
||||||
|
})
|
||||||
|
|
||||||
|
return { success: true }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const uploadService = new UploadService()
|
export const uploadService = new UploadService()
|
||||||
|
|||||||
@@ -329,6 +329,11 @@ export const deleteFilesRequestSchema = z.object({
|
|||||||
})
|
})
|
||||||
export type DeleteFilesRequest = z.infer<typeof deleteFilesRequestSchema>
|
export type DeleteFilesRequest = z.infer<typeof deleteFilesRequestSchema>
|
||||||
|
|
||||||
|
export const batchGetFilesRequestSchema = z.object({
|
||||||
|
ids: z.array(z.string()).max(100),
|
||||||
|
})
|
||||||
|
export type BatchGetFilesRequest = z.infer<typeof batchGetFilesRequestSchema>
|
||||||
|
|
||||||
export const restoreFilesRequestSchema = z.object({
|
export const restoreFilesRequestSchema = z.object({
|
||||||
ids: z.array(z.string()),
|
ids: z.array(z.string()),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -75,6 +75,11 @@ export const abortUploadResponseSchema = z.object({
|
|||||||
})
|
})
|
||||||
export type AbortUploadResponse = z.infer<typeof abortUploadResponseSchema>
|
export type AbortUploadResponse = z.infer<typeof abortUploadResponseSchema>
|
||||||
|
|
||||||
|
export const cancelUploadTaskResponseSchema = z.object({
|
||||||
|
success: z.boolean(),
|
||||||
|
})
|
||||||
|
export type CancelUploadTaskResponse = z.infer<typeof cancelUploadTaskResponseSchema>
|
||||||
|
|
||||||
export const confirmFileUploadRequestSchema = z.object({
|
export const confirmFileUploadRequestSchema = z.object({
|
||||||
fileId: z.string(),
|
fileId: z.string(),
|
||||||
errorMessage: z.string().optional(),
|
errorMessage: z.string().optional(),
|
||||||
@@ -87,6 +92,7 @@ export const taskInfoSchema = z.object({
|
|||||||
total: z.number(),
|
total: z.number(),
|
||||||
uploaded: z.number(),
|
uploaded: z.number(),
|
||||||
createdAt: z.string(),
|
createdAt: z.string(),
|
||||||
|
status: z.enum(['pending', 'uploading', 'completed', 'failed']).optional(),
|
||||||
})
|
})
|
||||||
export type TaskInfo = z.infer<typeof taskInfoSchema>
|
export type TaskInfo = z.infer<typeof taskInfoSchema>
|
||||||
|
|
||||||
|
|||||||
@@ -263,15 +263,12 @@ describe('Workflow E2E - agentAutofillMedia', () => {
|
|||||||
// 5. Verification
|
// 5. Verification
|
||||||
expect(completedTask.status).toBe('completed')
|
expect(completedTask.status).toBe('completed')
|
||||||
|
|
||||||
// Verify placeholder comment is updated correctly
|
// Verify no comments created on successful autofill
|
||||||
const comments = await prisma.assetComment.findMany({
|
const comments = await prisma.assetComment.findMany({
|
||||||
where: { assetId: asset.id },
|
where: { assetId: asset.id },
|
||||||
orderBy: { createdAt: 'asc' },
|
orderBy: { createdAt: 'asc' },
|
||||||
})
|
})
|
||||||
expect(comments.length).toBeGreaterThan(0)
|
expect(comments.length).toBe(0)
|
||||||
// The last updated message of placeholder comment should indicate success
|
|
||||||
const finalComment = comments[comments.length - 1]
|
|
||||||
expect(finalComment.message).toBe('Autofill completed successfully.')
|
|
||||||
|
|
||||||
// Verify AssetMetadataValue contains the AI-extracted title
|
// Verify AssetMetadataValue contains the AI-extracted title
|
||||||
const titleVal = await prisma.assetMetadataValue.findUnique({
|
const titleVal = await prisma.assetMetadataValue.findUnique({
|
||||||
@@ -324,4 +321,123 @@ describe('Workflow E2E - agentAutofillMedia', () => {
|
|||||||
})
|
})
|
||||||
expect(manualNotesVal).toBeNull()
|
expect(manualNotesVal).toBeNull()
|
||||||
}, 50000)
|
}, 50000)
|
||||||
|
|
||||||
|
it('should post an error comment when agent autofill workflow fails', async () => {
|
||||||
|
// 1. Seed Database
|
||||||
|
const team = await prisma.team.create({
|
||||||
|
data: { name: 'E2E Autofill Fail Team' },
|
||||||
|
})
|
||||||
|
|
||||||
|
const project = await prisma.project.create({
|
||||||
|
data: { name: 'E2E Autofill Fail Project', teamId: team.id },
|
||||||
|
})
|
||||||
|
|
||||||
|
await prisma.metadataField.create({
|
||||||
|
data: {
|
||||||
|
key: 'title_fail_field',
|
||||||
|
scope: 'PROJECT',
|
||||||
|
projectId: project.id,
|
||||||
|
teamId: team.id,
|
||||||
|
config: { name: 'Title', type: 'text', autofillSource: 'CONTENT' },
|
||||||
|
description: 'AI-extracted document title',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const user = await prisma.user.create({
|
||||||
|
data: {
|
||||||
|
name: 'Autofill Fail Agent User',
|
||||||
|
email: 'failautofill@shumai.ai',
|
||||||
|
type: 'agent',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await prisma.teamMember.create({
|
||||||
|
data: {
|
||||||
|
teamId: team.id,
|
||||||
|
userId: user.id,
|
||||||
|
role: 'editor',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const provider = await prisma.provider.create({
|
||||||
|
data: {
|
||||||
|
name: 'google-fail',
|
||||||
|
teamId: team.id,
|
||||||
|
config: { api: 'google-generative-ai', apiKey: 'dummy-google-api-key' },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const model = await prisma.model.create({
|
||||||
|
data: {
|
||||||
|
modelId: 'gemini-fail',
|
||||||
|
name: 'Gemini Fail',
|
||||||
|
providerId: provider.id,
|
||||||
|
config: {
|
||||||
|
input: ['text', 'image'],
|
||||||
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||||
|
contextWindow: 8192,
|
||||||
|
maxTokens: 2048,
|
||||||
|
reasoning: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const agent = await prisma.agent.create({
|
||||||
|
data: {
|
||||||
|
id: user.id,
|
||||||
|
type: 'autofill',
|
||||||
|
enabled: true,
|
||||||
|
teamId: team.id,
|
||||||
|
providerId: provider.id,
|
||||||
|
modelId: model.id,
|
||||||
|
config: { provider: 'google', model: 'gemini-fail' },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const asset = await prisma.asset.create({
|
||||||
|
data: {
|
||||||
|
name: 'fail-sample.mp4',
|
||||||
|
type: 'file',
|
||||||
|
projectId: project.id,
|
||||||
|
mediaType: 'video/mp4',
|
||||||
|
status: 'uploaded',
|
||||||
|
media: {
|
||||||
|
proxyType: 'video',
|
||||||
|
duration: 12.0,
|
||||||
|
videoTranscodes: [{ key: 'files/fail-sample.mp4', width: 1280, height: 720 }],
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- partial MediaInfo mock for testing
|
||||||
|
} as any,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
// Mock prompt to fail
|
||||||
|
vi.spyOn(AgentHarness.prototype, 'prompt').mockRejectedValueOnce(
|
||||||
|
new Error('LLM rate limit reached'),
|
||||||
|
)
|
||||||
|
|
||||||
|
const task = await prisma.workflowTask.create({
|
||||||
|
data: {
|
||||||
|
type: 'ai_metadata_autofill',
|
||||||
|
status: 'pending',
|
||||||
|
assetId: asset.id,
|
||||||
|
projectId: project.id,
|
||||||
|
teamId: team.id,
|
||||||
|
payload: {
|
||||||
|
projectId: project.id,
|
||||||
|
agent: { sessionId: 'mock-session-fail', agentId: agent.id },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
await expect(workflowService.executeWait(task, 45000)).rejects.toThrow()
|
||||||
|
|
||||||
|
// Verify error comment was created on the asset
|
||||||
|
const comments = await prisma.assetComment.findMany({
|
||||||
|
where: { assetId: asset.id },
|
||||||
|
orderBy: { createdAt: 'asc' },
|
||||||
|
})
|
||||||
|
expect(comments.length).toBe(1)
|
||||||
|
expect(comments[0].message).toContain('Autofill failed:')
|
||||||
|
expect(comments[0].message).toContain('LLM rate limit reached')
|
||||||
|
}, 50000)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -178,14 +178,12 @@ describe('Workflow E2E - agentEmbeddingMedia', () => {
|
|||||||
// 5. Verification
|
// 5. Verification
|
||||||
expect(completedTask.status).toBe('completed')
|
expect(completedTask.status).toBe('completed')
|
||||||
|
|
||||||
// Verify AssetComment indicates success
|
// Verify no comments created on successful embedding
|
||||||
const comments = await prisma.assetComment.findMany({
|
const comments = await prisma.assetComment.findMany({
|
||||||
where: { assetId: asset.id },
|
where: { assetId: asset.id },
|
||||||
orderBy: { createdAt: 'asc' },
|
orderBy: { createdAt: 'asc' },
|
||||||
})
|
})
|
||||||
expect(comments.length).toBeGreaterThan(0)
|
expect(comments.length).toBe(0)
|
||||||
const finalComment = comments[comments.length - 1]
|
|
||||||
expect(finalComment.message).toBe('Embedding completed successfully.')
|
|
||||||
|
|
||||||
// Verify raw embedding is saved in DB
|
// Verify raw embedding is saved in DB
|
||||||
const dbEmbeddings = (await prisma.$queryRaw`
|
const dbEmbeddings = (await prisma.$queryRaw`
|
||||||
@@ -445,5 +443,15 @@ describe('Workflow E2E - agentEmbeddingMedia', () => {
|
|||||||
where: { id: task.id },
|
where: { id: task.id },
|
||||||
})
|
})
|
||||||
expect(dbTask?.status).toBe(WorkflowTaskStatus.failed)
|
expect(dbTask?.status).toBe(WorkflowTaskStatus.failed)
|
||||||
|
|
||||||
|
// Verify failure comment was posted to the asset
|
||||||
|
const comments = await prisma.assetComment.findMany({
|
||||||
|
where: { assetId: asset.id },
|
||||||
|
orderBy: { createdAt: 'asc' },
|
||||||
|
})
|
||||||
|
expect(comments.length).toBe(1)
|
||||||
|
expect(comments[0].message).toContain(
|
||||||
|
'Embedding failed: unsupported media type for embeddings: audio/wav',
|
||||||
|
)
|
||||||
}, 50000)
|
}, 50000)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
// @vitest-environment happy-dom
|
// @vitest-environment happy-dom
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { cleanup, render, fireEvent } from '@testing-library/react'
|
import { cleanup, render, fireEvent } from '@testing-library/react'
|
||||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||||
import type { CommentInfo } from '@shumai/dtos'
|
import type { CommentInfo } from '@shumai/dtos'
|
||||||
|
import { useTeamContextStore } from '@/ui/stores/team-context'
|
||||||
import { ChatInput } from './message-input'
|
import { ChatInput } from './message-input'
|
||||||
|
|
||||||
function createWrapper() {
|
function createWrapper() {
|
||||||
@@ -16,6 +17,13 @@ function createWrapper() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe('ChatInput auto-mention on reply', () => {
|
describe('ChatInput auto-mention on reply', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
useTeamContextStore.setState({
|
||||||
|
teamId: 'team-123',
|
||||||
|
projectTeamMap: { 'proj-1': 'team-123' },
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
cleanup()
|
cleanup()
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
|
|||||||
@@ -195,9 +195,12 @@ describe('FileBrowser', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('renders empty state when there are no folders and files and not loading', () => {
|
it('renders empty state when there are no folders and files and not loading', () => {
|
||||||
renderComponent({ folders: [], files: [], isLoading: false })
|
const { container } = renderComponent({ folders: [], files: [], isLoading: false })
|
||||||
|
|
||||||
expect(screen.getByText(/This folder is empty|此文件夹为空/i)).toBeDefined()
|
expect(screen.getByText(/This folder is empty|此文件夹为空/i)).toBeDefined()
|
||||||
|
const emptyArea = container.querySelector('.empty-area')
|
||||||
|
expect(emptyArea).not.toBeNull()
|
||||||
|
expect(emptyArea?.classList.contains('flex-1')).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('renders loading skeleton and does not render empty state when isLoading is true (card view)', () => {
|
it('renders loading skeleton and does not render empty state when isLoading is true (card view)', () => {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import type {
|
|||||||
AssetInfo,
|
AssetInfo,
|
||||||
CollectionInfo,
|
CollectionInfo,
|
||||||
CreateUploadTaskRequest,
|
CreateUploadTaskRequest,
|
||||||
|
FileNode,
|
||||||
SearchCondition,
|
SearchCondition,
|
||||||
SearchSort,
|
SearchSort,
|
||||||
ShareLinkInfo,
|
ShareLinkInfo,
|
||||||
@@ -57,6 +58,7 @@ import { FileBrowserGridView } from './grid-view'
|
|||||||
import { FileBrowserListView } from './list-view'
|
import { FileBrowserListView } from './list-view'
|
||||||
import { FileBrowserToolbar } from './toolbar'
|
import { FileBrowserToolbar } from './toolbar'
|
||||||
import { useFileActions } from './use-file-actions'
|
import { useFileActions } from './use-file-actions'
|
||||||
|
import { useBatchFileStatus } from '@/ui/hooks/use-batch-file-status'
|
||||||
|
|
||||||
interface FileBrowserProps {
|
interface FileBrowserProps {
|
||||||
teamId: string
|
teamId: string
|
||||||
@@ -250,12 +252,17 @@ export function FileBrowser({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const [localUploadingFiles, setLocalUploadingFiles] = useState<AssetInfo[]>([])
|
const [localUploadingFiles, setLocalUploadingFiles] = useState<AssetInfo[]>([])
|
||||||
|
const fileProgress = useUploadStore((state) => state.fileProgress)
|
||||||
|
|
||||||
const displayedFiles = useMemo(() => {
|
const displayedFiles = useMemo(() => {
|
||||||
const existingIds = new Set(files.map((f) => f.id))
|
const existingIds = new Set(files.map((f) => f.id))
|
||||||
const filteredLocal = localUploadingFiles.filter((f) => !existingIds.has(f.id))
|
const filteredLocal = localUploadingFiles
|
||||||
|
.filter((f) => !existingIds.has(f.id))
|
||||||
|
.filter((f) => fileProgress[f.id!]?.status !== 'failed')
|
||||||
return [...filteredLocal, ...files]
|
return [...filteredLocal, ...files]
|
||||||
}, [files, localUploadingFiles])
|
}, [files, localUploadingFiles, fileProgress])
|
||||||
|
|
||||||
|
useBatchFileStatus({ teamId, items: displayedFiles })
|
||||||
|
|
||||||
const activePreviewItem = useMemo(() => {
|
const activePreviewItem = useMemo(() => {
|
||||||
if (selectedIds.size === 0) return null
|
if (selectedIds.size === 0) return null
|
||||||
@@ -530,6 +537,47 @@ export function FileBrowser({
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const throttledInvalidateSearch = useMemo(() => {
|
||||||
|
let timer: ReturnType<typeof setTimeout> | null = null
|
||||||
|
let lastRun = 0
|
||||||
|
const THROTTLE_MS = 2500
|
||||||
|
|
||||||
|
const run = () => {
|
||||||
|
const now = Date.now()
|
||||||
|
if (now - lastRun >= THROTTLE_MS) {
|
||||||
|
lastRun = now
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ['search', teamId, assetId],
|
||||||
|
})
|
||||||
|
} else if (!timer) {
|
||||||
|
timer = setTimeout(
|
||||||
|
() => {
|
||||||
|
timer = null
|
||||||
|
lastRun = Date.now()
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ['search', teamId, assetId],
|
||||||
|
})
|
||||||
|
},
|
||||||
|
THROTTLE_MS - (now - lastRun),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const cancel = () => {
|
||||||
|
if (timer) {
|
||||||
|
clearTimeout(timer)
|
||||||
|
timer = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const reset = () => {
|
||||||
|
cancel()
|
||||||
|
lastRun = Date.now()
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.assign(run, { cancel, reset })
|
||||||
|
}, [queryClient, teamId, assetId])
|
||||||
|
|
||||||
const $createUploadTask = client.api.teams[':teamId'].upload.tasks.$post
|
const $createUploadTask = client.api.teams[':teamId'].upload.tasks.$post
|
||||||
const { mutate: createUploadTaskMutation } = useMutation<
|
const { mutate: createUploadTaskMutation } = useMutation<
|
||||||
InferResponseType<typeof $createUploadTask>,
|
InferResponseType<typeof $createUploadTask>,
|
||||||
@@ -545,6 +593,9 @@ export function FileBrowser({
|
|||||||
onMutate: () => {
|
onMutate: () => {
|
||||||
return { files: [...filesToUpload] }
|
return { files: [...filesToUpload] }
|
||||||
},
|
},
|
||||||
|
onError: () => {
|
||||||
|
toast.error(m.failed_upload_file())
|
||||||
|
},
|
||||||
onSuccess: async (data, variables, context) => {
|
onSuccess: async (data, variables, context) => {
|
||||||
const currentFiles = context?.files || []
|
const currentFiles = context?.files || []
|
||||||
const isCurrentFolderUpload = variables.json.parentId === assetId
|
const isCurrentFolderUpload = variables.json.parentId === assetId
|
||||||
@@ -572,6 +623,7 @@ export function FileBrowser({
|
|||||||
startTask(data.taskId, taskName, filesProgressInfo)
|
startTask(data.taskId, taskName, filesProgressInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let batchIds: Set<string> | null = null
|
||||||
if (isCurrentFolderUpload) {
|
if (isCurrentFolderUpload) {
|
||||||
const newLocalFiles: AssetInfo[] = currentFiles
|
const newLocalFiles: AssetInfo[] = currentFiles
|
||||||
.filter((f) => {
|
.filter((f) => {
|
||||||
@@ -612,37 +664,49 @@ export function FileBrowser({
|
|||||||
updatedAt: new Date().toISOString(),
|
updatedAt: new Date().toISOString(),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
batchIds = new Set(newLocalFiles.map((f) => f.id!))
|
||||||
setLocalUploadingFiles((prev) => [...newLocalFiles, ...prev])
|
setLocalUploadingFiles((prev) => [...newLocalFiles, ...prev])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invalidate queries immediately so any newly created folders appear
|
// Invalidate queries immediately only if newly created folders appear
|
||||||
queryClient.invalidateQueries({
|
const hasCreatedFolders = (variables.json.files as FileNode[]).some(
|
||||||
queryKey: ['search', teamId, assetId],
|
(f) => f.children && f.children.length > 0,
|
||||||
})
|
)
|
||||||
|
if (hasCreatedFolders) {
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ['search', teamId, assetId],
|
||||||
|
})
|
||||||
|
}
|
||||||
queryClient.invalidateQueries({
|
queryClient.invalidateQueries({
|
||||||
queryKey: ['teams', teamId, 'upload', 'tasks'],
|
queryKey: ['teams', teamId, 'upload', 'tasks'],
|
||||||
})
|
})
|
||||||
|
throttledInvalidateSearch.reset()
|
||||||
|
|
||||||
await uploadFilesWithUppy({
|
try {
|
||||||
files: currentFiles,
|
await uploadFilesWithUppy({
|
||||||
taskId: data.taskId!,
|
files: currentFiles,
|
||||||
teamId: teamId!,
|
taskId: data.taskId!,
|
||||||
storageBackend: (data.storageBackend as 's3' | 'local') || 'local',
|
teamId: teamId!,
|
||||||
createdAssets: data.createdAssets,
|
storageBackend: (data.storageBackend as 's3' | 'local') || 'local',
|
||||||
presignedUrls: data.presignedUrls,
|
createdAssets: data.createdAssets,
|
||||||
onFileFinished: async (fileId) => {
|
presignedUrls: data.presignedUrls,
|
||||||
await queryClient.invalidateQueries({
|
onFileFinished: (fileId) => {
|
||||||
queryKey: ['search', teamId, assetId],
|
setLocalUploadingFiles((prev) => prev.filter((f) => f.id !== fileId))
|
||||||
})
|
throttledInvalidateSearch()
|
||||||
setLocalUploadingFiles((prev) => prev.filter((f) => f.id !== fileId))
|
},
|
||||||
},
|
})
|
||||||
})
|
} finally {
|
||||||
queryClient.invalidateQueries({
|
throttledInvalidateSearch.cancel()
|
||||||
queryKey: ['search', teamId, assetId],
|
if (batchIds) {
|
||||||
})
|
setLocalUploadingFiles((prev) => prev.filter((f) => !batchIds!.has(f.id!)))
|
||||||
queryClient.invalidateQueries({
|
}
|
||||||
queryKey: ['teams', teamId, 'upload', 'tasks'],
|
queryClient.invalidateQueries({
|
||||||
})
|
queryKey: ['search', teamId, assetId],
|
||||||
|
})
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ['teams', teamId, 'upload', 'tasks'],
|
||||||
|
})
|
||||||
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -1007,6 +1071,27 @@ export function FileBrowser({
|
|||||||
>
|
>
|
||||||
{isLoading && folders.length === 0 && displayedFiles.length === 0 ? (
|
{isLoading && folders.length === 0 && displayedFiles.length === 0 ? (
|
||||||
<FileBrowserContentSkeleton displayStyle={displayStyle} />
|
<FileBrowserContentSkeleton displayStyle={displayStyle} />
|
||||||
|
) : !isLoading &&
|
||||||
|
folders.length === 0 &&
|
||||||
|
displayedFiles.length === 0 &&
|
||||||
|
!isFetchingNextFoldersPage &&
|
||||||
|
!isFetchingNextFilesPage ? (
|
||||||
|
<div
|
||||||
|
className="empty-area flex-1 flex items-center justify-center text-sm text-muted-foreground"
|
||||||
|
onClick={handleEmptyAreaClick}
|
||||||
|
>
|
||||||
|
{isRecents ? (
|
||||||
|
<div className="flex flex-col items-center gap-2 p-8 text-center animate-in fade-in duration-300">
|
||||||
|
<FolderClock className="h-10 w-10 text-muted-foreground/50 mb-1" />
|
||||||
|
<p className="font-medium text-foreground">{m.no_recent_files()}</p>
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
{m.no_recent_files_description()}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<span>{m.this_folder_is_empty()}</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
) : displayStyle === 'list' ? (
|
) : displayStyle === 'list' ? (
|
||||||
<FileBrowserListView
|
<FileBrowserListView
|
||||||
folders={folders}
|
folders={folders}
|
||||||
@@ -1079,26 +1164,6 @@ export function FileBrowser({
|
|||||||
<p>{m.loading_more()}</p>
|
<p>{m.loading_more()}</p>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{!isLoading &&
|
|
||||||
folders.length === 0 &&
|
|
||||||
displayedFiles.length === 0 &&
|
|
||||||
!isFetchingNextFoldersPage &&
|
|
||||||
!isFetchingNextFilesPage && (
|
|
||||||
<div className="empty-area flex h-full items-center justify-center text-sm text-muted-foreground">
|
|
||||||
{isRecents ? (
|
|
||||||
<div className="flex flex-col items-center gap-2 p-8 text-center animate-in fade-in duration-300">
|
|
||||||
<FolderClock className="h-10 w-10 text-muted-foreground/50 mb-1" />
|
|
||||||
<p className="font-medium text-foreground">{m.no_recent_files()}</p>
|
|
||||||
<p className="text-xs text-muted-foreground">
|
|
||||||
{m.no_recent_files_description()}
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
<span>{m.this_folder_is_empty()}</span>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{selectedCount > 0 && (
|
{selectedCount > 0 && (
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|||||||
import { FileCard } from './file-card'
|
import { FileCard } from './file-card'
|
||||||
import type { AssetInfo } from '@shumai/dtos'
|
import type { AssetInfo } from '@shumai/dtos'
|
||||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||||
|
import { useUploadStore } from '@/ui/stores/upload'
|
||||||
|
|
||||||
vi.mock('@dnd-kit/react', () => ({
|
vi.mock('@dnd-kit/react', () => ({
|
||||||
useDraggable: () => ({
|
useDraggable: () => ({
|
||||||
@@ -341,6 +342,35 @@ describe('FileCard', () => {
|
|||||||
expect(screen.queryByText(/Alice/i)).toBeNull()
|
expect(screen.queryByText(/Alice/i)).toBeNull()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('transitions from uploading to preparing circle when upload store marks file completed', () => {
|
||||||
|
useUploadStore.setState({
|
||||||
|
fileProgress: {
|
||||||
|
'file-uploading': {
|
||||||
|
fileId: 'file-uploading',
|
||||||
|
name: 'clip.mp4',
|
||||||
|
loaded: 1000,
|
||||||
|
total: 1000,
|
||||||
|
status: 'completed',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const uploadingItem: AssetInfo = {
|
||||||
|
...fileItem,
|
||||||
|
id: 'file-uploading',
|
||||||
|
status: 'uploading',
|
||||||
|
creator: { id: 'u1', name: 'Alice' },
|
||||||
|
} as AssetInfo
|
||||||
|
|
||||||
|
renderComponent({ item: uploadingItem })
|
||||||
|
|
||||||
|
expect(screen.queryByTestId('file-card-preview-media')).toBeNull()
|
||||||
|
const circle = screen.getByTestId('file-card-preparing-circle')
|
||||||
|
expect(circle.tagName.toLowerCase()).toBe('svg')
|
||||||
|
expect(screen.getByText(/Preparing|准备中/i)).toBeTruthy()
|
||||||
|
expect(screen.queryByText(/Alice/i)).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
it('shows a preparing circle and "Preparing..." when processing without a preview yet', () => {
|
it('shows a preparing circle and "Preparing..." when processing without a preview yet', () => {
|
||||||
const processingItem: AssetInfo = {
|
const processingItem: AssetInfo = {
|
||||||
...fileItem,
|
...fileItem,
|
||||||
|
|||||||
@@ -119,11 +119,6 @@ export function FileCard({
|
|||||||
: 0
|
: 0
|
||||||
: 0
|
: 0
|
||||||
|
|
||||||
const shouldPoll =
|
|
||||||
(item.type === 'file' || item.type === 'version_stack') &&
|
|
||||||
!!teamId &&
|
|
||||||
(item.status === 'uploading' || item.status === 'uploaded' || item.status === 'processing')
|
|
||||||
|
|
||||||
const { data: polledItem } = useQuery({
|
const { data: polledItem } = useQuery({
|
||||||
queryKey: ['file', teamId, item.id],
|
queryKey: ['file', teamId, item.id],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
@@ -133,13 +128,7 @@ export function FileCard({
|
|||||||
if (!res.ok) throw new Error('failed to fetch file')
|
if (!res.ok) throw new Error('failed to fetch file')
|
||||||
return (await res.json()) as unknown as AssetInfo
|
return (await res.json()) as unknown as AssetInfo
|
||||||
},
|
},
|
||||||
enabled: shouldPoll,
|
enabled: false,
|
||||||
refetchInterval: (query: unknown) => {
|
|
||||||
const data = (query as { state: { data: { status?: string } } }).state.data
|
|
||||||
return data?.status === 'processed' || data?.status === 'failed' || data?.status === 'error'
|
|
||||||
? false
|
|
||||||
: 1000
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const displayItem = useMemo(() => {
|
const displayItem = useMemo(() => {
|
||||||
@@ -277,16 +266,16 @@ export function FileCard({
|
|||||||
// since the poster/sprite are generated and persisted ahead of the proxy transcodes.
|
// since the poster/sprite are generated and persisted ahead of the proxy transcodes.
|
||||||
const hasPreview = Boolean(displayItem.preview?.thumbnailUrl || displayItem.preview?.spriteUrl)
|
const hasPreview = Boolean(displayItem.preview?.thumbnailUrl || displayItem.preview?.spriteUrl)
|
||||||
const isFailed = displayItem.status === 'failed'
|
const isFailed = displayItem.status === 'failed'
|
||||||
const isProcessing = displayItem.status === 'processing' || displayItem.status === 'uploaded'
|
const isUploadDone = fileUploadState?.status === 'completed' || uploadPercent >= 100
|
||||||
|
const isUploading = displayItem.status === 'uploading' && !isUploadDone
|
||||||
|
const isProcessing =
|
||||||
|
displayItem.status === 'processing' ||
|
||||||
|
displayItem.status === 'uploaded' ||
|
||||||
|
(displayItem.status === 'uploading' && isUploadDone)
|
||||||
|
|
||||||
// While uploading/transcoding, the creator row is replaced by a short status label so the real
|
// While uploading/transcoding, the creator row is replaced by a short status label so the real
|
||||||
// date/author only appears once the asset is ready or failed.
|
// date/author only appears once the asset is ready or failed.
|
||||||
const statusText =
|
const statusText = isUploading ? m.uploading() : isProcessing ? m.preparing() : null
|
||||||
displayItem.status === 'uploading'
|
|
||||||
? m.uploading()
|
|
||||||
: displayItem.status === 'uploaded' || displayItem.status === 'processing'
|
|
||||||
? m.preparing()
|
|
||||||
: null
|
|
||||||
|
|
||||||
const failedErrorMessage = displayItem.media?.error || m.processing_failed()
|
const failedErrorMessage = displayItem.media?.error || m.processing_failed()
|
||||||
|
|
||||||
@@ -393,7 +382,7 @@ export function FileCard({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="relative aspect-square overflow-hidden bg-muted/30">
|
<div className="relative aspect-square overflow-hidden bg-muted/30">
|
||||||
{displayItem.status === 'uploading' ? (
|
{isUploading ? (
|
||||||
<div className="flex h-full w-full items-center justify-center bg-background/50">
|
<div className="flex h-full w-full items-center justify-center bg-background/50">
|
||||||
<ProgressCircle progress={uploadPercent} className="w-16 h-16 z-10" />
|
<ProgressCircle progress={uploadPercent} className="w-16 h-16 z-10" />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -74,11 +74,6 @@ export function FileListItem({
|
|||||||
: 0
|
: 0
|
||||||
: 0
|
: 0
|
||||||
|
|
||||||
const shouldPoll =
|
|
||||||
(item.type === 'file' || item.type === 'version_stack') &&
|
|
||||||
!!teamId &&
|
|
||||||
(item.status === 'uploading' || item.status === 'uploaded' || item.status === 'processing')
|
|
||||||
|
|
||||||
const { data: polledItem } = useQuery({
|
const { data: polledItem } = useQuery({
|
||||||
queryKey: ['file', teamId, item.id],
|
queryKey: ['file', teamId, item.id],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
@@ -88,13 +83,7 @@ export function FileListItem({
|
|||||||
if (!res.ok) throw new Error('failed to fetch file')
|
if (!res.ok) throw new Error('failed to fetch file')
|
||||||
return (await res.json()) as unknown as AssetInfo
|
return (await res.json()) as unknown as AssetInfo
|
||||||
},
|
},
|
||||||
enabled: shouldPoll,
|
enabled: false,
|
||||||
refetchInterval: (query: unknown) => {
|
|
||||||
const data = (query as { state: { data: { status?: string } } }).state.data
|
|
||||||
return data?.status === 'processed' || data?.status === 'failed' || data?.status === 'error'
|
|
||||||
? false
|
|
||||||
: 1000
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const displayItem = useMemo(() => {
|
const displayItem = useMemo(() => {
|
||||||
@@ -111,6 +100,9 @@ export function FileListItem({
|
|||||||
return polledItem || item
|
return polledItem || item
|
||||||
}, [item, polledItem])
|
}, [item, polledItem])
|
||||||
|
|
||||||
|
const isUploadDone = fileUploadState?.status === 'completed' || uploadPercent >= 100
|
||||||
|
const isUploading = displayItem.status === 'uploading' && !isUploadDone
|
||||||
|
|
||||||
// dnd-kit hooks
|
// dnd-kit hooks
|
||||||
const { ref: setDraggableRef, isDragging: isDraggableDragging } = useDraggable({
|
const { ref: setDraggableRef, isDragging: isDraggableDragging } = useDraggable({
|
||||||
id: `browser:${displayItem.id!}`,
|
id: `browser:${displayItem.id!}`,
|
||||||
@@ -220,7 +212,7 @@ export function FileListItem({
|
|||||||
className="h-4 w-4 shrink-0 data-[state=checked]:bg-primary data-[state=checked]:border-primary"
|
className="h-4 w-4 shrink-0 data-[state=checked]:bg-primary data-[state=checked]:border-primary"
|
||||||
/>
|
/>
|
||||||
<div className="w-8 h-8 shrink-0 flex items-center justify-center bg-muted rounded overflow-hidden">
|
<div className="w-8 h-8 shrink-0 flex items-center justify-center bg-muted rounded overflow-hidden">
|
||||||
{displayItem.status === 'uploading' ? (
|
{isUploading ? (
|
||||||
<ProgressCircle progress={uploadPercent} className="w-7 h-7" />
|
<ProgressCircle progress={uploadPercent} className="w-7 h-7" />
|
||||||
) : displayItem.status === 'failed' && !displayItem.preview?.thumbnailUrl ? (
|
) : displayItem.status === 'failed' && !displayItem.preview?.thumbnailUrl ? (
|
||||||
<AlertCircle className="h-4 w-4 text-destructive" />
|
<AlertCircle className="h-4 w-4 text-destructive" />
|
||||||
|
|||||||
@@ -358,7 +358,7 @@ describe('MobileFileBrowser', () => {
|
|||||||
window.XMLHttpRequest = originalXhr
|
window.XMLHttpRequest = originalXhr
|
||||||
})
|
})
|
||||||
|
|
||||||
it('fails upload and confirms with error on network failure', async () => {
|
it('keeps upload active and does not fail task on network failure', async () => {
|
||||||
const originalXhr = window.XMLHttpRequest
|
const originalXhr = window.XMLHttpRequest
|
||||||
const patchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) })
|
const patchMock = vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) })
|
||||||
|
|
||||||
@@ -409,17 +409,13 @@ describe('MobileFileBrowser', () => {
|
|||||||
fireEvent.change(fileInput, { target: { files: [testFile] } })
|
fireEvent.change(fileInput, { target: { files: [testFile] } })
|
||||||
|
|
||||||
await vi.waitFor(() => {
|
await vi.waitFor(() => {
|
||||||
expect(patchMock).toHaveBeenCalledWith(
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
expect.objectContaining({
|
expect((client.api.teams[':teamId'].upload.tasks as any)['$post']).toHaveBeenCalled()
|
||||||
param: { teamId: 'team-1', taskId: 'task-3' },
|
|
||||||
json: expect.objectContaining({
|
|
||||||
fileId: 'server-file-3',
|
|
||||||
errorMessage: expect.any(String),
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Network failures are retried automatically with backoff and do not fail task on server
|
||||||
|
expect(patchMock).not.toHaveBeenCalled()
|
||||||
|
|
||||||
window.XMLHttpRequest = originalXhr
|
window.XMLHttpRequest = originalXhr
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { usePermissions } from '@/ui/hooks/use-permissions'
|
|||||||
import { m } from '@/ui/paraglide/messages.js'
|
import { m } from '@/ui/paraglide/messages.js'
|
||||||
import { useUploadStore } from '@/ui/stores/upload'
|
import { useUploadStore } from '@/ui/stores/upload'
|
||||||
import { uploadFilesWithUppy } from '@/ui/lib/uploader'
|
import { uploadFilesWithUppy } from '@/ui/lib/uploader'
|
||||||
import type { AssetInfo, CreateUploadTaskRequest } from '@shumai/dtos'
|
import type { AssetInfo, CreateUploadTaskRequest, FileNode } from '@shumai/dtos'
|
||||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||||
import type { InferRequestType, InferResponseType } from 'hono/client'
|
import type { InferRequestType, InferResponseType } from 'hono/client'
|
||||||
import { formatSize } from '@/ui/lib/format'
|
import { formatSize } from '@/ui/lib/format'
|
||||||
@@ -20,7 +20,7 @@ import {
|
|||||||
Plus,
|
Plus,
|
||||||
Upload,
|
Upload,
|
||||||
} from 'lucide-react'
|
} from 'lucide-react'
|
||||||
import React, { useRef, useState } from 'react'
|
import React, { useMemo, useRef, useState } from 'react'
|
||||||
import { useInView } from 'react-intersection-observer'
|
import { useInView } from 'react-intersection-observer'
|
||||||
import { toast } from 'sonner'
|
import { toast } from 'sonner'
|
||||||
import { ulid } from 'ulid'
|
import { ulid } from 'ulid'
|
||||||
@@ -54,6 +54,7 @@ import { FileCard } from './file-card'
|
|||||||
import { FolderCard } from './folder-card'
|
import { FolderCard } from './folder-card'
|
||||||
import { useFileActions } from './use-file-actions'
|
import { useFileActions } from './use-file-actions'
|
||||||
import { MobileFileBrowserContentSkeleton } from '../loading-skeletons'
|
import { MobileFileBrowserContentSkeleton } from '../loading-skeletons'
|
||||||
|
import { useBatchFileStatus } from '@/ui/hooks/use-batch-file-status'
|
||||||
|
|
||||||
export interface MobileFileBrowserProps {
|
export interface MobileFileBrowserProps {
|
||||||
teamId: string
|
teamId: string
|
||||||
@@ -125,6 +126,7 @@ export function MobileFileBrowser({
|
|||||||
allowDownload = true,
|
allowDownload = true,
|
||||||
}: MobileFileBrowserProps) {
|
}: MobileFileBrowserProps) {
|
||||||
const queryClient = useQueryClient()
|
const queryClient = useQueryClient()
|
||||||
|
useBatchFileStatus({ teamId, items: files })
|
||||||
const { canEdit, canAdmin } = usePermissions(projectId)
|
const { canEdit, canAdmin } = usePermissions(projectId)
|
||||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||||
const folderInputRef = useRef<HTMLInputElement>(null)
|
const folderInputRef = useRef<HTMLInputElement>(null)
|
||||||
@@ -200,6 +202,47 @@ export function MobileFileBrowser({
|
|||||||
// Upload actions
|
// Upload actions
|
||||||
const startTask = useUploadStore((state) => state.startTask)
|
const startTask = useUploadStore((state) => state.startTask)
|
||||||
|
|
||||||
|
const throttledInvalidateSearch = useMemo(() => {
|
||||||
|
let timer: ReturnType<typeof setTimeout> | null = null
|
||||||
|
let lastRun = 0
|
||||||
|
const THROTTLE_MS = 2500
|
||||||
|
|
||||||
|
const run = () => {
|
||||||
|
const now = Date.now()
|
||||||
|
if (now - lastRun >= THROTTLE_MS) {
|
||||||
|
lastRun = now
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ['search', teamId, assetId],
|
||||||
|
})
|
||||||
|
} else if (!timer) {
|
||||||
|
timer = setTimeout(
|
||||||
|
() => {
|
||||||
|
timer = null
|
||||||
|
lastRun = Date.now()
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ['search', teamId, assetId],
|
||||||
|
})
|
||||||
|
},
|
||||||
|
THROTTLE_MS - (now - lastRun),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const cancel = () => {
|
||||||
|
if (timer) {
|
||||||
|
clearTimeout(timer)
|
||||||
|
timer = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const reset = () => {
|
||||||
|
cancel()
|
||||||
|
lastRun = Date.now()
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.assign(run, { cancel, reset })
|
||||||
|
}, [queryClient, teamId, assetId])
|
||||||
|
|
||||||
const $createUploadTask = client.api.teams[':teamId'].upload.tasks.$post
|
const $createUploadTask = client.api.teams[':teamId'].upload.tasks.$post
|
||||||
const { mutate: createUploadTaskMutation } = useMutation<
|
const { mutate: createUploadTaskMutation } = useMutation<
|
||||||
InferResponseType<typeof $createUploadTask>,
|
InferResponseType<typeof $createUploadTask>,
|
||||||
@@ -215,7 +258,10 @@ export function MobileFileBrowser({
|
|||||||
onMutate: () => {
|
onMutate: () => {
|
||||||
return { files: [...pendingFilesToUpload] }
|
return { files: [...pendingFilesToUpload] }
|
||||||
},
|
},
|
||||||
onSuccess: async (data, _variables, context) => {
|
onError: () => {
|
||||||
|
toast.error(m.failed_upload_file())
|
||||||
|
},
|
||||||
|
onSuccess: async (data, variables, context) => {
|
||||||
const currentFiles = context?.files || []
|
const currentFiles = context?.files || []
|
||||||
const filesProgressInfo = currentFiles
|
const filesProgressInfo = currentFiles
|
||||||
.map((f) => {
|
.map((f) => {
|
||||||
@@ -237,22 +283,32 @@ export function MobileFileBrowser({
|
|||||||
startTask(data.taskId, taskName, filesProgressInfo)
|
startTask(data.taskId, taskName, filesProgressInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
queryClient.invalidateQueries({ queryKey: ['search', teamId, assetId] })
|
const hasCreatedFolders = (variables.json.files as FileNode[]).some(
|
||||||
|
(f) => f.children && f.children.length > 0,
|
||||||
|
)
|
||||||
|
if (hasCreatedFolders) {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['search', teamId, assetId] })
|
||||||
|
}
|
||||||
queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'upload', 'tasks'] })
|
queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'upload', 'tasks'] })
|
||||||
|
throttledInvalidateSearch.reset()
|
||||||
|
|
||||||
await uploadFilesWithUppy({
|
try {
|
||||||
files: currentFiles,
|
await uploadFilesWithUppy({
|
||||||
taskId: data.taskId!,
|
files: currentFiles,
|
||||||
teamId: teamId!,
|
taskId: data.taskId!,
|
||||||
storageBackend: (data.storageBackend as 's3' | 'local') || 'local',
|
teamId: teamId!,
|
||||||
createdAssets: data.createdAssets,
|
storageBackend: (data.storageBackend as 's3' | 'local') || 'local',
|
||||||
presignedUrls: data.presignedUrls,
|
createdAssets: data.createdAssets,
|
||||||
onFileFinished: async () => {
|
presignedUrls: data.presignedUrls,
|
||||||
await queryClient.invalidateQueries({ queryKey: ['search', teamId, assetId] })
|
onFileFinished: () => {
|
||||||
},
|
throttledInvalidateSearch()
|
||||||
})
|
},
|
||||||
queryClient.invalidateQueries({ queryKey: ['search', teamId, assetId] })
|
})
|
||||||
queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'upload', 'tasks'] })
|
} finally {
|
||||||
|
throttledInvalidateSearch.cancel()
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['search', teamId, assetId] })
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'upload', 'tasks'] })
|
||||||
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,172 @@
|
|||||||
|
// @vitest-environment happy-dom
|
||||||
|
import { cleanup, render, screen, fireEvent, waitFor } from '@testing-library/react'
|
||||||
|
import React from 'react'
|
||||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||||
|
import { UploadTasks } from './upload-tasks'
|
||||||
|
import { useUploadStore } from '@/ui/stores/upload'
|
||||||
|
import * as uploaderLib from '@/ui/lib/uploader'
|
||||||
|
import { toast } from 'sonner'
|
||||||
|
|
||||||
|
const mockGetTasks = vi.fn()
|
||||||
|
const mockCancelTask = vi.fn()
|
||||||
|
|
||||||
|
vi.mock('@/ui/api/client', () => ({
|
||||||
|
client: {
|
||||||
|
api: {
|
||||||
|
teams: {
|
||||||
|
':teamId': {
|
||||||
|
upload: {
|
||||||
|
tasks: Object.assign(
|
||||||
|
{
|
||||||
|
$get: () => mockGetTasks(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
':taskId': {
|
||||||
|
cancel: {
|
||||||
|
$post: (args: unknown) => mockCancelTask(args),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
|
vi.mock('@/ui/hooks/use-team-id', () => ({
|
||||||
|
useTeamId: () => 'team-1',
|
||||||
|
}))
|
||||||
|
|
||||||
|
vi.mock('sonner', () => ({
|
||||||
|
toast: {
|
||||||
|
success: vi.fn(),
|
||||||
|
error: vi.fn(),
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
|
describe('UploadTasks', () => {
|
||||||
|
let queryClient: QueryClient
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks()
|
||||||
|
queryClient = new QueryClient({
|
||||||
|
defaultOptions: {
|
||||||
|
queries: { retry: false },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
useUploadStore.setState({
|
||||||
|
uploading: 0,
|
||||||
|
tasks: {},
|
||||||
|
fileProgress: {},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
cleanup()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('renders completed and failed tasks without a cancel button', async () => {
|
||||||
|
mockGetTasks.mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
json: async () => ({
|
||||||
|
data: [
|
||||||
|
{
|
||||||
|
id: 'task-done',
|
||||||
|
name: 'Completed Task',
|
||||||
|
total: 2,
|
||||||
|
uploaded: 2,
|
||||||
|
status: 'completed',
|
||||||
|
createdAt: new Date().toISOString(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'task-failed',
|
||||||
|
name: 'Failed Task',
|
||||||
|
total: 2,
|
||||||
|
uploaded: 1,
|
||||||
|
status: 'failed',
|
||||||
|
createdAt: new Date().toISOString(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
pageInfo: {},
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
render(
|
||||||
|
<QueryClientProvider client={queryClient}>
|
||||||
|
<UploadTasks />
|
||||||
|
</QueryClientProvider>,
|
||||||
|
)
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(screen.getByText('Completed Task')).toBeDefined()
|
||||||
|
expect(screen.getByText('Failed Task')).toBeDefined()
|
||||||
|
})
|
||||||
|
|
||||||
|
// No cancel button should be rendered for completed or failed tasks
|
||||||
|
expect(screen.queryByRole('button', { name: /cancel/i })).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('renders cancel button for running tasks and executes cancel flow on confirmation', async () => {
|
||||||
|
const cancelActiveSpy = vi.spyOn(uploaderLib, 'cancelActiveUploadTask')
|
||||||
|
|
||||||
|
mockGetTasks.mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
json: async () => ({
|
||||||
|
data: [
|
||||||
|
{
|
||||||
|
id: 'task-running',
|
||||||
|
name: 'Running Task',
|
||||||
|
total: 3,
|
||||||
|
uploaded: 1,
|
||||||
|
status: 'uploading',
|
||||||
|
createdAt: new Date().toISOString(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
pageInfo: {},
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
mockCancelTask.mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
json: async () => ({ success: true }),
|
||||||
|
})
|
||||||
|
|
||||||
|
render(
|
||||||
|
<QueryClientProvider client={queryClient}>
|
||||||
|
<UploadTasks />
|
||||||
|
</QueryClientProvider>,
|
||||||
|
)
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(screen.getByText('Running Task')).toBeDefined()
|
||||||
|
})
|
||||||
|
|
||||||
|
// Cancel button should be present
|
||||||
|
const cancelButton = screen.getByRole('button', { name: /cancel/i })
|
||||||
|
expect(cancelButton).toBeDefined()
|
||||||
|
|
||||||
|
// Click Cancel to open confirmation dialog
|
||||||
|
fireEvent.click(cancelButton)
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(screen.getByText(/cancel upload task\?/i)).toBeDefined()
|
||||||
|
})
|
||||||
|
|
||||||
|
// Click confirm in the dialog
|
||||||
|
const confirmButtons = screen.getAllByRole('button', { name: /cancel upload/i })
|
||||||
|
fireEvent.click(confirmButtons[confirmButtons.length - 1])
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(cancelActiveSpy).toHaveBeenCalledWith('task-running')
|
||||||
|
expect(mockCancelTask).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
param: { teamId: 'team-1', taskId: 'task-running' },
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
expect(toast.success).toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,13 +1,26 @@
|
|||||||
import { client } from '@/ui/api/client'
|
import { client } from '@/ui/api/client'
|
||||||
import type { TaskInfo } from '@shumai/dtos'
|
import type { TaskInfo } from '@shumai/dtos'
|
||||||
import { useTeamId } from '@/ui/hooks/use-team-id'
|
import { useTeamId } from '@/ui/hooks/use-team-id'
|
||||||
import { useInfiniteQuery } from '@tanstack/react-query'
|
import { useInfiniteQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||||
import { useUploadStore } from '@/ui/stores/upload'
|
import { useUploadStore } from '@/ui/stores/upload'
|
||||||
import { Loader2, CheckCircle2, Clock, UploadCloud } from 'lucide-react'
|
import { cancelActiveUploadTask } from '@/ui/lib/uploader'
|
||||||
import { useEffect, useMemo } from 'react'
|
import { Loader2, CheckCircle2, Clock, UploadCloud, AlertCircle } from 'lucide-react'
|
||||||
|
import { useEffect, useMemo, useState } from 'react'
|
||||||
import { useInView } from 'react-intersection-observer'
|
import { useInView } from 'react-intersection-observer'
|
||||||
import { format, parseISO, isToday, isYesterday } from 'date-fns'
|
import { format, parseISO, isToday, isYesterday } from 'date-fns'
|
||||||
import { Progress } from '@/ui/components/ui/progress'
|
import { Progress } from '@/ui/components/ui/progress'
|
||||||
|
import { Button } from '@/ui/components/ui/button'
|
||||||
|
import {
|
||||||
|
AlertDialog,
|
||||||
|
AlertDialogAction,
|
||||||
|
AlertDialogCancel,
|
||||||
|
AlertDialogContent,
|
||||||
|
AlertDialogDescription,
|
||||||
|
AlertDialogFooter,
|
||||||
|
AlertDialogHeader,
|
||||||
|
AlertDialogTitle,
|
||||||
|
} from '@/ui/components/ui/alert-dialog'
|
||||||
|
import { toast } from 'sonner'
|
||||||
import { formatTimeAgo } from '@/ui/lib/time'
|
import { formatTimeAgo } from '@/ui/lib/time'
|
||||||
import { m } from '@/ui/paraglide/messages.js'
|
import { m } from '@/ui/paraglide/messages.js'
|
||||||
|
|
||||||
@@ -33,20 +46,29 @@ function formatBytes(bytes: number): string {
|
|||||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i]
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
function UploadTaskItem({ task }: { task: TaskInfo }) {
|
function UploadTaskItem({ task, teamId }: { task: TaskInfo; teamId?: string }) {
|
||||||
|
const [isCancelConfirmOpen, setIsCancelConfirmOpen] = useState(false)
|
||||||
const clientProgress = useUploadStore((state) => state.tasks[task.id])
|
const clientProgress = useUploadStore((state) => state.tasks[task.id])
|
||||||
|
const cancelTaskInStore = useUploadStore((state) => state.cancelTask)
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
|
||||||
const isCompleted = clientProgress
|
const isCompleted = clientProgress
|
||||||
? clientProgress.loaded === clientProgress.total && task.uploaded === task.total
|
? clientProgress.loaded === clientProgress.total && task.uploaded === task.total
|
||||||
: task.uploaded === task.total
|
: task.uploaded === task.total
|
||||||
|
|
||||||
const percent = clientProgress
|
const isFailed = task.status === 'failed'
|
||||||
? clientProgress.total > 0
|
const isDone = task.status === 'completed' || isCompleted
|
||||||
? (clientProgress.loaded / clientProgress.total) * 100
|
const isRunning = !isDone && !isFailed
|
||||||
: 0
|
|
||||||
: task.total > 0
|
const percent = isDone
|
||||||
? (task.uploaded / task.total) * 100
|
? 100
|
||||||
: 0
|
: clientProgress
|
||||||
|
? clientProgress.total > 0
|
||||||
|
? (clientProgress.loaded / clientProgress.total) * 100
|
||||||
|
: 0
|
||||||
|
: task.total > 0
|
||||||
|
? (task.uploaded / task.total) * 100
|
||||||
|
: 0
|
||||||
|
|
||||||
let formattedTime: string
|
let formattedTime: string
|
||||||
try {
|
try {
|
||||||
@@ -55,6 +77,37 @@ function UploadTaskItem({ task }: { task: TaskInfo }) {
|
|||||||
formattedTime = m.unknown()
|
formattedTime = m.unknown()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { mutate: cancelMutation, isPending: isCanceling } = useMutation({
|
||||||
|
mutationFn: async () => {
|
||||||
|
cancelActiveUploadTask(task.id)
|
||||||
|
cancelTaskInStore(task.id)
|
||||||
|
|
||||||
|
if (teamId) {
|
||||||
|
const res = await client.api.teams[':teamId'].upload.tasks[':taskId'].cancel.$post({
|
||||||
|
param: { teamId, taskId: task.id },
|
||||||
|
})
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new Error('Failed to cancel upload task')
|
||||||
|
}
|
||||||
|
return await res.json()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onSuccess: () => {
|
||||||
|
setIsCancelConfirmOpen(false)
|
||||||
|
toast.success(m.upload_task_canceled())
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ['teams', teamId, 'upload', 'tasks'],
|
||||||
|
})
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ['search', teamId],
|
||||||
|
})
|
||||||
|
},
|
||||||
|
onError: (err) => {
|
||||||
|
console.error('Failed to cancel upload task:', err)
|
||||||
|
toast.error(m.failed_to_cancel_upload_task())
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="group relative flex flex-col p-4 rounded-xl border border-sidebar-border bg-sidebar-accent/30 text-sidebar-foreground shadow-xs transition-all duration-200 hover:shadow-md hover:border-sidebar-foreground/20 hover:bg-sidebar-accent/50">
|
<div className="group relative flex flex-col p-4 rounded-xl border border-sidebar-border bg-sidebar-accent/30 text-sidebar-foreground shadow-xs transition-all duration-200 hover:shadow-md hover:border-sidebar-foreground/20 hover:bg-sidebar-accent/50">
|
||||||
{/* Top row: Name & Status Badge */}
|
{/* Top row: Name & Status Badge */}
|
||||||
@@ -68,11 +121,16 @@ function UploadTaskItem({ task }: { task: TaskInfo }) {
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex-shrink-0">
|
<div className="flex-shrink-0">
|
||||||
{isCompleted ? (
|
{isDone ? (
|
||||||
<span className="inline-flex items-center gap-1 text-xs font-semibold text-muted-foreground bg-muted px-2 py-0.5 rounded-full transition-colors">
|
<span className="inline-flex items-center gap-1 text-xs font-semibold text-muted-foreground bg-muted px-2 py-0.5 rounded-full transition-colors">
|
||||||
<CheckCircle2 className="w-3.5 h-3.5" />
|
<CheckCircle2 className="w-3.5 h-3.5" />
|
||||||
{m.done()}
|
{m.done()}
|
||||||
</span>
|
</span>
|
||||||
|
) : isFailed ? (
|
||||||
|
<span className="inline-flex items-center gap-1 text-xs font-semibold text-destructive bg-destructive/10 px-2 py-0.5 rounded-full">
|
||||||
|
<AlertCircle className="w-3.5 h-3.5" />
|
||||||
|
{m.status_failed()}
|
||||||
|
</span>
|
||||||
) : (
|
) : (
|
||||||
<span className="inline-flex items-center gap-1 text-xs font-semibold text-sidebar-primary bg-sidebar-primary/10 px-2 py-0.5 rounded-full animate-pulse">
|
<span className="inline-flex items-center gap-1 text-xs font-semibold text-sidebar-primary bg-sidebar-primary/10 px-2 py-0.5 rounded-full animate-pulse">
|
||||||
<Loader2 className="w-3.5 h-3.5 animate-spin" />
|
<Loader2 className="w-3.5 h-3.5 animate-spin" />
|
||||||
@@ -88,7 +146,7 @@ function UploadTaskItem({ task }: { task: TaskInfo }) {
|
|||||||
<span>{formattedTime}</span>
|
<span>{formattedTime}</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Bottom row: Progress Bar and Fraction */}
|
{/* Bottom row: Progress Bar, Fraction & Cancel Button */}
|
||||||
<div className="space-y-1.5 mt-auto">
|
<div className="space-y-1.5 mt-auto">
|
||||||
<div className="flex items-center justify-between text-xs font-semibold">
|
<div className="flex items-center justify-between text-xs font-semibold">
|
||||||
<span className="text-muted-foreground/80">
|
<span className="text-muted-foreground/80">
|
||||||
@@ -96,12 +154,56 @@ function UploadTaskItem({ task }: { task: TaskInfo }) {
|
|||||||
? `${formatBytes(clientProgress.loaded)} / ${formatBytes(clientProgress.total)} (${m.files_progress({ uploaded: task.uploaded, total: task.total })})`
|
? `${formatBytes(clientProgress.loaded)} / ${formatBytes(clientProgress.total)} (${m.files_progress({ uploaded: task.uploaded, total: task.total })})`
|
||||||
: m.files_progress({ uploaded: task.uploaded, total: task.total })}
|
: m.files_progress({ uploaded: task.uploaded, total: task.total })}
|
||||||
</span>
|
</span>
|
||||||
{!isCompleted && (
|
<div className="flex items-center gap-2">
|
||||||
<span className="text-sidebar-primary font-mono font-bold">{Math.round(percent)}%</span>
|
{!isDone && !isFailed && (
|
||||||
)}
|
<span className="text-sidebar-primary font-mono font-bold">
|
||||||
|
{Math.round(percent)}%
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{isRunning && (
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
className="h-6 px-2 text-xs font-medium text-muted-foreground hover:text-destructive hover:bg-destructive/10"
|
||||||
|
disabled={isCanceling}
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation()
|
||||||
|
setIsCancelConfirmOpen(true)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{isCanceling ? <Loader2 className="w-3 h-3 animate-spin mr-1" /> : null}
|
||||||
|
{m.cancel()}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<Progress value={percent} className="h-1.5 w-full bg-muted" />
|
<Progress value={percent} className="h-1.5 w-full bg-muted" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<AlertDialog open={isCancelConfirmOpen} onOpenChange={setIsCancelConfirmOpen}>
|
||||||
|
<AlertDialogContent>
|
||||||
|
<AlertDialogHeader>
|
||||||
|
<AlertDialogTitle>{m.cancel_upload_task_confirm_title()}</AlertDialogTitle>
|
||||||
|
<AlertDialogDescription>
|
||||||
|
{m.cancel_upload_task_confirm_description()}
|
||||||
|
</AlertDialogDescription>
|
||||||
|
</AlertDialogHeader>
|
||||||
|
<AlertDialogFooter>
|
||||||
|
<AlertDialogCancel disabled={isCanceling}>{m.cancel()}</AlertDialogCancel>
|
||||||
|
<AlertDialogAction
|
||||||
|
disabled={isCanceling}
|
||||||
|
onClick={(e) => {
|
||||||
|
e.preventDefault()
|
||||||
|
cancelMutation()
|
||||||
|
}}
|
||||||
|
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||||
|
>
|
||||||
|
{isCanceling ? <Loader2 className="w-4 h-4 animate-spin mr-1" /> : null}
|
||||||
|
{m.cancel_upload_task()}
|
||||||
|
</AlertDialogAction>
|
||||||
|
</AlertDialogFooter>
|
||||||
|
</AlertDialogContent>
|
||||||
|
</AlertDialog>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -218,7 +320,7 @@ export function UploadTasks() {
|
|||||||
{/* Tasks in Day */}
|
{/* Tasks in Day */}
|
||||||
<div className="mt-3 space-y-3">
|
<div className="mt-3 space-y-3">
|
||||||
{group.tasks.map((task) => (
|
{group.tasks.map((task) => (
|
||||||
<UploadTaskItem key={task.id} task={task} />
|
<UploadTaskItem key={task.id} task={task} teamId={teamId} />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import { type ReactElement, useCallback, useRef, useState } from 'react'
|
import { type ReactElement, useCallback, useEffect, useRef, useState } from 'react'
|
||||||
import ReactDOM from 'react-dom/client'
|
import ReactDOM from 'react-dom/client'
|
||||||
import type { MediaController } from '@/ui/components/viewers/types'
|
import type { MediaController } from '@/ui/components/viewers/types'
|
||||||
import VideoPlayer from '@/ui/components/viewers/video/video-viewer'
|
import VideoPlayer from '@/ui/components/viewers/video/video-viewer'
|
||||||
import ModelViewer from '@/ui/components/viewers/model/model-viewer'
|
import ModelViewer from '@/ui/components/viewers/model/model-viewer'
|
||||||
import { calculateFrameCenterTime } from '@/ui/components/viewers/video/utils'
|
import { calculateFrameCenterTime } from '@/ui/components/viewers/video/utils'
|
||||||
import { useUiStore } from '@/ui/stores/ui'
|
import { useUiStore } from '@/ui/stores/ui'
|
||||||
|
import { uploadFilesWithUppy } from '@/ui/lib/uploader'
|
||||||
|
import { useUploadStore } from '@/ui/stores/upload'
|
||||||
|
import { Toaster } from 'sonner'
|
||||||
import {
|
import {
|
||||||
SAMPLE_FRAME_RATE,
|
SAMPLE_FRAME_RATE,
|
||||||
sampleVideoAsset,
|
sampleVideoAsset,
|
||||||
@@ -185,9 +188,95 @@ function Harness(): ReactElement {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function UploaderHarness(): ReactElement {
|
||||||
|
const [status, setStatus] = useState<'idle' | 'uploading' | 'completed' | 'failed'>('idle')
|
||||||
|
const [errorMessage, setErrorMessage] = useState<string | null>(null)
|
||||||
|
const taskState = useUploadStore((state) => state.tasks['e2e-task-1'])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
;(window as unknown as Record<string, number>)['__SHUMAI_UPLOAD_BASE_DELAY__'] = 100
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
const startUpload = async (file: File) => {
|
||||||
|
setStatus('uploading')
|
||||||
|
setErrorMessage(null)
|
||||||
|
|
||||||
|
const taskId = 'e2e-task-1'
|
||||||
|
const teamId = 'e2e-team-1'
|
||||||
|
const fileId = 'e2e-file-1'
|
||||||
|
|
||||||
|
useUploadStore
|
||||||
|
.getState()
|
||||||
|
.startTask(taskId, 'e2e-task', [{ fileId, name: file.name, size: file.size }])
|
||||||
|
|
||||||
|
try {
|
||||||
|
await uploadFilesWithUppy({
|
||||||
|
files: [{ id: 'temp-1', file }],
|
||||||
|
taskId,
|
||||||
|
teamId,
|
||||||
|
storageBackend: 's3',
|
||||||
|
createdAssets: [{ tempId: 'temp-1', assetId: fileId, key: `files/${file.name}` }],
|
||||||
|
})
|
||||||
|
setStatus('completed')
|
||||||
|
} catch (err) {
|
||||||
|
setStatus('failed')
|
||||||
|
setErrorMessage(err instanceof Error ? err.message : String(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
const file = e.target.files?.[0]
|
||||||
|
if (file) {
|
||||||
|
void startUpload(file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleSimulatedStart = () => {
|
||||||
|
const mockFile = new File(['mock content for test'], 'test-file.mp4', {
|
||||||
|
type: 'video/mp4',
|
||||||
|
})
|
||||||
|
void startUpload(mockFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
const derivedStatus = taskState?.files['e2e-file-1']?.status ?? status
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{ padding: 24, fontFamily: 'sans-serif' }}>
|
||||||
|
<Toaster position="top-right" />
|
||||||
|
<h2>Uploader Resilience E2E Harness</h2>
|
||||||
|
<div style={{ marginBottom: 16 }}>
|
||||||
|
<input data-testid="uploader-file-input" type="file" onChange={handleFileChange} />
|
||||||
|
<button
|
||||||
|
data-testid="start-upload-btn"
|
||||||
|
onClick={handleSimulatedStart}
|
||||||
|
style={{ marginLeft: 8 }}
|
||||||
|
>
|
||||||
|
Start Simulated Upload
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style={{ display: 'flex', gap: 16, alignItems: 'center' }}>
|
||||||
|
<div>
|
||||||
|
Status: <span data-testid="upload-status">{derivedStatus}</span>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
Progress:{' '}
|
||||||
|
<span data-testid="upload-progress">{taskState?.files['e2e-file-1']?.loaded ?? 0}</span>
|
||||||
|
</div>
|
||||||
|
{errorMessage && <div data-testid="upload-error">{errorMessage}</div>}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const rootElement = document.getElementById('root')
|
const rootElement = document.getElementById('root')
|
||||||
if (!rootElement) {
|
if (!rootElement) {
|
||||||
throw new Error('Harness root element not found')
|
throw new Error('Harness root element not found')
|
||||||
}
|
}
|
||||||
|
|
||||||
ReactDOM.createRoot(rootElement).render(<Harness />)
|
const currentView =
|
||||||
|
typeof window !== 'undefined' ? new URLSearchParams(window.location.search).get('view') : null
|
||||||
|
|
||||||
|
ReactDOM.createRoot(rootElement).render(
|
||||||
|
currentView === 'uploader' ? <UploaderHarness /> : <Harness />,
|
||||||
|
)
|
||||||
|
|||||||
@@ -0,0 +1,193 @@
|
|||||||
|
import { expect, test } from '@playwright/test'
|
||||||
|
|
||||||
|
test.describe('Uploader Resilience E2E Tests', () => {
|
||||||
|
test.beforeEach(async ({ page }) => {
|
||||||
|
// Default route mocks for the isolated harness
|
||||||
|
await page.route('**/api/teams/**/upload/tasks/**', (route) =>
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ ok: true }),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('keeps upload active on network drop and automatically resumes on reconnection', async ({
|
||||||
|
page,
|
||||||
|
}) => {
|
||||||
|
let releaseSign: () => void = () => {}
|
||||||
|
const signGate = new Promise<void>((resolve) => {
|
||||||
|
releaseSign = resolve
|
||||||
|
})
|
||||||
|
|
||||||
|
let signCalls = 0
|
||||||
|
await page.route('**/api/teams/**/upload/sign', async (route) => {
|
||||||
|
signCalls++
|
||||||
|
if (signCalls === 1) {
|
||||||
|
// Wait until offline event is dispatched before releasing first sign call
|
||||||
|
await signGate
|
||||||
|
}
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ url: 'http://localhost:5199/mock-upload' }),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
await page.route('**/mock-upload', (route) =>
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
headers: { ['ETag']: '"test-etag"' },
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
await page.goto('/?view=uploader')
|
||||||
|
await expect(page.getByTestId('upload-status')).toHaveText('idle')
|
||||||
|
|
||||||
|
// Start upload
|
||||||
|
await page.getByTestId('start-upload-btn').click()
|
||||||
|
await expect(page.getByTestId('upload-status')).toHaveText('uploading')
|
||||||
|
|
||||||
|
// Simulate cutting the connection
|
||||||
|
await page.context().setOffline(true)
|
||||||
|
releaseSign()
|
||||||
|
|
||||||
|
// Status remains 'uploading' during offline state and no error toast appears
|
||||||
|
await page.waitForTimeout(500)
|
||||||
|
await expect(page.getByTestId('upload-status')).toHaveText('uploading')
|
||||||
|
await expect(page.locator('[data-sonner-toast][data-type="error"]')).toHaveCount(0)
|
||||||
|
|
||||||
|
// Reconnect network
|
||||||
|
await page.context().setOffline(false)
|
||||||
|
|
||||||
|
// Upload automatically resumes and completes
|
||||||
|
await expect(page.getByTestId('upload-status')).toHaveText('completed', { timeout: 15_000 })
|
||||||
|
await expect(page.locator('[data-sonner-toast][data-type="error"]')).toHaveCount(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('retries with backoff on in-flight socket drop and completes when network recovers', async ({
|
||||||
|
page,
|
||||||
|
}) => {
|
||||||
|
let signCalls = 0
|
||||||
|
await page.route('**/api/teams/**/upload/sign', (route) => {
|
||||||
|
signCalls++
|
||||||
|
if (signCalls <= 2) {
|
||||||
|
// In-flight connection reset / socket drop
|
||||||
|
route.abort('connectionreset')
|
||||||
|
} else {
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ url: 'http://localhost:5199/mock-upload' }),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
await page.route('**/mock-upload', (route) =>
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
headers: { ['ETag']: '"test-etag"' },
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
await page.goto('/?view=uploader')
|
||||||
|
await page.getByTestId('start-upload-btn').click()
|
||||||
|
|
||||||
|
await expect(page.getByTestId('upload-status')).toHaveText('uploading')
|
||||||
|
await expect(page.locator('[data-sonner-toast][data-type="error"]')).toHaveCount(0)
|
||||||
|
|
||||||
|
// After backoff retries, succeeds on attempt 3
|
||||||
|
await expect(page.getByTestId('upload-status')).toHaveText('completed', { timeout: 15_000 })
|
||||||
|
expect(signCalls).toBeGreaterThanOrEqual(3)
|
||||||
|
await expect(page.locator('[data-sonner-toast][data-type="error"]')).toHaveCount(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('retries continuously on 502 Bad Gateway and recovers once server returns 200', async ({
|
||||||
|
page,
|
||||||
|
}) => {
|
||||||
|
let signAttempts = 0
|
||||||
|
await page.route('**/api/teams/**/upload/sign', (route) => {
|
||||||
|
signAttempts++
|
||||||
|
if (signAttempts <= 2) {
|
||||||
|
// Return 502 Bad Gateway for the first 2 attempts
|
||||||
|
route.fulfill({
|
||||||
|
status: 502,
|
||||||
|
contentType: 'text/html',
|
||||||
|
body: '<html><body>502 Bad Gateway</body></html>',
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// Server recovers on attempt 3
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ url: 'http://localhost:5199/mock-upload' }),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
await page.route('**/mock-upload', (route) =>
|
||||||
|
route.fulfill({
|
||||||
|
status: 200,
|
||||||
|
headers: { ['ETag']: '"test-etag"' },
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
await page.goto('/?view=uploader')
|
||||||
|
await page.getByTestId('start-upload-btn').click()
|
||||||
|
|
||||||
|
// Verify it stays uploading without failing on 502
|
||||||
|
await expect(page.getByTestId('upload-status')).toHaveText('uploading')
|
||||||
|
await expect(page.locator('[data-sonner-toast][data-type="error"]')).toHaveCount(0)
|
||||||
|
|
||||||
|
// Once server recovers, upload completes
|
||||||
|
await expect(page.getByTestId('upload-status')).toHaveText('completed', { timeout: 15_000 })
|
||||||
|
expect(signAttempts).toBeGreaterThanOrEqual(3)
|
||||||
|
await expect(page.locator('[data-sonner-toast][data-type="error"]')).toHaveCount(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('halts immediately without retrying on 500 Internal Server Error', async ({ page }) => {
|
||||||
|
let signAttempts = 0
|
||||||
|
await page.route('**/api/teams/**/upload/sign', (route) => {
|
||||||
|
signAttempts++
|
||||||
|
route.fulfill({
|
||||||
|
status: 500,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ message: 'Internal Server Error' }),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
await page.goto('/?view=uploader')
|
||||||
|
await page.getByTestId('start-upload-btn').click()
|
||||||
|
|
||||||
|
// Must fail immediately with error toast
|
||||||
|
await expect(page.getByTestId('upload-status')).toHaveText('failed', { timeout: 5_000 })
|
||||||
|
await expect(page.locator('[data-sonner-toast][data-type="error"]')).toBeVisible({
|
||||||
|
timeout: 5_000,
|
||||||
|
})
|
||||||
|
// 0 retries (attempt count must be 1)
|
||||||
|
expect(signAttempts).toBe(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('halts immediately without retrying on 403 Forbidden', async ({ page }) => {
|
||||||
|
let signAttempts = 0
|
||||||
|
await page.route('**/api/teams/**/upload/sign', (route) => {
|
||||||
|
signAttempts++
|
||||||
|
route.fulfill({
|
||||||
|
status: 403,
|
||||||
|
contentType: 'application/json',
|
||||||
|
body: JSON.stringify({ message: 'Forbidden' }),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
await page.goto('/?view=uploader')
|
||||||
|
await page.getByTestId('start-upload-btn').click()
|
||||||
|
|
||||||
|
// Must fail immediately with error toast
|
||||||
|
await expect(page.getByTestId('upload-status')).toHaveText('failed', { timeout: 5_000 })
|
||||||
|
await expect(page.locator('[data-sonner-toast][data-type="error"]')).toBeVisible({
|
||||||
|
timeout: 5_000,
|
||||||
|
})
|
||||||
|
// 0 retries (attempt count must be 1)
|
||||||
|
expect(signAttempts).toBe(1)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,158 @@
|
|||||||
|
// @vitest-environment happy-dom
|
||||||
|
import { renderHook, waitFor } from '@testing-library/react'
|
||||||
|
import React from 'react'
|
||||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||||
|
import type { AssetInfo } from '@shumai/dtos'
|
||||||
|
import { useBatchFileStatus } from './use-batch-file-status'
|
||||||
|
import { client } from '@/ui/api/client'
|
||||||
|
import { useUploadStore } from '@/ui/stores/upload'
|
||||||
|
|
||||||
|
vi.mock('@/ui/api/client', () => ({
|
||||||
|
client: {
|
||||||
|
api: {
|
||||||
|
files: {
|
||||||
|
batch: {
|
||||||
|
$post: vi.fn(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
|
describe('useBatchFileStatus', () => {
|
||||||
|
let queryClient: QueryClient
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
useUploadStore.setState({ fileProgress: {} })
|
||||||
|
queryClient = new QueryClient({
|
||||||
|
defaultOptions: {
|
||||||
|
queries: { retry: false },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
vi.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.restoreAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
|
const wrapper = ({ children }: { children: React.ReactNode }) =>
|
||||||
|
React.createElement(QueryClientProvider, { client: queryClient }, children)
|
||||||
|
|
||||||
|
it('does not poll when items array is empty or no files are processing', () => {
|
||||||
|
const items: AssetInfo[] = [
|
||||||
|
{ id: 'f1', type: 'file', status: 'processed' } as AssetInfo,
|
||||||
|
{ id: 'f2', type: 'file', status: 'error' } as AssetInfo,
|
||||||
|
]
|
||||||
|
|
||||||
|
renderHook(() => useBatchFileStatus({ teamId: 'team-1', items }), { wrapper })
|
||||||
|
|
||||||
|
expect(client.api.files.batch.$post).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does NOT poll when files are in "uploading" status and upload is in progress', () => {
|
||||||
|
const items: AssetInfo[] = [
|
||||||
|
{ id: 'f1', type: 'file', status: 'uploading' } as AssetInfo,
|
||||||
|
{ id: 'f2', type: 'file', status: 'uploading' } as AssetInfo,
|
||||||
|
]
|
||||||
|
|
||||||
|
renderHook(() => useBatchFileStatus({ teamId: 'team-1', items }), { wrapper })
|
||||||
|
|
||||||
|
expect(client.api.files.batch.$post).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('polls batch API when file has status "uploading" but upload is completed in store', async () => {
|
||||||
|
useUploadStore.setState({
|
||||||
|
fileProgress: {
|
||||||
|
f1: { fileId: 'f1', name: 'f1.mp4', loaded: 1000, total: 1000, status: 'completed' },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const items: AssetInfo[] = [
|
||||||
|
{ id: 'f1', type: 'file', status: 'uploading' } as AssetInfo,
|
||||||
|
{ id: 'f2', type: 'file', status: 'uploading' } as AssetInfo,
|
||||||
|
]
|
||||||
|
|
||||||
|
const updatedAssets: AssetInfo[] = [
|
||||||
|
{ id: 'f1', type: 'file', status: 'uploaded', name: 'f1.mp4' } as AssetInfo,
|
||||||
|
]
|
||||||
|
|
||||||
|
const invalidateSpy = vi.spyOn(queryClient, 'invalidateQueries')
|
||||||
|
|
||||||
|
vi.mocked(client.api.files.batch.$post).mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
json: async () => updatedAssets,
|
||||||
|
} as unknown as Awaited<ReturnType<typeof client.api.files.batch.$post>>)
|
||||||
|
|
||||||
|
renderHook(() => useBatchFileStatus({ teamId: 'team-1', items }), { wrapper })
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(client.api.files.batch.$post).toHaveBeenCalledWith({
|
||||||
|
json: { ids: ['f1'] },
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(queryClient.getQueryData(['file', 'team-1', 'f1'])).toEqual(updatedAssets[0])
|
||||||
|
// Should NOT invalidate search query for intermediate 'uploaded' state; card updates via setQueryData
|
||||||
|
expect(invalidateSpy).not.toHaveBeenCalledWith({ queryKey: ['search', 'team-1'] })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('polls batch API when files are in "uploaded" or "processing" status and updates cache', async () => {
|
||||||
|
const items: AssetInfo[] = [
|
||||||
|
{ id: 'f1', type: 'file', status: 'uploaded' } as AssetInfo,
|
||||||
|
{ id: 'f2', type: 'file', status: 'processing' } as AssetInfo,
|
||||||
|
{ id: 'f3', type: 'file', status: 'uploading' } as AssetInfo,
|
||||||
|
]
|
||||||
|
|
||||||
|
const updatedAssets: AssetInfo[] = [
|
||||||
|
{ id: 'f1', type: 'file', status: 'processing', name: 'f1.mp4' } as AssetInfo,
|
||||||
|
{ id: 'f2', type: 'file', status: 'processed', name: 'f2.mp4' } as AssetInfo,
|
||||||
|
]
|
||||||
|
|
||||||
|
const invalidateSpy = vi.spyOn(queryClient, 'invalidateQueries')
|
||||||
|
|
||||||
|
vi.mocked(client.api.files.batch.$post).mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
json: async () => updatedAssets,
|
||||||
|
} as unknown as Awaited<ReturnType<typeof client.api.files.batch.$post>>)
|
||||||
|
|
||||||
|
renderHook(() => useBatchFileStatus({ teamId: 'team-1', items }), { wrapper })
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(client.api.files.batch.$post).toHaveBeenCalledWith({
|
||||||
|
json: { ids: ['f1', 'f2'] },
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// Should update per-file cache in TanStack Query
|
||||||
|
expect(queryClient.getQueryData(['file', 'team-1', 'f1'])).toEqual(updatedAssets[0])
|
||||||
|
expect(queryClient.getQueryData(['file', 'team-1', 'f2'])).toEqual(updatedAssets[1])
|
||||||
|
|
||||||
|
// Should invalidate folder search query because f2 finished ('processed')
|
||||||
|
expect(invalidateSpy).toHaveBeenCalledWith({ queryKey: ['search', 'team-1'] })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('chunks requests when processing items exceed 100', async () => {
|
||||||
|
const items: AssetInfo[] = Array.from({ length: 150 }, (_, i) => ({
|
||||||
|
id: `file-${i}`,
|
||||||
|
type: 'file',
|
||||||
|
status: 'processing',
|
||||||
|
})) as AssetInfo[]
|
||||||
|
|
||||||
|
vi.mocked(client.api.files.batch.$post).mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
json: async () => [],
|
||||||
|
} as unknown as Awaited<ReturnType<typeof client.api.files.batch.$post>>)
|
||||||
|
|
||||||
|
renderHook(() => useBatchFileStatus({ teamId: 'team-1', items }), { wrapper })
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(client.api.files.batch.$post).toHaveBeenCalledTimes(2)
|
||||||
|
})
|
||||||
|
|
||||||
|
const calls = vi.mocked(client.api.files.batch.$post).mock.calls
|
||||||
|
expect(calls[0][0].json.ids).toHaveLength(100)
|
||||||
|
expect(calls[1][0].json.ids).toHaveLength(50)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
||||||
|
import { useMemo, useRef } from 'react'
|
||||||
|
import type { AssetInfo } from '@shumai/dtos'
|
||||||
|
import { client } from '@/ui/api/client'
|
||||||
|
import { useUploadStore } from '@/ui/stores/upload'
|
||||||
|
|
||||||
|
export const BATCH_SIZE = 100
|
||||||
|
export const BATCH_POLL_INTERVAL_MS = 2000
|
||||||
|
export const BATCH_SEARCH_INVALIDATE_THROTTLE_MS = 5000
|
||||||
|
|
||||||
|
export interface UseBatchFileStatusOptions {
|
||||||
|
teamId?: string
|
||||||
|
items: AssetInfo[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useBatchFileStatus({ teamId, items }: UseBatchFileStatusOptions): void {
|
||||||
|
const queryClient = useQueryClient()
|
||||||
|
const fileProgress = useUploadStore((state) => state.fileProgress)
|
||||||
|
const lastInvalidateTimeRef = useRef(0)
|
||||||
|
|
||||||
|
const processingIds = useMemo(() => {
|
||||||
|
if (!teamId) return []
|
||||||
|
return items
|
||||||
|
.filter((item) => {
|
||||||
|
if (!item.id || (item.type !== 'file' && item.type !== 'version_stack')) return false
|
||||||
|
const isUploadedOrProcessing = item.status === 'uploaded' || item.status === 'processing'
|
||||||
|
const progress = fileProgress[item.id]
|
||||||
|
const isUploadCompleted =
|
||||||
|
item.status === 'uploading' &&
|
||||||
|
(progress?.status === 'completed' ||
|
||||||
|
(progress?.total != null && progress.total > 0 && progress.loaded >= progress.total))
|
||||||
|
return isUploadedOrProcessing || isUploadCompleted
|
||||||
|
})
|
||||||
|
.map((item) => item.id!)
|
||||||
|
}, [items, teamId, fileProgress])
|
||||||
|
|
||||||
|
const processingIdsRef = useRef(processingIds)
|
||||||
|
processingIdsRef.current = processingIds
|
||||||
|
|
||||||
|
const queryKey = useMemo(() => {
|
||||||
|
return ['files', 'batch', teamId]
|
||||||
|
}, [teamId])
|
||||||
|
|
||||||
|
useQuery({
|
||||||
|
queryKey,
|
||||||
|
queryFn: async () => {
|
||||||
|
const currentIds = processingIdsRef.current
|
||||||
|
if (currentIds.length === 0) return []
|
||||||
|
|
||||||
|
const chunks: string[][] = []
|
||||||
|
for (let i = 0; i < currentIds.length; i += BATCH_SIZE) {
|
||||||
|
chunks.push(currentIds.slice(i, i + BATCH_SIZE))
|
||||||
|
}
|
||||||
|
|
||||||
|
const results = await Promise.all(
|
||||||
|
chunks.map(async (chunkIds) => {
|
||||||
|
const res = await client.api.files.batch.$post({
|
||||||
|
json: { ids: chunkIds },
|
||||||
|
})
|
||||||
|
if (!res.ok) throw new Error('Failed to fetch batch file statuses')
|
||||||
|
return (await res.json()) as unknown as AssetInfo[]
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
const assets = results.flat()
|
||||||
|
|
||||||
|
for (const asset of assets) {
|
||||||
|
if (asset.id) {
|
||||||
|
queryClient.setQueryData(['file', teamId, asset.id], asset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const anyFinished = assets.some(
|
||||||
|
(a) => a.status === 'processed' || a.status === 'failed' || a.status === 'error',
|
||||||
|
)
|
||||||
|
const allFinished =
|
||||||
|
assets.length > 0 &&
|
||||||
|
assets.every(
|
||||||
|
(a) => a.status === 'processed' || a.status === 'failed' || a.status === 'error',
|
||||||
|
)
|
||||||
|
|
||||||
|
const now = Date.now()
|
||||||
|
if (
|
||||||
|
allFinished ||
|
||||||
|
(anyFinished && now - lastInvalidateTimeRef.current >= BATCH_SEARCH_INVALIDATE_THROTTLE_MS)
|
||||||
|
) {
|
||||||
|
lastInvalidateTimeRef.current = now
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['search', teamId] })
|
||||||
|
}
|
||||||
|
|
||||||
|
return assets
|
||||||
|
},
|
||||||
|
enabled: !!teamId && processingIds.length > 0,
|
||||||
|
refetchInterval: BATCH_POLL_INTERVAL_MS,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,8 +1,20 @@
|
|||||||
// @vitest-environment happy-dom
|
// @vitest-environment happy-dom
|
||||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||||
import { activeUploads, uploadFilesWithUppy, isNetworkIssue } from './uploader'
|
import {
|
||||||
|
activeUploads,
|
||||||
|
cancelActiveUploadTask,
|
||||||
|
uploadFilesWithUppy,
|
||||||
|
isNetworkIssue,
|
||||||
|
isLocalhostStorageMismatch,
|
||||||
|
calculateBackoffDelay,
|
||||||
|
MAX_RETRY_DELAY_MS,
|
||||||
|
BASE_RETRY_DELAY_MS,
|
||||||
|
UploadNetworkError,
|
||||||
|
UploadHttpError,
|
||||||
|
} from './uploader'
|
||||||
import { acquireWakeLock, releaseWakeLock } from './wake-lock'
|
import { acquireWakeLock, releaseWakeLock } from './wake-lock'
|
||||||
import { useUploadStore } from '@/ui/stores/upload'
|
import { useUploadStore } from '@/ui/stores/upload'
|
||||||
|
import { m } from '@/ui/paraglide/messages.js'
|
||||||
|
|
||||||
vi.mock('sonner', () => ({
|
vi.mock('sonner', () => ({
|
||||||
toast: {
|
toast: {
|
||||||
@@ -37,6 +49,16 @@ import { client } from '@/ui/api/client'
|
|||||||
import { toast } from 'sonner'
|
import { toast } from 'sonner'
|
||||||
|
|
||||||
describe('Uploader', () => {
|
describe('Uploader', () => {
|
||||||
|
const initialLocation = window.location
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
Object.defineProperty(window, 'location', {
|
||||||
|
value: initialLocation,
|
||||||
|
configurable: true,
|
||||||
|
writable: true,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
activeUploads.clear()
|
activeUploads.clear()
|
||||||
@@ -187,9 +209,59 @@ describe('Uploader', () => {
|
|||||||
Object.defineProperty(navigator, 'onLine', { value: false, configurable: true })
|
Object.defineProperty(navigator, 'onLine', { value: false, configurable: true })
|
||||||
expect(isNetworkIssue(new Error('Any error'))).toBe(true)
|
expect(isNetworkIssue(new Error('Any error'))).toBe(true)
|
||||||
expect(isNetworkIssue()).toBe(true)
|
expect(isNetworkIssue()).toBe(true)
|
||||||
|
// Non-retryable statuses must return false even while offline
|
||||||
|
expect(
|
||||||
|
isNetworkIssue(
|
||||||
|
new UploadHttpError('Internal Server Error', { status: 500, isTransient: false }),
|
||||||
|
),
|
||||||
|
).toBe(false)
|
||||||
|
expect(
|
||||||
|
isNetworkIssue(new UploadHttpError('Forbidden', { status: 403, isTransient: false })),
|
||||||
|
).toBe(false)
|
||||||
|
expect(isNetworkIssue({ status: 500 })).toBe(false)
|
||||||
|
expect(isNetworkIssue({ status: 403 })).toBe(false)
|
||||||
|
expect(isNetworkIssue({ request: { status: 500 } })).toBe(false)
|
||||||
|
expect(isNetworkIssue({ request: { status: 403 } })).toBe(false)
|
||||||
|
expect(isNetworkIssue('upload failed with status: 500')).toBe(false)
|
||||||
|
expect(isNetworkIssue('upload failed with status: 403')).toBe(false)
|
||||||
|
// Transient statuses while offline return true
|
||||||
|
expect(
|
||||||
|
isNetworkIssue(new UploadHttpError('Bad Gateway', { status: 502, isTransient: true })),
|
||||||
|
).toBe(true)
|
||||||
|
expect(isNetworkIssue({ status: 502 })).toBe(true)
|
||||||
|
|
||||||
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
|
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
|
||||||
expect(isNetworkIssue(new Error('Failed to fetch'))).toBe(true)
|
expect(isNetworkIssue(new Error('Failed to fetch'))).toBe(true)
|
||||||
|
expect(isNetworkIssue(new TypeError('Failed to fetch'))).toBe(true)
|
||||||
|
expect(isNetworkIssue('Failed to fetch')).toBe(true)
|
||||||
|
expect(isNetworkIssue('net::ERR_NETWORK_CHANGED')).toBe(true)
|
||||||
|
expect(isNetworkIssue(new UploadNetworkError('Network error while signing request'))).toBe(
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
expect(
|
||||||
|
isNetworkIssue(new UploadHttpError('Bad Gateway', { status: 502, isTransient: true })),
|
||||||
|
).toBe(true)
|
||||||
|
expect(
|
||||||
|
isNetworkIssue(
|
||||||
|
new UploadHttpError('Service Unavailable', { status: 503, isTransient: true }),
|
||||||
|
),
|
||||||
|
).toBe(true)
|
||||||
|
expect(
|
||||||
|
isNetworkIssue(new UploadHttpError('Gateway Timeout', { status: 504, isTransient: true })),
|
||||||
|
).toBe(true)
|
||||||
|
expect(
|
||||||
|
isNetworkIssue(new UploadHttpError('Rate Limited', { status: 429, isTransient: true })),
|
||||||
|
).toBe(true)
|
||||||
|
expect(
|
||||||
|
isNetworkIssue(
|
||||||
|
new UploadHttpError('Internal Server Error', { status: 500, isTransient: false }),
|
||||||
|
),
|
||||||
|
).toBe(false)
|
||||||
|
expect(
|
||||||
|
isNetworkIssue(new UploadHttpError('Forbidden', { status: 403, isTransient: false })),
|
||||||
|
).toBe(false)
|
||||||
|
expect(isNetworkIssue({ status: 502 })).toBe(true)
|
||||||
|
expect(isNetworkIssue({ status: 500 })).toBe(false)
|
||||||
expect(isNetworkIssue(new Error('NetworkError when attempting to fetch resource.'))).toBe(
|
expect(isNetworkIssue(new Error('NetworkError when attempting to fetch resource.'))).toBe(
|
||||||
true,
|
true,
|
||||||
)
|
)
|
||||||
@@ -206,6 +278,81 @@ describe('Uploader', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('isLocalhostStorageMismatch', () => {
|
||||||
|
it('detects mismatch when upload URL is localhost and window location is remote', () => {
|
||||||
|
// In happy-dom window.location defaults to localhost:3000
|
||||||
|
Object.defineProperty(window, 'location', {
|
||||||
|
value: {
|
||||||
|
hostname: '192.168.1.100',
|
||||||
|
origin: 'http://192.168.1.100:3000',
|
||||||
|
},
|
||||||
|
configurable: true,
|
||||||
|
writable: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(isLocalhostStorageMismatch('http://localhost:3000/files/shumai/files/1.png')).toBe(
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
expect(isLocalhostStorageMismatch('http://127.0.0.1:3000/files/shumai/files/1.png')).toBe(
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
expect(isLocalhostStorageMismatch('http://[::1]:3000/files/shumai/files/1.png')).toBe(true)
|
||||||
|
expect(isLocalhostStorageMismatch('http://0.0.0.0:3000/files/shumai/files/1.png')).toBe(true)
|
||||||
|
|
||||||
|
// Remote upload URL with remote window -> no mismatch
|
||||||
|
expect(isLocalhostStorageMismatch('http://192.168.1.100:3000/files/shumai/files/1.png')).toBe(
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
expect(isLocalhostStorageMismatch('https://s3.amazonaws.com/bucket/files/1.png')).toBe(false)
|
||||||
|
expect(isLocalhostStorageMismatch('/files/shumai/files/1.png')).toBe(false)
|
||||||
|
expect(isLocalhostStorageMismatch(undefined)).toBe(false)
|
||||||
|
expect(isLocalhostStorageMismatch('')).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns false when window location itself is localhost', () => {
|
||||||
|
Object.defineProperty(window, 'location', {
|
||||||
|
value: {
|
||||||
|
hostname: 'localhost',
|
||||||
|
origin: 'http://localhost:3000',
|
||||||
|
},
|
||||||
|
configurable: true,
|
||||||
|
writable: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(isLocalhostStorageMismatch('http://localhost:3000/files/shumai/files/1.png')).toBe(
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
expect(isLocalhostStorageMismatch('http://127.0.0.1:3000/files/shumai/files/1.png')).toBe(
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('calculateBackoffDelay', () => {
|
||||||
|
it('increases delay with attempts and caps at MAX_RETRY_DELAY_MS (60s)', () => {
|
||||||
|
const delay1 = calculateBackoffDelay(1)
|
||||||
|
expect(delay1).toBeGreaterThanOrEqual(1700)
|
||||||
|
expect(delay1).toBeLessThanOrEqual(2300)
|
||||||
|
|
||||||
|
const delay2 = calculateBackoffDelay(2)
|
||||||
|
expect(delay2).toBeGreaterThanOrEqual(3400)
|
||||||
|
expect(delay2).toBeLessThanOrEqual(4600)
|
||||||
|
|
||||||
|
const delay3 = calculateBackoffDelay(3)
|
||||||
|
expect(delay3).toBeGreaterThanOrEqual(6800)
|
||||||
|
expect(delay3).toBeLessThanOrEqual(9200)
|
||||||
|
|
||||||
|
const delay10 = calculateBackoffDelay(10)
|
||||||
|
expect(delay10).toBeGreaterThanOrEqual(Math.round(MAX_RETRY_DELAY_MS * 0.85))
|
||||||
|
expect(delay10).toBeLessThanOrEqual(MAX_RETRY_DELAY_MS)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('never returns less than baseDelayMs', () => {
|
||||||
|
const delay0 = calculateBackoffDelay(0)
|
||||||
|
expect(delay0).toBeGreaterThanOrEqual(BASE_RETRY_DELAY_MS)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it('pauses on network disconnect, does not report terminal failure, and resumes on back-online', async () => {
|
it('pauses on network disconnect, does not report terminal failure, and resumes on back-online', async () => {
|
||||||
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
|
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
|
||||||
useUploadStore
|
useUploadStore
|
||||||
@@ -315,9 +462,428 @@ describe('Uploader', () => {
|
|||||||
param: { teamId: 'team-1', taskId: 'task-1' },
|
param: { teamId: 'team-1', taskId: 'task-1' },
|
||||||
json: { fileId: 'file-1', errorMessage: '403 Forbidden' },
|
json: { fileId: 'file-1', errorMessage: '403 Forbidden' },
|
||||||
})
|
})
|
||||||
|
expect(toast.error).toHaveBeenCalledWith(m.failed_upload_file_with_name({ name: 'test.mp4' }))
|
||||||
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('failed')
|
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('failed')
|
||||||
expect(activeUploads.size).toBe(0)
|
expect(activeUploads.size).toBe(0)
|
||||||
|
|
||||||
addFileSpy.mockRestore()
|
addFileSpy.mockRestore()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('shows S3 endpoint localhost error toast without retrying when remote user encounters localhost upload URL', async () => {
|
||||||
|
Object.defineProperty(window, 'location', {
|
||||||
|
value: {
|
||||||
|
hostname: '192.168.1.100',
|
||||||
|
origin: 'http://192.168.1.100:3000',
|
||||||
|
},
|
||||||
|
configurable: true,
|
||||||
|
writable: true,
|
||||||
|
})
|
||||||
|
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
|
||||||
|
|
||||||
|
useUploadStore
|
||||||
|
.getState()
|
||||||
|
.startTask('task-1', 'task-1', [{ fileId: 'file-1', name: 'remote.mp4', size: 100 }])
|
||||||
|
|
||||||
|
const addFileSpy = vi.spyOn(Uppy.prototype, 'addFile').mockReturnValue('f1')
|
||||||
|
const retryAllSpy = vi.spyOn(Uppy.prototype, 'retryAll')
|
||||||
|
|
||||||
|
const uploadPromise = uploadFilesWithUppy({
|
||||||
|
files: [{ id: 'temp-1', file: new File(['content'], 'remote.mp4', { type: 'video/mp4' }) }],
|
||||||
|
taskId: 'task-1',
|
||||||
|
teamId: 'team-1',
|
||||||
|
storageBackend: 'local',
|
||||||
|
createdAssets: [{ tempId: 'temp-1', assetId: 'file-1', key: 'files/remote.mp4' }],
|
||||||
|
presignedUrls: [
|
||||||
|
{
|
||||||
|
id: 'temp-1',
|
||||||
|
fileId: 'file-1',
|
||||||
|
url: 'http://localhost:3000/files/shumai/files/remote.mp4',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
await new Promise((r) => setTimeout(r, 10))
|
||||||
|
const capturedUppy = addFileSpy.mock.contexts[0] as Uppy
|
||||||
|
expect(capturedUppy).toBeDefined()
|
||||||
|
|
||||||
|
const mockFile = {
|
||||||
|
id: 'f1',
|
||||||
|
name: 'remote.mp4',
|
||||||
|
meta: {
|
||||||
|
fileId: 'file-1',
|
||||||
|
url: 'http://localhost:3000/files/shumai/files/remote.mp4',
|
||||||
|
},
|
||||||
|
} as unknown as UppyFile<Meta, Record<string, never>>
|
||||||
|
|
||||||
|
const networkError = new Error('Failed to fetch')
|
||||||
|
capturedUppy.emit('upload-error', mockFile, networkError)
|
||||||
|
capturedUppy.emit('complete', {
|
||||||
|
successful: [],
|
||||||
|
failed: [{ ...mockFile, error: networkError.message }],
|
||||||
|
})
|
||||||
|
|
||||||
|
await uploadPromise
|
||||||
|
|
||||||
|
// Should NOT retry when localhost mismatch is detected
|
||||||
|
expect(retryAllSpy).not.toHaveBeenCalled()
|
||||||
|
|
||||||
|
// Must show specific AWS_ENDPOINT_URL_S3 toast
|
||||||
|
expect(toast.error).toHaveBeenCalledWith(m.upload_failed_s3_endpoint_localhost(), {
|
||||||
|
id: 's3-endpoint-localhost-mismatch',
|
||||||
|
duration: 10000,
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(client.api.teams[':teamId'].upload.tasks[':taskId'].$patch).toHaveBeenCalledWith({
|
||||||
|
param: { teamId: 'team-1', taskId: 'task-1' },
|
||||||
|
json: { fileId: 'file-1', errorMessage: 'Failed to fetch' },
|
||||||
|
})
|
||||||
|
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('failed')
|
||||||
|
expect(activeUploads.size).toBe(0)
|
||||||
|
|
||||||
|
addFileSpy.mockRestore()
|
||||||
|
retryAllSpy.mockRestore()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('retries continuously on transient network errors while online without capping at 3 attempts', async () => {
|
||||||
|
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
|
||||||
|
useUploadStore
|
||||||
|
.getState()
|
||||||
|
.startTask('task-1', 'task-1', [{ fileId: 'file-1', name: 'test.mp4', size: 100 }])
|
||||||
|
|
||||||
|
const addFileSpy = vi.spyOn(Uppy.prototype, 'addFile').mockReturnValue('f1')
|
||||||
|
const retryAllSpy = vi.spyOn(Uppy.prototype, 'retryAll').mockResolvedValue(undefined as never)
|
||||||
|
|
||||||
|
const uploadPromise = uploadFilesWithUppy({
|
||||||
|
files: [{ id: 'temp-1', file: new File(['content'], 'test.mp4', { type: 'video/mp4' }) }],
|
||||||
|
taskId: 'task-1',
|
||||||
|
teamId: 'team-1',
|
||||||
|
storageBackend: 's3',
|
||||||
|
createdAssets: [{ tempId: 'temp-1', assetId: 'file-1', key: 'files/test.mp4' }],
|
||||||
|
})
|
||||||
|
|
||||||
|
await new Promise((r) => setTimeout(r, 10))
|
||||||
|
const capturedUppy = addFileSpy.mock.contexts[0] as Uppy
|
||||||
|
expect(capturedUppy).toBeDefined()
|
||||||
|
|
||||||
|
const mockFile = {
|
||||||
|
id: 'f1',
|
||||||
|
name: 'test.mp4',
|
||||||
|
meta: { fileId: 'file-1' },
|
||||||
|
} as unknown as UppyFile<Meta, Record<string, never>>
|
||||||
|
|
||||||
|
const networkError = new Error('Failed to fetch')
|
||||||
|
|
||||||
|
// Simulate 5 consecutive network errors while online (previously would have aborted after 3)
|
||||||
|
for (let i = 1; i <= 5; i++) {
|
||||||
|
capturedUppy.emit('upload-error', mockFile, networkError)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should still be active and uploading, NOT permanently failed
|
||||||
|
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('uploading')
|
||||||
|
expect(activeUploads.has('file-1')).toBe(true)
|
||||||
|
expect(client.api.teams[':teamId'].upload.tasks[':taskId'].$patch).not.toHaveBeenCalled()
|
||||||
|
|
||||||
|
// Now succeed on retry
|
||||||
|
capturedUppy.emit('upload-success', mockFile, { status: 200 })
|
||||||
|
capturedUppy.emit('complete', { successful: [mockFile], failed: [] })
|
||||||
|
|
||||||
|
await uploadPromise
|
||||||
|
|
||||||
|
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('completed')
|
||||||
|
expect(activeUploads.size).toBe(0)
|
||||||
|
|
||||||
|
addFileSpy.mockRestore()
|
||||||
|
retryAllSpy.mockRestore()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('halts retry loop and fails permanently when S3 returns 403 (presigned URL expired)', async () => {
|
||||||
|
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
|
||||||
|
useUploadStore
|
||||||
|
.getState()
|
||||||
|
.startTask('task-1', 'task-1', [{ fileId: 'file-1', name: 'expired.mp4', size: 100 }])
|
||||||
|
|
||||||
|
const addFileSpy = vi.spyOn(Uppy.prototype, 'addFile').mockReturnValue('f1')
|
||||||
|
|
||||||
|
const uploadPromise = uploadFilesWithUppy({
|
||||||
|
files: [{ id: 'temp-1', file: new File(['content'], 'expired.mp4', { type: 'video/mp4' }) }],
|
||||||
|
taskId: 'task-1',
|
||||||
|
teamId: 'team-1',
|
||||||
|
storageBackend: 's3',
|
||||||
|
createdAssets: [{ tempId: 'temp-1', assetId: 'file-1', key: 'files/expired.mp4' }],
|
||||||
|
})
|
||||||
|
|
||||||
|
await new Promise((r) => setTimeout(r, 10))
|
||||||
|
const capturedUppy = addFileSpy.mock.contexts[0] as Uppy
|
||||||
|
|
||||||
|
const mockFile = {
|
||||||
|
id: 'f1',
|
||||||
|
name: 'expired.mp4',
|
||||||
|
meta: { fileId: 'file-1' },
|
||||||
|
} as unknown as UppyFile<Meta, Record<string, never>>
|
||||||
|
|
||||||
|
// S3 returns 403 when presigned URL expires after 12h
|
||||||
|
const expiredError = Object.assign(new Error('Request has expired'), { status: 403 })
|
||||||
|
capturedUppy.emit('upload-error', mockFile, expiredError)
|
||||||
|
capturedUppy.emit('complete', {
|
||||||
|
successful: [],
|
||||||
|
failed: [{ ...mockFile, error: 'Request has expired' }],
|
||||||
|
})
|
||||||
|
|
||||||
|
await uploadPromise
|
||||||
|
|
||||||
|
expect(client.api.teams[':teamId'].upload.tasks[':taskId'].$patch).toHaveBeenCalledWith({
|
||||||
|
param: { teamId: 'team-1', taskId: 'task-1' },
|
||||||
|
json: { fileId: 'file-1', errorMessage: 'Request has expired' },
|
||||||
|
})
|
||||||
|
expect(toast.error).toHaveBeenCalledWith(
|
||||||
|
m.failed_upload_file_with_name({ name: 'expired.mp4' }),
|
||||||
|
)
|
||||||
|
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('failed')
|
||||||
|
expect(activeUploads.size).toBe(0)
|
||||||
|
|
||||||
|
addFileSpy.mockRestore()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('keeps upload active and retries when socket drops with TypeError: Failed to fetch while navigator.onLine is still true', async () => {
|
||||||
|
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
|
||||||
|
useUploadStore
|
||||||
|
.getState()
|
||||||
|
.startTask('task-1', 'task-1', [{ fileId: 'file-1', name: 'socket-drop.mp4', size: 100 }])
|
||||||
|
|
||||||
|
const addFileSpy = vi.spyOn(Uppy.prototype, 'addFile').mockReturnValue('f1')
|
||||||
|
const retryAllSpy = vi.spyOn(Uppy.prototype, 'retryAll').mockResolvedValue(undefined as never)
|
||||||
|
|
||||||
|
const uploadPromise = uploadFilesWithUppy({
|
||||||
|
files: [
|
||||||
|
{ id: 'temp-1', file: new File(['content'], 'socket-drop.mp4', { type: 'video/mp4' }) },
|
||||||
|
],
|
||||||
|
taskId: 'task-1',
|
||||||
|
teamId: 'team-1',
|
||||||
|
storageBackend: 's3',
|
||||||
|
createdAssets: [{ tempId: 'temp-1', assetId: 'file-1', key: 'files/socket-drop.mp4' }],
|
||||||
|
})
|
||||||
|
|
||||||
|
await new Promise((r) => setTimeout(r, 10))
|
||||||
|
const capturedUppy = addFileSpy.mock.contexts[0] as Uppy
|
||||||
|
const mockFile = {
|
||||||
|
id: 'f1',
|
||||||
|
name: 'socket-drop.mp4',
|
||||||
|
meta: { fileId: 'file-1' },
|
||||||
|
} as unknown as UppyFile<Meta, Record<string, never>>
|
||||||
|
|
||||||
|
// Socket drops: navigator.onLine is STILL TRUE at this exact moment
|
||||||
|
const fetchError = new TypeError('Failed to fetch')
|
||||||
|
capturedUppy.emit('upload-error', mockFile, fetchError)
|
||||||
|
// Uppy emits complete with stripped error string
|
||||||
|
capturedUppy.emit('complete', {
|
||||||
|
successful: [],
|
||||||
|
failed: [{ ...mockFile, error: 'Failed to fetch' }],
|
||||||
|
})
|
||||||
|
|
||||||
|
await new Promise((r) => setTimeout(r, 20))
|
||||||
|
|
||||||
|
// Must NOT be marked failed, activeUploads must retain file, no error toast
|
||||||
|
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('uploading')
|
||||||
|
expect(activeUploads.has('file-1')).toBe(true)
|
||||||
|
expect(toast.error).not.toHaveBeenCalled()
|
||||||
|
expect(client.api.teams[':teamId'].upload.tasks[':taskId'].$patch).not.toHaveBeenCalled()
|
||||||
|
|
||||||
|
// Simulate recovery on retry
|
||||||
|
capturedUppy.emit('upload-success', mockFile, { status: 200 })
|
||||||
|
capturedUppy.emit('complete', { successful: [mockFile], failed: [] })
|
||||||
|
|
||||||
|
await uploadPromise
|
||||||
|
|
||||||
|
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('completed')
|
||||||
|
expect(activeUploads.size).toBe(0)
|
||||||
|
|
||||||
|
addFileSpy.mockRestore()
|
||||||
|
retryAllSpy.mockRestore()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('retries continuously on 502 Bad Gateway and recovers on subsequent attempt', async () => {
|
||||||
|
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
|
||||||
|
useUploadStore
|
||||||
|
.getState()
|
||||||
|
.startTask('task-1', 'task-1', [{ fileId: 'file-1', name: 'gateway.mp4', size: 100 }])
|
||||||
|
|
||||||
|
const addFileSpy = vi.spyOn(Uppy.prototype, 'addFile').mockReturnValue('f1')
|
||||||
|
const retryAllSpy = vi.spyOn(Uppy.prototype, 'retryAll').mockResolvedValue(undefined as never)
|
||||||
|
|
||||||
|
const uploadPromise = uploadFilesWithUppy({
|
||||||
|
files: [{ id: 'temp-1', file: new File(['content'], 'gateway.mp4', { type: 'video/mp4' }) }],
|
||||||
|
taskId: 'task-1',
|
||||||
|
teamId: 'team-1',
|
||||||
|
storageBackend: 's3',
|
||||||
|
createdAssets: [{ tempId: 'temp-1', assetId: 'file-1', key: 'files/gateway.mp4' }],
|
||||||
|
})
|
||||||
|
|
||||||
|
await new Promise((r) => setTimeout(r, 10))
|
||||||
|
const capturedUppy = addFileSpy.mock.contexts[0] as Uppy
|
||||||
|
const mockFile = {
|
||||||
|
id: 'f1',
|
||||||
|
name: 'gateway.mp4',
|
||||||
|
meta: { fileId: 'file-1' },
|
||||||
|
} as unknown as UppyFile<Meta, Record<string, never>>
|
||||||
|
|
||||||
|
const gatewayError = new UploadHttpError('Failed to sign request: Bad Gateway', {
|
||||||
|
status: 502,
|
||||||
|
isTransient: true,
|
||||||
|
})
|
||||||
|
capturedUppy.emit('upload-error', mockFile, gatewayError)
|
||||||
|
capturedUppy.emit('complete', {
|
||||||
|
successful: [],
|
||||||
|
failed: [{ ...mockFile, error: 'Failed to sign request: Bad Gateway' }],
|
||||||
|
})
|
||||||
|
|
||||||
|
await new Promise((r) => setTimeout(r, 20))
|
||||||
|
|
||||||
|
// Must NOT be marked failed, activeUploads must retain file, no error toast
|
||||||
|
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('uploading')
|
||||||
|
expect(activeUploads.has('file-1')).toBe(true)
|
||||||
|
expect(toast.error).not.toHaveBeenCalled()
|
||||||
|
|
||||||
|
// Simulate recovery on retry
|
||||||
|
capturedUppy.emit('upload-success', mockFile, { status: 200 })
|
||||||
|
capturedUppy.emit('complete', { successful: [mockFile], failed: [] })
|
||||||
|
|
||||||
|
await uploadPromise
|
||||||
|
|
||||||
|
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('completed')
|
||||||
|
expect(activeUploads.size).toBe(0)
|
||||||
|
|
||||||
|
addFileSpy.mockRestore()
|
||||||
|
retryAllSpy.mockRestore()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('fails permanently on 500 Internal Server Error without retrying', async () => {
|
||||||
|
Object.defineProperty(navigator, 'onLine', { value: true, configurable: true })
|
||||||
|
useUploadStore
|
||||||
|
.getState()
|
||||||
|
.startTask('task-1', 'task-1', [{ fileId: 'file-1', name: 'server-err.mp4', size: 100 }])
|
||||||
|
|
||||||
|
const addFileSpy = vi.spyOn(Uppy.prototype, 'addFile').mockReturnValue('f1')
|
||||||
|
|
||||||
|
const uploadPromise = uploadFilesWithUppy({
|
||||||
|
files: [
|
||||||
|
{ id: 'temp-1', file: new File(['content'], 'server-err.mp4', { type: 'video/mp4' }) },
|
||||||
|
],
|
||||||
|
taskId: 'task-1',
|
||||||
|
teamId: 'team-1',
|
||||||
|
storageBackend: 's3',
|
||||||
|
createdAssets: [{ tempId: 'temp-1', assetId: 'file-1', key: 'files/server-err.mp4' }],
|
||||||
|
})
|
||||||
|
|
||||||
|
await new Promise((r) => setTimeout(r, 10))
|
||||||
|
const capturedUppy = addFileSpy.mock.contexts[0] as Uppy
|
||||||
|
const mockFile = {
|
||||||
|
id: 'f1',
|
||||||
|
name: 'server-err.mp4',
|
||||||
|
meta: { fileId: 'file-1' },
|
||||||
|
} as unknown as UppyFile<Meta, Record<string, never>>
|
||||||
|
|
||||||
|
const serverError = new UploadHttpError('Internal Server Error', {
|
||||||
|
status: 500,
|
||||||
|
isTransient: false,
|
||||||
|
})
|
||||||
|
capturedUppy.emit('upload-error', mockFile, serverError)
|
||||||
|
capturedUppy.emit('complete', {
|
||||||
|
successful: [],
|
||||||
|
failed: [{ ...mockFile, error: 'Internal Server Error' }],
|
||||||
|
})
|
||||||
|
|
||||||
|
await uploadPromise
|
||||||
|
|
||||||
|
expect(client.api.teams[':teamId'].upload.tasks[':taskId'].$patch).toHaveBeenCalledWith({
|
||||||
|
param: { teamId: 'team-1', taskId: 'task-1' },
|
||||||
|
json: { fileId: 'file-1', errorMessage: 'Internal Server Error' },
|
||||||
|
})
|
||||||
|
expect(toast.error).toHaveBeenCalledWith(
|
||||||
|
m.failed_upload_file_with_name({ name: 'server-err.mp4' }),
|
||||||
|
)
|
||||||
|
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('failed')
|
||||||
|
expect(activeUploads.size).toBe(0)
|
||||||
|
|
||||||
|
addFileSpy.mockRestore()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('fails permanently on 500/403 status even while navigator.onLine is false', async () => {
|
||||||
|
Object.defineProperty(navigator, 'onLine', { value: false, configurable: true })
|
||||||
|
useUploadStore
|
||||||
|
.getState()
|
||||||
|
.startTask('task-1', 'task-1', [{ fileId: 'file-1', name: 'offline-500.mp4', size: 100 }])
|
||||||
|
|
||||||
|
const addFileSpy = vi.spyOn(Uppy.prototype, 'addFile').mockReturnValue('f1')
|
||||||
|
|
||||||
|
const uploadPromise = uploadFilesWithUppy({
|
||||||
|
files: [
|
||||||
|
{ id: 'temp-1', file: new File(['content'], 'offline-500.mp4', { type: 'video/mp4' }) },
|
||||||
|
],
|
||||||
|
taskId: 'task-1',
|
||||||
|
teamId: 'team-1',
|
||||||
|
storageBackend: 's3',
|
||||||
|
createdAssets: [{ tempId: 'temp-1', assetId: 'file-1', key: 'files/offline-500.mp4' }],
|
||||||
|
})
|
||||||
|
|
||||||
|
await new Promise((r) => setTimeout(r, 10))
|
||||||
|
const capturedUppy = addFileSpy.mock.contexts[0] as Uppy
|
||||||
|
const mockFile = {
|
||||||
|
id: 'f1',
|
||||||
|
name: 'offline-500.mp4',
|
||||||
|
meta: { fileId: 'file-1' },
|
||||||
|
} as unknown as UppyFile<Meta, Record<string, never>>
|
||||||
|
|
||||||
|
const serverError = new UploadHttpError('Internal Server Error', {
|
||||||
|
status: 500,
|
||||||
|
isTransient: false,
|
||||||
|
})
|
||||||
|
capturedUppy.emit('upload-error', mockFile, serverError)
|
||||||
|
capturedUppy.emit('complete', {
|
||||||
|
successful: [],
|
||||||
|
failed: [{ ...mockFile, error: 'Internal Server Error' }],
|
||||||
|
})
|
||||||
|
|
||||||
|
await uploadPromise
|
||||||
|
|
||||||
|
expect(client.api.teams[':teamId'].upload.tasks[':taskId'].$patch).toHaveBeenCalledWith({
|
||||||
|
param: { teamId: 'team-1', taskId: 'task-1' },
|
||||||
|
json: { fileId: 'file-1', errorMessage: 'Internal Server Error' },
|
||||||
|
})
|
||||||
|
expect(toast.error).toHaveBeenCalledWith(
|
||||||
|
m.failed_upload_file_with_name({ name: 'offline-500.mp4' }),
|
||||||
|
)
|
||||||
|
expect(useUploadStore.getState().tasks['task-1']?.files['file-1']?.status).toBe('failed')
|
||||||
|
expect(activeUploads.size).toBe(0)
|
||||||
|
|
||||||
|
addFileSpy.mockRestore()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('cancelActiveUploadTask cancels in-flight uppy upload and cleans up activeUploads', async () => {
|
||||||
|
useUploadStore
|
||||||
|
.getState()
|
||||||
|
.startTask('task-cancel', 'Task Cancel', [
|
||||||
|
{ fileId: 'file-c1', name: 'cancel.mp4', size: 1000 },
|
||||||
|
])
|
||||||
|
|
||||||
|
const uploadPromise = uploadFilesWithUppy({
|
||||||
|
files: [
|
||||||
|
{
|
||||||
|
id: 'temp-c1',
|
||||||
|
file: new File(['data'], 'cancel.mp4', { type: 'video/mp4' }),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
taskId: 'task-cancel',
|
||||||
|
teamId: 'team-1',
|
||||||
|
storageBackend: 's3',
|
||||||
|
createdAssets: [{ tempId: 'temp-c1', assetId: 'file-c1', key: 'files/cancel.mp4' }],
|
||||||
|
})
|
||||||
|
|
||||||
|
await new Promise((r) => setTimeout(r, 10))
|
||||||
|
expect(activeUploads.has('file-c1')).toBe(true)
|
||||||
|
|
||||||
|
cancelActiveUploadTask('task-cancel')
|
||||||
|
|
||||||
|
expect(activeUploads.has('file-c1')).toBe(false)
|
||||||
|
await expect(uploadPromise).resolves.toBeUndefined()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
+264
-59
@@ -5,6 +5,7 @@ import type { PresignedUrl } from '@shumai/dtos'
|
|||||||
import { client } from '@/ui/api/client'
|
import { client } from '@/ui/api/client'
|
||||||
import { useUploadStore } from '@/ui/stores/upload'
|
import { useUploadStore } from '@/ui/stores/upload'
|
||||||
import { toast } from 'sonner'
|
import { toast } from 'sonner'
|
||||||
|
import { m } from '@/ui/paraglide/messages.js'
|
||||||
import { acquireWakeLock, releaseWakeLock } from './wake-lock'
|
import { acquireWakeLock, releaseWakeLock } from './wake-lock'
|
||||||
|
|
||||||
export type FileWithId = {
|
export type FileWithId = {
|
||||||
@@ -18,9 +19,28 @@ export interface ActiveUploadFile {
|
|||||||
fileId: string
|
fileId: string
|
||||||
key?: string
|
key?: string
|
||||||
uploadId?: string
|
uploadId?: string
|
||||||
|
url?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const activeUploads = new Map<string, ActiveUploadFile>()
|
export const activeUploads = new Map<string, ActiveUploadFile>()
|
||||||
|
export const activeUppyByTaskId = new Map<string, Uppy>()
|
||||||
|
|
||||||
|
export function cancelActiveUploadTask(taskId: string): void {
|
||||||
|
const uppy = activeUppyByTaskId.get(taskId)
|
||||||
|
if (uppy) {
|
||||||
|
try {
|
||||||
|
uppy.cancelAll()
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('Failed to cancel uppy uploads:', err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const [fileId, item] of activeUploads.entries()) {
|
||||||
|
if (item.taskId === taskId) {
|
||||||
|
activeUploads.delete(fileId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Setup beforeunload and pagehide listeners once
|
// Setup beforeunload and pagehide listeners once
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
@@ -63,26 +83,151 @@ if (typeof window !== 'undefined') {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const LOCALHOST_HOSTS = new Set(['localhost', '127.0.0.1', '::1', '[::1]', '0.0.0.0'])
|
||||||
|
|
||||||
|
export const MAX_RETRY_DELAY_MS = 60000
|
||||||
|
export const BASE_RETRY_DELAY_MS = 1000
|
||||||
|
|
||||||
|
export class UploadNetworkError extends Error {
|
||||||
|
readonly code = 'NETWORK_ERROR' as const
|
||||||
|
constructor(message: string, options?: ErrorOptions) {
|
||||||
|
super(message, options)
|
||||||
|
this.name = 'UploadNetworkError'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class UploadHttpError extends Error {
|
||||||
|
readonly code = 'HTTP_ERROR' as const
|
||||||
|
readonly status: number
|
||||||
|
readonly isTransient: boolean
|
||||||
|
constructor(message: string, options: { status: number; isTransient: boolean; cause?: unknown }) {
|
||||||
|
super(message, { cause: options.cause })
|
||||||
|
this.name = 'UploadHttpError'
|
||||||
|
this.status = options.status
|
||||||
|
this.isTransient = options.isTransient
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function calculateBackoffDelay(
|
||||||
|
attempts: number,
|
||||||
|
maxDelayMs = MAX_RETRY_DELAY_MS,
|
||||||
|
baseDelayMs = BASE_RETRY_DELAY_MS,
|
||||||
|
): number {
|
||||||
|
const effectiveBaseDelay =
|
||||||
|
(typeof window !== 'undefined' &&
|
||||||
|
(window as unknown as Record<string, number | undefined>)['__SHUMAI_UPLOAD_BASE_DELAY__']) ||
|
||||||
|
baseDelayMs
|
||||||
|
const exponentialDelay = Math.min(Math.pow(2, attempts) * effectiveBaseDelay, maxDelayMs)
|
||||||
|
const jitter = exponentialDelay * (Math.random() * 0.3 - 0.15) // ±15%
|
||||||
|
return Math.min(maxDelayMs, Math.max(effectiveBaseDelay, Math.round(exponentialDelay + jitter)))
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isLocalhostStorageMismatch(uploadUrlString?: string): boolean {
|
||||||
|
if (!uploadUrlString || typeof window === 'undefined') return false
|
||||||
|
try {
|
||||||
|
const uploadUrl = new URL(uploadUrlString, window.location.origin)
|
||||||
|
const isUploadLocalhost = LOCALHOST_HOSTS.has(uploadUrl.hostname)
|
||||||
|
const isWindowLocalhost = LOCALHOST_HOSTS.has(window.location.hostname)
|
||||||
|
|
||||||
|
return isUploadLocalhost && !isWindowLocalhost
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function isNetworkIssue(error?: unknown): boolean {
|
export function isNetworkIssue(error?: unknown): boolean {
|
||||||
|
if (!error) {
|
||||||
|
return typeof navigator !== 'undefined' && !navigator.onLine
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. Structured typed errors
|
||||||
|
if (error instanceof UploadHttpError) {
|
||||||
|
return error.isTransient
|
||||||
|
}
|
||||||
|
if (error instanceof UploadNetworkError) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
const err = (typeof error === 'object' && error !== null ? error : {}) as Record<string, unknown>
|
||||||
|
|
||||||
|
// 2. HTTP status inspection (check first before generic offline or NetworkError flags,
|
||||||
|
// so non-retryable HTTP 500 / 4xx responses delivered as the browser transitions offline
|
||||||
|
// fail immediately instead of being treated as network drops)
|
||||||
|
const status =
|
||||||
|
typeof err.status === 'number'
|
||||||
|
? err.status
|
||||||
|
: typeof (err.request as Record<string, unknown> | undefined)?.status === 'number'
|
||||||
|
? ((err.request as Record<string, unknown>).status as number)
|
||||||
|
: undefined
|
||||||
|
|
||||||
|
if (status !== undefined) {
|
||||||
|
if (status === 0 || status === 429 || status === 502 || status === 503 || status === 504) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
// Explicitly non-retryable statuses (500 Internal Server Error, 4xx except 429)
|
||||||
|
if (status === 500 || (status >= 400 && status < 500)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (status >= 500 && status < 600) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Fallback string matching for explicit non-retryable HTTP status messages
|
||||||
|
const rawMsg =
|
||||||
|
typeof error === 'string' ? error : typeof err.message === 'string' ? err.message : ''
|
||||||
|
if (rawMsg) {
|
||||||
|
const msg = rawMsg.toLowerCase()
|
||||||
|
// Explicit non-retryable status codes in string messages
|
||||||
|
if (
|
||||||
|
msg.includes('status: 500') ||
|
||||||
|
msg.includes('status: 400') ||
|
||||||
|
msg.includes('status: 401') ||
|
||||||
|
msg.includes('status: 403') ||
|
||||||
|
msg.includes('status: 404')
|
||||||
|
) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
msg.includes('network error') ||
|
||||||
|
msg.includes('failed to fetch') ||
|
||||||
|
msg.includes('networkerror') ||
|
||||||
|
msg.includes('network changed') ||
|
||||||
|
msg.includes('err_network_changed') ||
|
||||||
|
msg.includes('err_internet_disconnected') ||
|
||||||
|
msg.includes('err_connection_') ||
|
||||||
|
msg.includes('err_name_not_resolved') ||
|
||||||
|
msg.includes('the network connection was lost') ||
|
||||||
|
msg.includes('the internet connection appears to be offline') ||
|
||||||
|
(msg.includes('load failed') && !msg.includes('upload failed')) ||
|
||||||
|
msg.includes('timed out') ||
|
||||||
|
msg.includes('timeout') ||
|
||||||
|
msg.includes('bad gateway') ||
|
||||||
|
msg.includes('gateway timeout') ||
|
||||||
|
msg.includes('service unavailable')
|
||||||
|
) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Generic offline state (only when the error does not carry a non-retryable status code)
|
||||||
if (typeof navigator !== 'undefined' && !navigator.onLine) {
|
if (typeof navigator !== 'undefined' && !navigator.onLine) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if (!error) return false
|
|
||||||
const err = error as Record<string, unknown>
|
if (err.code === 'NETWORK' || err.code === 'NETWORK_ERROR') return true
|
||||||
if (err.isNetworkError === true) return true
|
if (err.name === 'S3NetworkError' || err.name === 'UploadNetworkError') {
|
||||||
if (err.name === 'S3NetworkError' || err.name === 'NetworkError') return true
|
|
||||||
const msg = (typeof err.message === 'string' ? err.message : '').toLowerCase()
|
|
||||||
if (
|
|
||||||
msg.includes('network error') ||
|
|
||||||
msg.includes('failed to fetch') ||
|
|
||||||
msg.includes('networkerror') ||
|
|
||||||
msg.includes('load failed') ||
|
|
||||||
msg.includes('err_internet_disconnected')
|
|
||||||
) {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
const req = err.request as Record<string, unknown> | undefined
|
|
||||||
if (req && req.status === 0) return true
|
// Standard fetch network failure
|
||||||
|
if (error instanceof TypeError) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there was no HTTP status attached, check generic Uppy NetworkError flag
|
||||||
|
if (err.name === 'NetworkError' || err.isNetworkError === true) return true
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,7 +255,7 @@ export async function uploadFilesWithUppy({
|
|||||||
await acquireWakeLock()
|
await acquireWakeLock()
|
||||||
|
|
||||||
const store = useUploadStore.getState()
|
const store = useUploadStore.getState()
|
||||||
const { increment, decrement, updateFileProgress, completeFile, failFile } = store
|
const { increment, updateFileProgress, completeFile, failFile } = store
|
||||||
|
|
||||||
// Build lookup maps
|
// Build lookup maps
|
||||||
const assetByTempId = new Map<string, { assetId: string; key?: string }>()
|
const assetByTempId = new Map<string, { assetId: string; key?: string }>()
|
||||||
@@ -131,6 +276,7 @@ export async function uploadFilesWithUppy({
|
|||||||
autoProceed: true,
|
autoProceed: true,
|
||||||
allowMultipleUploadBatches: false,
|
allowMultipleUploadBatches: false,
|
||||||
})
|
})
|
||||||
|
activeUppyByTaskId.set(taskId, uppy)
|
||||||
|
|
||||||
if (storageBackend === 's3') {
|
if (storageBackend === 's3') {
|
||||||
uppy.use(AwsS3, {
|
uppy.use(AwsS3, {
|
||||||
@@ -153,24 +299,43 @@ export async function uploadFilesWithUppy({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await fetch(`/api/teams/${teamId}/upload/sign`, {
|
let res: Response
|
||||||
method: 'POST',
|
try {
|
||||||
headers: { 'Content-Type': 'application/json' },
|
res = await fetch(`/api/teams/${teamId}/upload/sign`, {
|
||||||
body: JSON.stringify({
|
method: 'POST',
|
||||||
key: req.key,
|
headers: { 'Content-Type': 'application/json' },
|
||||||
method: req.method,
|
body: JSON.stringify({
|
||||||
uploadId,
|
key: req.key,
|
||||||
partNumber,
|
method: req.method,
|
||||||
fileId,
|
uploadId,
|
||||||
}),
|
partNumber,
|
||||||
credentials: 'include',
|
fileId,
|
||||||
})
|
}),
|
||||||
|
credentials: 'include',
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof TypeError) {
|
||||||
|
throw new UploadNetworkError('Network error while signing request', { cause: err })
|
||||||
|
}
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
throw new Error(`Failed to sign request: ${res.statusText}`)
|
const isTransient =
|
||||||
|
res.status === 429 || res.status === 502 || res.status === 503 || res.status === 504
|
||||||
|
throw new UploadHttpError(`Failed to sign request: ${res.statusText}`, {
|
||||||
|
status: res.status,
|
||||||
|
isTransient,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = (await res.json()) as { url: string }
|
const data = (await res.json()) as { url: string }
|
||||||
|
if (fileId) {
|
||||||
|
const active = activeUploads.get(fileId)
|
||||||
|
if (active) {
|
||||||
|
active.url = data.url
|
||||||
|
}
|
||||||
|
}
|
||||||
return { url: data.url }
|
return { url: data.url }
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -199,7 +364,7 @@ export async function uploadFilesWithUppy({
|
|||||||
|
|
||||||
const confirmationPromises = new Map<string, Promise<void>>()
|
const confirmationPromises = new Map<string, Promise<void>>()
|
||||||
const retryCounts = new Map<string, number>()
|
const retryCounts = new Map<string, number>()
|
||||||
const MAX_ONLINE_RETRIES = 3
|
const pendingNetworkRetries = new Set<string>()
|
||||||
let retryTimeout: ReturnType<typeof setTimeout> | null = null
|
let retryTimeout: ReturnType<typeof setTimeout> | null = null
|
||||||
|
|
||||||
const triggerRetry = () => {
|
const triggerRetry = () => {
|
||||||
@@ -225,6 +390,11 @@ export async function uploadFilesWithUppy({
|
|||||||
const handleBackOnline = () => {
|
const handleBackOnline = () => {
|
||||||
const isOnline = typeof navigator !== 'undefined' ? navigator.onLine : true
|
const isOnline = typeof navigator !== 'undefined' ? navigator.onLine : true
|
||||||
console.log(`[Uploader] Network 'online' event received! navigator.onLine = ${isOnline}`)
|
console.log(`[Uploader] Network 'online' event received! navigator.onLine = ${isOnline}`)
|
||||||
|
retryCounts.clear()
|
||||||
|
if (retryTimeout) {
|
||||||
|
clearTimeout(retryTimeout)
|
||||||
|
retryTimeout = null
|
||||||
|
}
|
||||||
triggerRetry()
|
triggerRetry()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,6 +420,10 @@ export async function uploadFilesWithUppy({
|
|||||||
uppy.on('upload-success', (file) => {
|
uppy.on('upload-success', (file) => {
|
||||||
if (!file) return
|
if (!file) return
|
||||||
const fileId = file.meta.fileId as string
|
const fileId = file.meta.fileId as string
|
||||||
|
if (fileId) {
|
||||||
|
pendingNetworkRetries.delete(fileId)
|
||||||
|
retryCounts.delete(fileId)
|
||||||
|
}
|
||||||
activeUploads.delete(fileId)
|
activeUploads.delete(fileId)
|
||||||
|
|
||||||
const confirmPromise = (async () => {
|
const confirmPromise = (async () => {
|
||||||
@@ -262,13 +436,11 @@ export async function uploadFilesWithUppy({
|
|||||||
throw new Error(`Failed to confirm upload: ${res.statusText}`)
|
throw new Error(`Failed to confirm upload: ${res.statusText}`)
|
||||||
}
|
}
|
||||||
completeFile(taskId, fileId)
|
completeFile(taskId, fileId)
|
||||||
decrement()
|
|
||||||
await onFileFinished?.(fileId)
|
await onFileFinished?.(fileId)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to confirm upload:', err)
|
console.error('Failed to confirm upload:', err)
|
||||||
failFile(taskId, fileId)
|
failFile(taskId, fileId)
|
||||||
decrement()
|
toast.error(m.failed_confirm_upload_with_name({ name: file.name }))
|
||||||
toast.error(`Failed to confirm upload: ${file.name}`)
|
|
||||||
await onFileFinished?.(fileId)
|
await onFileFinished?.(fileId)
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
@@ -283,11 +455,20 @@ export async function uploadFilesWithUppy({
|
|||||||
) => {
|
) => {
|
||||||
const fileId = file.meta.fileId as string
|
const fileId = file.meta.fileId as string
|
||||||
if (!activeUploads.has(fileId)) return
|
if (!activeUploads.has(fileId)) return
|
||||||
|
const active = activeUploads.get(fileId)
|
||||||
|
const uploadUrl = (file.meta?.url as string | undefined) || active?.url
|
||||||
|
|
||||||
activeUploads.delete(fileId)
|
activeUploads.delete(fileId)
|
||||||
failFile(taskId, fileId)
|
failFile(taskId, fileId)
|
||||||
decrement()
|
|
||||||
|
|
||||||
toast.error(`Failed to upload file: ${file.name}`)
|
if (isLocalhostStorageMismatch(uploadUrl)) {
|
||||||
|
toast.error(m.upload_failed_s3_endpoint_localhost(), {
|
||||||
|
id: 's3-endpoint-localhost-mismatch',
|
||||||
|
duration: 10000,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
toast.error(m.failed_upload_file_with_name({ name: file.name }))
|
||||||
|
}
|
||||||
|
|
||||||
const errorPromise = (async () => {
|
const errorPromise = (async () => {
|
||||||
try {
|
try {
|
||||||
@@ -295,7 +476,8 @@ export async function uploadFilesWithUppy({
|
|||||||
param: { teamId, taskId },
|
param: { teamId, taskId },
|
||||||
json: {
|
json: {
|
||||||
fileId,
|
fileId,
|
||||||
errorMessage: (error as Error)?.message || 'Upload failed',
|
errorMessage:
|
||||||
|
(error as Error)?.message || (typeof error === 'string' ? error : 'Upload failed'),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -311,32 +493,49 @@ export async function uploadFilesWithUppy({
|
|||||||
if (!file) return
|
if (!file) return
|
||||||
console.error(`[Uploader] upload-error for ${file.name}:`, error)
|
console.error(`[Uploader] upload-error for ${file.name}:`, error)
|
||||||
|
|
||||||
|
const fileId = file.meta?.fileId as string | undefined
|
||||||
|
const uploadUrl =
|
||||||
|
(file.meta?.url as string | undefined) ||
|
||||||
|
(fileId ? activeUploads.get(fileId)?.url : undefined)
|
||||||
|
|
||||||
|
if (isLocalhostStorageMismatch(uploadUrl)) {
|
||||||
|
console.warn(
|
||||||
|
`[Uploader] Localhost storage endpoint mismatch detected for ${file.name}. Failing permanently without retry.`,
|
||||||
|
)
|
||||||
|
if (fileId) {
|
||||||
|
pendingNetworkRetries.delete(fileId)
|
||||||
|
}
|
||||||
|
handlePermanentError(file, error)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (isNetworkIssue(error)) {
|
if (isNetworkIssue(error)) {
|
||||||
console.warn(
|
console.warn(
|
||||||
`[Uploader] Network error detected for ${file.name}. Keeping upload active to resume when connection is restored.`,
|
`[Uploader] Network error detected for ${file.name}. Keeping upload active to resume when connection is restored.`,
|
||||||
)
|
)
|
||||||
|
|
||||||
if (typeof navigator !== 'undefined' && navigator.onLine) {
|
if (fileId) {
|
||||||
const fileId = file.meta.fileId as string
|
pendingNetworkRetries.add(fileId)
|
||||||
const attempts = (retryCounts.get(fileId) || 0) + 1
|
}
|
||||||
retryCounts.set(fileId, attempts)
|
|
||||||
|
|
||||||
if (attempts > MAX_ONLINE_RETRIES) {
|
if (typeof navigator !== 'undefined' && navigator.onLine) {
|
||||||
console.error(
|
const attempts = (fileId ? retryCounts.get(fileId) || 0 : 0) + 1
|
||||||
`[Uploader] Exceeded max online retries (${MAX_ONLINE_RETRIES}) for ${file.name}. Failing permanently.`,
|
if (fileId) {
|
||||||
)
|
retryCounts.set(fileId, attempts)
|
||||||
handlePermanentError(file, error)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const delayMs = calculateBackoffDelay(attempts)
|
||||||
console.log(
|
console.log(
|
||||||
`[Uploader] Scheduling retry for ${file.name} (attempt ${attempts}/${MAX_ONLINE_RETRIES})...`,
|
`[Uploader] Scheduling retry for ${file.name} (attempt ${attempts}, delay ${delayMs}ms)...`,
|
||||||
)
|
)
|
||||||
scheduleRetry(2000)
|
scheduleRetry(delayMs)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (fileId) {
|
||||||
|
pendingNetworkRetries.delete(fileId)
|
||||||
|
}
|
||||||
handlePermanentError(file, error)
|
handlePermanentError(file, error)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -357,6 +556,7 @@ export async function uploadFilesWithUppy({
|
|||||||
taskId,
|
taskId,
|
||||||
fileId,
|
fileId,
|
||||||
key,
|
key,
|
||||||
|
url,
|
||||||
})
|
})
|
||||||
|
|
||||||
increment()
|
increment()
|
||||||
@@ -383,20 +583,24 @@ export async function uploadFilesWithUppy({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const failedFiles = result?.failed || []
|
const failedFiles = result?.failed || []
|
||||||
const isOffline = typeof navigator !== 'undefined' && !navigator.onLine
|
|
||||||
const hasNetworkFailures =
|
|
||||||
isOffline || failedFiles.some((f) => isNetworkIssue((f as { error?: unknown })?.error))
|
|
||||||
|
|
||||||
if (hasNetworkFailures) {
|
|
||||||
console.log(
|
|
||||||
'[Uploader] Batch completed with network issues; waiting for reconnection to resume...',
|
|
||||||
)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const f of failedFiles) {
|
for (const f of failedFiles) {
|
||||||
const file = f as { name: string; meta: Record<string, unknown> }
|
const file = f as { name: string; meta: Record<string, unknown> }
|
||||||
handlePermanentError(file, (f as { error?: unknown })?.error)
|
const fId = file.meta?.fileId as string | undefined
|
||||||
|
if (fId && !pendingNetworkRetries.has(fId)) {
|
||||||
|
const err = (f as { error?: unknown })?.error
|
||||||
|
if (isNetworkIssue(err)) {
|
||||||
|
pendingNetworkRetries.add(fId)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
handlePermanentError(file, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pendingNetworkRetries.size > 0) {
|
||||||
|
console.log(
|
||||||
|
`[Uploader] Batch completed with ${pendingNetworkRetries.size} retrying file(s); waiting for reconnection or backoff to resume...`,
|
||||||
|
)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (activeUploads.size === 0) {
|
if (activeUploads.size === 0) {
|
||||||
@@ -412,6 +616,7 @@ export async function uploadFilesWithUppy({
|
|||||||
|
|
||||||
await Promise.allSettled(Array.from(confirmationPromises.values()))
|
await Promise.allSettled(Array.from(confirmationPromises.values()))
|
||||||
} finally {
|
} finally {
|
||||||
|
activeUppyByTaskId.delete(taskId)
|
||||||
if (retryTimeout) {
|
if (retryTimeout) {
|
||||||
clearTimeout(retryTimeout)
|
clearTimeout(retryTimeout)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1124,6 +1124,9 @@
|
|||||||
"n_folders_singular": "{count} Folder",
|
"n_folders_singular": "{count} Folder",
|
||||||
"n_folders_plural": "{count} Folders",
|
"n_folders_plural": "{count} Folders",
|
||||||
"failed_upload_file": "Failed to upload file",
|
"failed_upload_file": "Failed to upload file",
|
||||||
|
"failed_upload_file_with_name": "Failed to upload file: {name}",
|
||||||
|
"failed_confirm_upload_with_name": "Failed to confirm upload: {name}",
|
||||||
|
"upload_failed_s3_endpoint_localhost": "Upload failed: Server storage endpoint is configured as localhost, but Shumai is being accessed from a remote address. Please set the AWS_ENDPOINT_URL_S3 environment variable on your server to your external URL.",
|
||||||
|
|
||||||
"failed_fetch_share_link": "Failed to fetch share link",
|
"failed_fetch_share_link": "Failed to fetch share link",
|
||||||
"failed_fetch_folders": "Failed to fetch folders",
|
"failed_fetch_folders": "Failed to fetch folders",
|
||||||
@@ -1604,5 +1607,10 @@
|
|||||||
"deny": "Deny",
|
"deny": "Deny",
|
||||||
"denying": "Denying...",
|
"denying": "Denying...",
|
||||||
"consent_failed": "Failed to process authorization",
|
"consent_failed": "Failed to process authorization",
|
||||||
"mcp_access_scope_desc": "Full user-level access to Shumai workspace and MCP tools"
|
"mcp_access_scope_desc": "Full user-level access to Shumai workspace and MCP tools",
|
||||||
|
"cancel_upload_task": "Cancel upload",
|
||||||
|
"cancel_upload_task_confirm_title": "Cancel upload task?",
|
||||||
|
"cancel_upload_task_confirm_description": "Files that have already finished uploading will be kept in your project. Any incomplete or pending uploads will be canceled and removed.",
|
||||||
|
"upload_task_canceled": "Upload task canceled",
|
||||||
|
"failed_to_cancel_upload_task": "Failed to cancel upload task"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1124,6 +1124,9 @@
|
|||||||
"n_folders_singular": "{count} 个文件夹",
|
"n_folders_singular": "{count} 个文件夹",
|
||||||
"n_folders_plural": "{count} 个文件夹",
|
"n_folders_plural": "{count} 个文件夹",
|
||||||
"failed_upload_file": "上传文件失败",
|
"failed_upload_file": "上传文件失败",
|
||||||
|
"failed_upload_file_with_name": "上传文件失败: {name}",
|
||||||
|
"failed_confirm_upload_with_name": "确认上传失败: {name}",
|
||||||
|
"upload_failed_s3_endpoint_localhost": "上传失败:服务器存储端点配置为 localhost,但当前通过远程地址访问 Shumai。请在服务器上将 AWS_ENDPOINT_URL_S3 环境变量配置为外部访问地址。",
|
||||||
|
|
||||||
"failed_fetch_share_link": "获取分享链接失败",
|
"failed_fetch_share_link": "获取分享链接失败",
|
||||||
"failed_fetch_folders": "获取文件夹失败",
|
"failed_fetch_folders": "获取文件夹失败",
|
||||||
@@ -1604,5 +1607,10 @@
|
|||||||
"deny": "拒绝",
|
"deny": "拒绝",
|
||||||
"denying": "拒绝中...",
|
"denying": "拒绝中...",
|
||||||
"consent_failed": "处理授权失败",
|
"consent_failed": "处理授权失败",
|
||||||
"mcp_access_scope_desc": "以当前用户权限完全访问 Shumai 工作空间及 MCP 工具"
|
"mcp_access_scope_desc": "以当前用户权限完全访问 Shumai 工作空间及 MCP 工具",
|
||||||
|
"cancel_upload_task": "取消上传",
|
||||||
|
"cancel_upload_task_confirm_title": "取消上传任务?",
|
||||||
|
"cancel_upload_task_confirm_description": "已完成上传的文件将保留在项目中。任何未完成或等待中的上传将被取消并移除。",
|
||||||
|
"upload_task_canceled": "上传任务已取消",
|
||||||
|
"failed_to_cancel_upload_task": "取消上传任务失败"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,7 +75,12 @@ function RootComponent() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (!showSidebar) {
|
if (!showSidebar) {
|
||||||
return <Outlet />
|
return (
|
||||||
|
<>
|
||||||
|
<Toaster />
|
||||||
|
<Outlet />
|
||||||
|
</>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const unreadCount = me?.unreadNotificationCount ?? 0
|
const unreadCount = me?.unreadNotificationCount ?? 0
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
import { describe, it, expect, beforeEach } from 'vitest'
|
||||||
|
import { useUploadStore } from './upload'
|
||||||
|
|
||||||
|
describe('useUploadStore', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
useUploadStore.setState({
|
||||||
|
uploading: 0,
|
||||||
|
tasks: {},
|
||||||
|
fileProgress: {},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('starts task and tracks initial progress', () => {
|
||||||
|
useUploadStore.getState().startTask('task-1', 'My Task', [
|
||||||
|
{ fileId: 'f1', name: 'video1.mp4', size: 1000 },
|
||||||
|
{ fileId: 'f2', name: 'video2.mp4', size: 2000 },
|
||||||
|
])
|
||||||
|
|
||||||
|
const state = useUploadStore.getState()
|
||||||
|
expect(state.tasks['task-1']).toBeDefined()
|
||||||
|
expect(state.tasks['task-1'].name).toBe('My Task')
|
||||||
|
expect(state.tasks['task-1'].total).toBe(3000)
|
||||||
|
expect(state.tasks['task-1'].loaded).toBe(0)
|
||||||
|
expect(state.tasks['task-1'].files['f1'].status).toBe('uploading')
|
||||||
|
expect(state.tasks['task-1'].files['f2'].status).toBe('uploading')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('updates file progress and task loaded bytes', () => {
|
||||||
|
useUploadStore.getState().startTask('task-1', 'My Task', [
|
||||||
|
{ fileId: 'f1', name: 'video1.mp4', size: 1000 },
|
||||||
|
{ fileId: 'f2', name: 'video2.mp4', size: 2000 },
|
||||||
|
])
|
||||||
|
|
||||||
|
useUploadStore.getState().updateFileProgress('task-1', 'f1', 500)
|
||||||
|
expect(useUploadStore.getState().tasks['task-1'].loaded).toBe(500)
|
||||||
|
expect(useUploadStore.getState().fileProgress['f1'].loaded).toBe(500)
|
||||||
|
|
||||||
|
useUploadStore.getState().completeFile('task-1', 'f1')
|
||||||
|
expect(useUploadStore.getState().tasks['task-1'].files['f1'].status).toBe('completed')
|
||||||
|
expect(useUploadStore.getState().tasks['task-1'].files['f1'].loaded).toBe(1000)
|
||||||
|
expect(useUploadStore.getState().tasks['task-1'].loaded).toBe(1000)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('cancelTask marks incomplete files as failed and decrements uploading count', () => {
|
||||||
|
useUploadStore.setState({ uploading: 3 })
|
||||||
|
useUploadStore.getState().startTask('task-1', 'My Task', [
|
||||||
|
{ fileId: 'f1', name: 'done.mp4', size: 1000 },
|
||||||
|
{ fileId: 'f2', name: 'stuck1.mp4', size: 2000 },
|
||||||
|
{ fileId: 'f3', name: 'stuck2.mp4', size: 3000 },
|
||||||
|
])
|
||||||
|
|
||||||
|
// f1 completes (automatically decrements uploading from 3 to 2)
|
||||||
|
useUploadStore.getState().completeFile('task-1', 'f1')
|
||||||
|
expect(useUploadStore.getState().uploading).toBe(2)
|
||||||
|
|
||||||
|
// cancelTask is called
|
||||||
|
useUploadStore.getState().cancelTask('task-1')
|
||||||
|
|
||||||
|
const task = useUploadStore.getState().tasks['task-1']
|
||||||
|
expect(task.files['f1'].status).toBe('completed')
|
||||||
|
expect(task.files['f2'].status).toBe('failed')
|
||||||
|
expect(task.files['f3'].status).toBe('failed')
|
||||||
|
expect(useUploadStore.getState().fileProgress['f2'].status).toBe('failed')
|
||||||
|
expect(useUploadStore.getState().fileProgress['f3'].status).toBe('failed')
|
||||||
|
expect(useUploadStore.getState().uploading).toBe(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('does not decrement uploading count twice if completeFile or failFile is called after cancelTask', () => {
|
||||||
|
useUploadStore.setState({ uploading: 2 })
|
||||||
|
useUploadStore.getState().startTask('task-1', 'My Task', [
|
||||||
|
{ fileId: 'f1', name: 'in-flight.mp4', size: 1000 },
|
||||||
|
{ fileId: 'f2', name: 'waiting.mp4', size: 2000 },
|
||||||
|
])
|
||||||
|
|
||||||
|
// Cancel task while f1 confirmation was in-flight
|
||||||
|
useUploadStore.getState().cancelTask('task-1')
|
||||||
|
expect(useUploadStore.getState().uploading).toBe(0)
|
||||||
|
expect(useUploadStore.getState().tasks['task-1'].files['f1'].status).toBe('failed')
|
||||||
|
|
||||||
|
// In-flight confirmation settles late
|
||||||
|
useUploadStore.getState().completeFile('task-1', 'f1')
|
||||||
|
expect(useUploadStore.getState().uploading).toBe(0)
|
||||||
|
|
||||||
|
// Another late error settles
|
||||||
|
useUploadStore.getState().failFile('task-1', 'f2')
|
||||||
|
expect(useUploadStore.getState().uploading).toBe(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('cancelTask gracefully handles non-existent task', () => {
|
||||||
|
useUploadStore.setState({ uploading: 1 })
|
||||||
|
useUploadStore.getState().cancelTask('non-existent')
|
||||||
|
expect(useUploadStore.getState().uploading).toBe(1)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -30,6 +30,7 @@ type UploadStore = {
|
|||||||
updateFileProgress: (taskId: string, fileId: string, loaded: number) => void
|
updateFileProgress: (taskId: string, fileId: string, loaded: number) => void
|
||||||
completeFile: (taskId: string, fileId: string) => void
|
completeFile: (taskId: string, fileId: string) => void
|
||||||
failFile: (taskId: string, fileId: string) => void
|
failFile: (taskId: string, fileId: string) => void
|
||||||
|
cancelTask: (taskId: string) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useUploadStore = create<UploadStore>((set) => ({
|
export const useUploadStore = create<UploadStore>((set) => ({
|
||||||
@@ -114,12 +115,14 @@ export const useUploadStore = create<UploadStore>((set) => ({
|
|||||||
const file = task.files[fileId]
|
const file = task.files[fileId]
|
||||||
if (!file) return {}
|
if (!file) return {}
|
||||||
|
|
||||||
|
const wasUploading = file.status === 'uploading'
|
||||||
const updatedFile: FileUploadState = { ...file, status: 'completed', loaded: file.total }
|
const updatedFile: FileUploadState = { ...file, status: 'completed', loaded: file.total }
|
||||||
const updatedFiles = { ...task.files, [fileId]: updatedFile }
|
const updatedFiles = { ...task.files, [fileId]: updatedFile }
|
||||||
|
|
||||||
const loadedBytes = Object.values(updatedFiles).reduce((sum, f) => sum + f.loaded, 0)
|
const loadedBytes = Object.values(updatedFiles).reduce((sum, f) => sum + f.loaded, 0)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
uploading: wasUploading ? Math.max(0, state.uploading - 1) : state.uploading,
|
||||||
tasks: {
|
tasks: {
|
||||||
...state.tasks,
|
...state.tasks,
|
||||||
[taskId]: {
|
[taskId]: {
|
||||||
@@ -143,12 +146,14 @@ export const useUploadStore = create<UploadStore>((set) => ({
|
|||||||
const file = task.files[fileId]
|
const file = task.files[fileId]
|
||||||
if (!file) return {}
|
if (!file) return {}
|
||||||
|
|
||||||
|
const wasUploading = file.status === 'uploading'
|
||||||
const updatedFile: FileUploadState = { ...file, status: 'failed' }
|
const updatedFile: FileUploadState = { ...file, status: 'failed' }
|
||||||
const updatedFiles = { ...task.files, [fileId]: updatedFile }
|
const updatedFiles = { ...task.files, [fileId]: updatedFile }
|
||||||
|
|
||||||
const loadedBytes = Object.values(updatedFiles).reduce((sum, f) => sum + f.loaded, 0)
|
const loadedBytes = Object.values(updatedFiles).reduce((sum, f) => sum + f.loaded, 0)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
uploading: wasUploading ? Math.max(0, state.uploading - 1) : state.uploading,
|
||||||
tasks: {
|
tasks: {
|
||||||
...state.tasks,
|
...state.tasks,
|
||||||
[taskId]: {
|
[taskId]: {
|
||||||
@@ -163,4 +168,34 @@ export const useUploadStore = create<UploadStore>((set) => ({
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
cancelTask: (taskId) =>
|
||||||
|
set((state) => {
|
||||||
|
const task = state.tasks[taskId]
|
||||||
|
if (!task) return {}
|
||||||
|
|
||||||
|
let decrements = 0
|
||||||
|
const updatedFiles = { ...task.files }
|
||||||
|
const updatedFileProgress = { ...state.fileProgress }
|
||||||
|
|
||||||
|
for (const [fileId, file] of Object.entries(task.files)) {
|
||||||
|
if (file.status === 'uploading') {
|
||||||
|
updatedFiles[fileId] = { ...file, status: 'failed' }
|
||||||
|
updatedFileProgress[fileId] = { ...file, status: 'failed' }
|
||||||
|
decrements++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
uploading: Math.max(0, state.uploading - decrements),
|
||||||
|
tasks: {
|
||||||
|
...state.tasks,
|
||||||
|
[taskId]: {
|
||||||
|
...task,
|
||||||
|
files: updatedFiles,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fileProgress: updatedFileProgress,
|
||||||
|
}
|
||||||
|
}),
|
||||||
}))
|
}))
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import { DBOS, Error as DBOSError } from '@dbos-inc/dbos-sdk'
|
import { DBOS, Error as DBOSError } from '@dbos-inc/dbos-sdk'
|
||||||
|
|
||||||
|
export { DBOS, DBOSError }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Task queue constants.
|
* Task queue constants.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user