Files
ihaleist/tests/document-file-failures.test.mjs
T

42 lines
2.0 KiB
JavaScript

import { test, after } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
const dir = fs.mkdtempSync(join(tmpdir(),'ihaleist-doc-failure-'));
process.env.IHALEIST_USER_DATA_DIR = dir;
const store = await import('../server/user-store.mjs');
after(()=>fs.rmSync(dir,{recursive:true,force:true}));
test('failed document insert removes only the new upload and preserves previous files',()=>{
const bytes=Buffer.from('Özel test belgesi');
const id=store.saveDocumentFile('a',{ad:'test.txt',mime:'text/plain'},bytes);
const record=store.getDocument('a',id);
const before=fs.readdirSync(store.BELGE_DIR);
const db=store.openDb();
db.exec("CREATE TRIGGER reject_document BEFORE INSERT ON belgeler BEGIN SELECT RAISE(FAIL,'test insert rejected'); END;");
db.close();
assert.throws(()=>store.saveDocumentFile('a',{ad:'failed.txt'},bytes),/test insert rejected/);
assert.deepEqual(fs.readdirSync(store.BELGE_DIR),before);
assert.deepEqual(fs.readFileSync(join(store.BELGE_DIR,record.dosya)),bytes);
assert.equal(store.listDocuments('a').length,1);
});
test('filesystem cleanup failure keeps a durable deletion task and denies document access',()=>{
const destination=join(store.BELGE_DIR,'not-a-file');
fs.mkdirSync(destination);
const db=store.openDb(); db.exec('DROP TRIGGER reject_document'); db.close();
const id=store.addDocument('a',{ad:'test',boyut:1,dosya:'not-a-file'});
assert.equal(store.deleteDocument('b',id),false);
assert.ok(store.getDocument('a',id));
assert.equal(store.deleteDocument('a',id),true);
assert.equal(store.getDocument('a',id),null);
const pending=store.openDb();
assert.ok(pending.prepare('SELECT 1 FROM document_file_cleanup WHERE dosya=?').get('not-a-file'));
pending.close();
fs.rmdirSync(destination);
const recovered=store.openDb();
assert.equal(recovered.prepare('SELECT COUNT(*) n FROM document_file_cleanup').get().n,0);
recovered.close();
});