LibreChat/packages/api/src/files/dispatch.spec.ts
lia-by-librechat[bot] a51c45bbf3
🌳 fix: Preserve Primary Files Until Secondary Cleanup Succeeds (#17228)
* fix: Preserve primary files until secondary cleanup succeeds

* test: Use a flat deletion-strategy lookup

* fix: Preserve erasure retries and reconcile removed embeddings

* test: Match deletion regression formatting

* fix: Preserve search intent after vector cleanup

* fix: Repair retained project search files before tool execution

* fix: Normalize Retained Project File Runtime Types

* test: Use the Storage Source Enum in Project Repair Coverage

* fix: Confirm Secondary Cleanup and Preserve Repair Policy

* test: Correct Repair Fixture Types and Cleanup Retry Assertions

* fix: Reconcile Code Copies Compatibly and Charge Shared File Budgets

* fix: Reserve Admitted File Budgets Before Deferred Screening

* fix: Retain Concurrent File Copies and Scope Cleanup by Tenant

* fix: Bound Erasure File Checks and Correct Cleanup Fixtures

* fix: Preserve Provider-Only Assistant File Deletion

* fix: Reconcile Primary Vector Deletions Before Finalization

* fix: Finalize Successful Files in Mixed Cleanup Batches

* test: Load Real File Finalizer in Cleanup Integration Tests

* fix: Preserve Project Routing MIME and Fence File Storage Operations

* fix: Validate Provisioning Principal and Type Project Repair Fixtures

* fix: Recover Provisioning Claims and Retain Uncertain Storage Outcomes

* fix: Normalize File Recovery Results and Reconcile Transport Attempts

* test: Preserve Deployment Routes in Host Provisioning Fixtures

* fix: Recover Deletion Claims and Fence Generated Output Writes

* fix: Preserve Generated Output Commit Snapshots

* test: Use Persisted Owner Type in Generated File Fixture

* test: Type Generated Output Fixture with Its Commit Contract

* fix: Preserve Publication Error Handling and Cleanup Test Boundaries

---------

Co-authored-by: Lia <lia@librechat.ai>
2026-10-23 07:00:20 +02:00

142 lines
5.2 KiB
TypeScript

import type { FileStorageClaim } from '@librechat/data-schemas';
import type { FileStorageUploadObserver } from './operations';
import {
createCodeApiRateLimitBudget,
createCodeApiUploadRegistry,
withCodeApiUploadRecovery,
} from '../utils/code';
import { createFileProvisionRecovery, withFileDeletion, withFileProvisioning } from './operations';
import { dispatchFileDeletion, dispatchFileStorageOperation } from './dispatch';
function fixture() {
const claims = new Set<string>();
const storage = {
recovery: createFileProvisionRecovery(),
acquireFileProvision: jest.fn(async ({ token }: { token: string }) => {
claims.add(token);
return true;
}),
releaseFileProvision: jest.fn(async ({ token }: { token: string }) => {
claims.delete(token);
return true;
}),
commitFileProvision: jest.fn(async () => true),
acquireFileDeletion: jest.fn(async () => (claims.size ? null : { resumed: false })),
releaseFileDeletion: jest.fn(async () => true),
};
return { storage, file: { file_id: 'file', user: 'owner' }, claims };
}
it('marks each definitive rejection without treating a later canceled wait as an upload', async () => {
const { storage, file, claims } = fixture();
const controller = new AbortController();
const rejected = Object.assign(new Error('throttled'), {
isAxiosError: true,
response: { status: 429, headers: { 'retry-after': '1' } },
});
const remote = jest.fn(async () => {
throw rejected;
});
const operation = jest.fn(
async (_claim: FileStorageClaim, onUploadStart: FileStorageUploadObserver) => {
await withCodeApiUploadRecovery({
registry: createCodeApiUploadRegistry(),
scope: 'route:user',
label: 'upload',
budget: createCodeApiRateLimitBudget(10000),
signal: controller.signal,
onWait: () => controller.abort(),
openSource: async () => 'source',
upload: () => dispatchFileStorageOperation(onUploadStart, remote),
});
return { value: 'uploaded', commit: { embedded: true } };
},
);
await expect(
withFileProvisioning({ file, userId: 'owner', storage, destination: 'code', operation }),
).rejects.toMatchObject({ name: 'AbortError' });
expect(remote).toHaveBeenCalledTimes(1);
expect(claims.size).toBe(0);
await expect(
withFileDeletion({ file, userId: 'owner', storage, operation: async () => undefined }),
).resolves.toBeUndefined();
});
it('retains a later uncertain request even after an earlier rejected attempt', async () => {
const { storage, file, claims } = fixture();
const denied = Object.assign(new Error('throttled'), { response: { status: 429 } });
await expect(
withFileProvisioning({
file,
userId: 'owner',
storage,
destination: 'code',
operation: async (_claim, onStart) => {
await dispatchFileStorageOperation(onStart, async () => {
throw denied;
}).catch(() => undefined);
await dispatchFileStorageOperation(onStart, async () => {
throw new Error('response lost');
});
return { value: 'unused', commit: { embedded: true } };
},
}),
).rejects.toMatchObject({ name: 'FileProvisionOutcomeUnknownError' });
expect(claims.size).toBe(1);
expect(storage.releaseFileProvision).not.toHaveBeenCalled();
});
it('does not clear uncertainty after a successful transport response and failed result validation', async () => {
const { storage, file, claims } = fixture();
await expect(
withFileProvisioning({
file,
userId: 'owner',
storage,
destination: 'code',
operation: async (_claim, onStart) => {
await dispatchFileStorageOperation(onStart, async () => ({ status: 200 }));
throw new Error('malformed upload result');
},
}),
).rejects.toMatchObject({ name: 'FileProvisionOutcomeUnknownError' });
expect(claims.size).toBe(1);
});
describe('dispatchFileDeletion', () => {
it('marks the mutation before dispatch and confirms its result', async () => {
const onDelete = jest.fn();
const operation = jest.fn(async () => {
expect(onDelete.mock.calls).toEqual([[]]);
return 'deleted';
});
await expect(dispatchFileDeletion(onDelete, operation)).resolves.toBe('deleted');
expect(onDelete.mock.calls).toEqual([[], ['confirmed']]);
});
it('reports a definitive mutation rejection', async () => {
const onDelete = jest.fn();
const error = Object.assign(new Error('denied'), { $metadata: { httpStatusCode: 403 } });
await expect(
dispatchFileDeletion(onDelete, async () => {
throw error;
}),
).rejects.toBe(error);
expect(onDelete.mock.calls).toEqual([[], ['rejected']]);
});
it('leaves a dispatched mutation uncertain on a lost response', async () => {
const onDelete = jest.fn();
const error = Object.assign(new Error('response lost'), { code: 'ETIMEDOUT' });
await expect(
dispatchFileDeletion(onDelete, async () => {
throw error;
}),
).rejects.toBe(error);
expect(onDelete.mock.calls).toEqual([[]]);
});
it('supports callers without an observer', async () => {
await expect(dispatchFileDeletion(undefined, async () => true)).resolves.toBe(true);
});
});