adjust attachments to hopefully fix them?
This commit is contained in:
@@ -47,3 +47,50 @@ test("composer preview, pending state, upload failure/retry, removal, paste, and
|
||||
assert.equal(history.children[0].children[0].href, `/attachments/${metadata.id}`);
|
||||
assert.ok(changes > 4);
|
||||
});
|
||||
|
||||
test("discarding a chat draft aborts upload, deletes temporary files, and serializes Retry", async t => {
|
||||
const originalDocument = globalThis.document, originalFetch = globalThis.fetch;
|
||||
t.after(() => { globalThis.document = originalDocument; globalThis.fetch = originalFetch; });
|
||||
const elements = Object.fromEntries(["attachImage", "imagePicker", "uploadPreviews", "composer"].map(id => [id, new Element()]));
|
||||
globalThis.document = {getElementById: id => elements[id], querySelector: () => elements.composer, createElement: () => new Element()};
|
||||
let uploads = 0;
|
||||
let aborted = false;
|
||||
const deletes = [];
|
||||
globalThis.fetch = (url, options = {}) => {
|
||||
if (url === "/uploads/config") return Promise.resolve(Response.json({maxFiles: 5, maxSizeMb: 10}));
|
||||
if (url.startsWith("/attachments/")) { deletes.push(url); return Promise.resolve(Response.json({success: true})); }
|
||||
uploads++;
|
||||
if (uploads === 1) return Promise.resolve(Response.json({error: "Try again"}, {status: 503}));
|
||||
if (uploads === 2) return new Promise((_resolve, reject) => options.signal.addEventListener("abort", () => {
|
||||
aborted = true; reject(new DOMException("Aborted", "AbortError"));
|
||||
}, {once: true}));
|
||||
return Promise.resolve(Response.json({id: `00000000-0000-0000-0000-${String(uploads).padStart(12, "0")}`}));
|
||||
};
|
||||
const composer = createAttachmentComposer({onChange() {}, onError: message => assert.fail(message)});
|
||||
const tick = () => new Promise(resolve => setTimeout(resolve, 0));
|
||||
const addFile = name => {
|
||||
elements.imagePicker.files = [new File(["fixture"], name, {type: "image/png"})];
|
||||
elements.imagePicker.onchange();
|
||||
};
|
||||
addFile("pending.png");
|
||||
await tick();
|
||||
const retry = elements.uploadPreviews.children[0].children[2];
|
||||
retry.onclick(); retry.onclick();
|
||||
assert.equal(uploads, 2, "Repeated Retry clicks start only one upload");
|
||||
composer.discard();
|
||||
await tick();
|
||||
assert.equal(aborted, true);
|
||||
assert.equal(composer.pending, false);
|
||||
assert.equal(elements.uploadPreviews.hidden, true);
|
||||
|
||||
addFile("temporary.png");
|
||||
await tick();
|
||||
const temporaryId = composer.attachments[0].id;
|
||||
composer.discard();
|
||||
assert.deepEqual(deletes, [`/attachments/${temporaryId}`]);
|
||||
|
||||
addFile("accepted.png");
|
||||
await tick();
|
||||
composer.clear();
|
||||
assert.deepEqual(deletes, [`/attachments/${temporaryId}`], "Sent attachments are retained");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user