Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
test: cover Loom importer progress webhook throttle
Richie McIlroy09be1c2dc180
diff --git a/apps/media-server/src/__tests__/loom-importer/progress-webhooks.test.ts b/apps/media-server/src/__tests__/loom-importer/progress-webhooks.test.tsnew file mode 100644index 000000000..66b300838--- /dev/null+++ b/apps/media-server/src/__tests__/loom-importer/progress-webhooks.test.ts@@ -0,0 +1,44 @@+import { describe, expect, test } from "bun:test";+import { ProgressWebhookThrottle } from "../../loom-importer/job";++describe("ProgressWebhookThrottle", () => {+ test("sends the first update for a job", () => {+ const throttle = new ProgressWebhookThrottle(15_000);+ expect(throttle.shouldSend("job-1", "downloading", 0)).toBe(true);+ });++ test("holds same-phase updates inside the interval", () => {+ const throttle = new ProgressWebhookThrottle(15_000);+ throttle.shouldSend("job-1", "downloading", 0);+ expect(throttle.shouldSend("job-1", "downloading", 1_000)).toBe(false);+ expect(throttle.shouldSend("job-1", "downloading", 14_999)).toBe(false);+ });++ test("sends again once the interval passes", () => {+ const throttle = new ProgressWebhookThrottle(15_000);+ throttle.shouldSend("job-1", "downloading", 0);+ expect(throttle.shouldSend("job-1", "downloading", 15_000)).toBe(true);+ expect(throttle.shouldSend("job-1", "downloading", 20_000)).toBe(false);+ expect(throttle.shouldSend("job-1", "downloading", 30_000)).toBe(true);+ });++ test("sends a phase change straight away", () => {+ const throttle = new ProgressWebhookThrottle(15_000);+ throttle.shouldSend("job-1", "downloading", 0);+ expect(throttle.shouldSend("job-1", "uploading", 1)).toBe(true);+ expect(throttle.shouldSend("job-1", "uploading", 2)).toBe(false);+ });++ test("tracks jobs separately", () => {+ const throttle = new ProgressWebhookThrottle(15_000);+ throttle.shouldSend("job-1", "downloading", 0);+ expect(throttle.shouldSend("job-2", "downloading", 1)).toBe(true);+ });++ test("forgets finished jobs", () => {+ const throttle = new ProgressWebhookThrottle(15_000);+ throttle.shouldSend("job-1", "downloading", 0);+ throttle.forget("job-1");+ expect(throttle.shouldSend("job-1", "downloading", 1)).toBe(true);+ });+});diff --git a/apps/media-server/src/loom-importer/job.ts b/apps/media-server/src/loom-importer/job.tsindex 8b335c91b..2d34c8ae6 100644--- a/apps/media-server/src/loom-importer/job.ts+++ b/apps/media-server/src/loom-importer/job.ts@@ -140,8 +140,25 @@ async function deliverTerminalWebhook(job: Job, attempts: number) {);}-const PROGRESS_WEBHOOK_INTERVAL_MS = 15_000;-const lastProgressWebhook = new Map<string, { at: number; phase: string }>();+export class ProgressWebhookThrottle {+ private readonly last = new Map<string, { at: number; phase: string }>();++ constructor(private readonly intervalMs: number) {}++ shouldSend(jobId: string, phase: string, now: number) {+ const last = this.last.get(jobId);+ if (last && last.phase === phase && now - last.at < this.intervalMs)+ return false;+ this.last.set(jobId, { at: now, phase });+ return true;+ }++ forget(jobId: string) {+ this.last.delete(jobId);+ }+}++const progressWebhooks = new ProgressWebhookThrottle(15_000);function progressUpdate(jobId: string,@@ -150,21 +167,8 @@ function progressUpdate(message: string,) {const job = updateJob(jobId, { phase, progress, message });- if (!job) return;- const now = Date.now();- const last = lastProgressWebhook.get(jobId);- if (- last &&- last.phase === phase &&- now - last.at < PROGRESS_WEBHOOK_INTERVAL_MS- )- return;- lastProgressWebhook.set(jobId, { at: now, phase });- void sendWebhook(job);-}--function forgetProgressWebhooks(jobId: string) {- lastProgressWebhook.delete(jobId);+ if (job && progressWebhooks.shouldSend(jobId, phase, Date.now()))+ void sendWebhook(job);}interface PreparedSource {@@ -446,7 +450,7 @@ export async function runLoomImport(if (failed)await deliverTerminalWebhook(failed, settings.terminalWebhookAttempts);} finally {- forgetProgressWebhooks(job.jobId);+ progressWebhooks.forget(job.jobId);if (workDir)await rm(workDir, { recursive: true, force: true }).catch(() => {});}