LibreChat/api/server/services/Files/Local/__tests__/crud-delete.spec.js
lia-by-librechat[bot] 94fed5bcd5
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Votes / vote (full suite) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
🥡 fix: Delete Storage and Embeddings With Owned Agent Files (#16007)
The agent tool-resource branch of DELETE /files removed a file's association
from the agent and answered 200 without reaching processDeleteRequest, so the
bytes on disk and the RAG chunks of a file the caller owned survived the delete.
A replaced File Search document kept answering from its old chunks (#15930).

An attached file now goes through the full delete pass, but only when nothing
else holds it: a file the caller does not own, and a file any other
(agent, tool_resource) pair still references, are unlinked and left whole.
Nothing is unlinked until the destroy it depends on has succeeded.

- packages/api/src/files/deletion.ts: deleteAgentResourceFiles owns the
  operation with its db methods and delete pass injected.
- packages/data-schemas: getSharedResourceFileIds answers the last-reference
  question, excluding the pair being removed by the agent's _id.
- Local/crud.js: unlinkFile propagates non-ENOENT, so a file whose bytes
  survive is no longer reported as deleted.

Co-authored-by: Mohammed Alshyakh <mohmedmm@users.noreply.github.com>
2026-09-17 02:59:25 +00:00

70 lines
2.5 KiB
JavaScript

/** Exactly what `deleteLocalFile` reaches for, so the double does not depend on a built package. */
jest.mock('@librechat/api', () => ({
deleteRagFile: jest.fn().mockResolvedValue(undefined),
stripCacheBust: (filepath) => String(filepath).split('?')[0],
}));
jest.mock('@librechat/data-schemas', () => ({
logger: { warn: jest.fn(), error: jest.fn() },
}));
const fs = require('fs');
const os = require('os');
const path = require('path');
const { deleteLocalFile } = require('../crud');
/* The resolved promise of a delete is what `processDeleteRequest` reads to decide that a record may
lose its metadata and its agent references, so this adapter may only resolve once the bytes are
actually gone. Storage that was already missing is the one benign case. */
describe('deleteLocalFile failure reporting', () => {
const userId = 'user-1';
let tmpBase;
let req;
beforeEach(() => {
jest.restoreAllMocks();
tmpBase = fs.mkdtempSync(path.join(os.tmpdir(), 'crud-delete-'));
fs.mkdirSync(path.join(tmpBase, 'uploads', userId), { recursive: true });
req = {
user: { id: userId },
config: {
paths: {
publicPath: path.join(tmpBase, 'public'),
uploads: path.join(tmpBase, 'uploads'),
},
},
};
});
afterEach(() => {
fs.rmSync(tmpBase, { recursive: true, force: true });
});
const uploadedFile = (filename) => {
const filepath = path.join(tmpBase, 'uploads', userId, filename);
fs.writeFileSync(filepath, 'contents');
return { file_id: 'file-1', filepath: `/uploads/${userId}/${filename}` };
};
it('removes the file and resolves', async () => {
const file = uploadedFile('knowledge.txt');
await expect(deleteLocalFile(req, file)).resolves.toBeUndefined();
expect(fs.existsSync(path.join(tmpBase, 'uploads', userId, 'knowledge.txt'))).toBe(false);
});
it('resolves when the file is already gone', async () => {
await expect(
deleteLocalFile(req, { file_id: 'file-1', filepath: `/uploads/${userId}/missing.txt` }),
).resolves.toBeUndefined();
});
it('rejects when the bytes survive the delete', async () => {
const file = uploadedFile('locked.txt');
jest
.spyOn(fs.promises, 'unlink')
.mockRejectedValue(Object.assign(new Error('permission denied'), { code: 'EACCES' }));
await expect(deleteLocalFile(req, file)).rejects.toThrow('permission denied');
expect(fs.existsSync(path.join(tmpBase, 'uploads', userId, 'locked.txt'))).toBe(true);
});
});