|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + clearPendingUploads, |
| 4 | + getPendingUpload, |
| 5 | + getPendingUploadCount, |
| 6 | + removePendingUpload, |
| 7 | + storePendingUpload, |
| 8 | +} from "./pending-uploads.js"; |
| 9 | + |
| 10 | +describe("msteams pending uploads", () => { |
| 11 | + beforeEach(() => { |
| 12 | + vi.useFakeTimers(); |
| 13 | + clearPendingUploads(); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + clearPendingUploads(); |
| 18 | + vi.useRealTimers(); |
| 19 | + }); |
| 20 | + |
| 21 | + it("stores uploads, exposes them by id, and tracks count", () => { |
| 22 | + const id = storePendingUpload({ |
| 23 | + buffer: Buffer.from("hello"), |
| 24 | + filename: "hello.txt", |
| 25 | + contentType: "text/plain", |
| 26 | + conversationId: "conv-1", |
| 27 | + }); |
| 28 | + |
| 29 | + expect(getPendingUploadCount()).toBe(1); |
| 30 | + expect(getPendingUpload(id)).toEqual( |
| 31 | + expect.objectContaining({ |
| 32 | + id, |
| 33 | + filename: "hello.txt", |
| 34 | + contentType: "text/plain", |
| 35 | + conversationId: "conv-1", |
| 36 | + }), |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + it("removes uploads explicitly and ignores empty ids", () => { |
| 41 | + const id = storePendingUpload({ |
| 42 | + buffer: Buffer.from("hello"), |
| 43 | + filename: "hello.txt", |
| 44 | + conversationId: "conv-1", |
| 45 | + }); |
| 46 | + |
| 47 | + removePendingUpload(undefined); |
| 48 | + expect(getPendingUploadCount()).toBe(1); |
| 49 | + |
| 50 | + removePendingUpload(id); |
| 51 | + expect(getPendingUpload(id)).toBeUndefined(); |
| 52 | + expect(getPendingUploadCount()).toBe(0); |
| 53 | + }); |
| 54 | + |
| 55 | + it("expires uploads by ttl even if the timeout callback has not been observed yet", () => { |
| 56 | + const id = storePendingUpload({ |
| 57 | + buffer: Buffer.from("hello"), |
| 58 | + filename: "hello.txt", |
| 59 | + conversationId: "conv-1", |
| 60 | + }); |
| 61 | + |
| 62 | + vi.advanceTimersByTime(5 * 60 * 1000 + 1); |
| 63 | + |
| 64 | + expect(getPendingUpload(id)).toBeUndefined(); |
| 65 | + expect(getPendingUploadCount()).toBe(0); |
| 66 | + }); |
| 67 | + |
| 68 | + it("clears all uploads for test cleanup", () => { |
| 69 | + storePendingUpload({ |
| 70 | + buffer: Buffer.from("a"), |
| 71 | + filename: "a.txt", |
| 72 | + conversationId: "conv-1", |
| 73 | + }); |
| 74 | + storePendingUpload({ |
| 75 | + buffer: Buffer.from("b"), |
| 76 | + filename: "b.txt", |
| 77 | + conversationId: "conv-2", |
| 78 | + }); |
| 79 | + |
| 80 | + clearPendingUploads(); |
| 81 | + |
| 82 | + expect(getPendingUploadCount()).toBe(0); |
| 83 | + }); |
| 84 | +}); |
0 commit comments