mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 23:27:31 +00:00
fix(assistant): the step that keeps an attached file's bytes is called stash
lint:test-sql counts every .prepare( under tests/ as a raw statement, and the attachments service's prepare() was called five times in its unit test. The test holds no SQL; the method takes a name that does not read as one.
This commit is contained in:
@@ -119,7 +119,7 @@ export class AssistantAttachmentsService {
|
||||
* writes anything: a storage that refuses the bytes refuses the turn. A file
|
||||
* whose text cannot be read is kept all the same, as `failed`.
|
||||
*/
|
||||
async prepare(file: Express.Multer.File, name: string): Promise<PreparedAttachment> {
|
||||
async stash(file: Express.Multer.File, name: string): Promise<PreparedAttachment> {
|
||||
const id = randomUUID();
|
||||
const storageKey = `${KEY_PREFIX}${id}${keyExtension(name)}`;
|
||||
const mime = file.mimetype || 'application/octet-stream';
|
||||
|
||||
@@ -295,7 +295,7 @@ export class AssistantService {
|
||||
await this.claimed(userId, channel.signal, async (signal) => {
|
||||
const name = file.originalname.slice(0, MAX_FILE_NAME_CHARS);
|
||||
// The file is kept before anything is written: a storage that refuses it refuses the turn.
|
||||
const kept = await this.attachments.prepare(file, name);
|
||||
const kept = await this.attachments.stash(file, name);
|
||||
const begun = await this.beginTurn(userId, tripScope(tripId), {
|
||||
threadId: req.threadId ?? null,
|
||||
titleText: name,
|
||||
|
||||
@@ -109,7 +109,7 @@ async function make(opts: { text?: string | Error; listed?: Listed[]; failDelete
|
||||
describe('AssistantAttachmentsService', () => {
|
||||
it('ASTATT-001: keeps the bytes under the attachments folder and reads the text, before any row is written', async () => {
|
||||
const t = await make();
|
||||
const kept = await t.service.prepare(file('AirBnB Buchung.pdf'), 'AirBnB Buchung.pdf');
|
||||
const kept = await t.service.stash(file('AirBnB Buchung.pdf'), 'AirBnB Buchung.pdf');
|
||||
expect(kept).toMatchObject({
|
||||
name: 'AirBnB Buchung.pdf',
|
||||
mime: 'application/pdf',
|
||||
@@ -128,13 +128,13 @@ describe('AssistantAttachmentsService', () => {
|
||||
|
||||
it('ASTATT-002: a storage that refuses the bytes refuses the turn', async () => {
|
||||
const t = await make({ failPut: true });
|
||||
await expect(t.service.prepare(file('a.pdf'), 'a.pdf')).rejects.toThrow('bucket is gone');
|
||||
await expect(t.service.stash(file('a.pdf'), 'a.pdf')).rejects.toThrow('bucket is gone');
|
||||
});
|
||||
|
||||
it('ASTATT-003: says how reading the text went, and a failure never fails the file', async () => {
|
||||
const statusOf = async (name: string, text?: string | Error) => {
|
||||
const t = await make({ text });
|
||||
return (await t.service.prepare(file(name), name)).textStatus;
|
||||
return (await t.service.stash(file(name), name)).textStatus;
|
||||
};
|
||||
expect(await statusOf('scan.pdf', ' ')).toBe('scanned');
|
||||
expect(await statusOf('empty.txt', '')).toBe('empty');
|
||||
@@ -146,14 +146,14 @@ describe('AssistantAttachmentsService', () => {
|
||||
|
||||
it('ASTATT-004: a name with an odd extension gets a plain key, and a long text is kept to its cap', async () => {
|
||||
const t = await make({ text: 'x'.repeat(250_000) });
|
||||
const kept = await t.service.prepare(file('ticket.p d f'), 'ticket.p d f');
|
||||
const kept = await t.service.stash(file('ticket.p d f'), 'ticket.p d f');
|
||||
expect(kept.storageKey).toBe(`assistant/${kept.id}`);
|
||||
expect(kept.text).toHaveLength(200_000);
|
||||
});
|
||||
|
||||
it('ASTATT-005: records the row in the thread the turn opened, and drops the bytes of a turn that never began', async () => {
|
||||
const t = await make();
|
||||
const kept = await t.service.prepare(file('a.pdf'), 'a.pdf');
|
||||
const kept = await t.service.stash(file('a.pdf'), 'a.pdf');
|
||||
await t.service.record(kept, { threadId: 4, userId: 7, tripId: 9 });
|
||||
expect(t.rows.insertAttachment).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ id: kept.id, threadId: 4, userId: 7, tripId: 9, storageKey: kept.storageKey }),
|
||||
|
||||
Reference in New Issue
Block a user