|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { DocumentService } from "./document-service"; |
| 6 | +import { findProjectRecordings, removeRecordingSets } from "./project-recordings"; |
| 7 | + |
| 8 | +async function touch(filePath: string, content = "x"): Promise<string> { |
| 9 | + await fs.writeFile(filePath, content); |
| 10 | + return filePath; |
| 11 | +} |
| 12 | + |
| 13 | +describe("project recordings", () => { |
| 14 | + let root: string; |
| 15 | + let recordingsDir: string; |
| 16 | + let elsewhere: string; |
| 17 | + let documents: DocumentService; |
| 18 | + |
| 19 | + beforeEach(async () => { |
| 20 | + root = await fs.mkdtemp(path.join(os.tmpdir(), "openscreen-project-recordings-")); |
| 21 | + recordingsDir = path.join(root, "recordings"); |
| 22 | + elsewhere = path.join(root, "Desktop"); |
| 23 | + await fs.mkdir(recordingsDir); |
| 24 | + await fs.mkdir(elsewhere); |
| 25 | + documents = new DocumentService(path.join(root, "projects"), root); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(async () => { |
| 29 | + await fs.rm(root, { recursive: true, force: true }); |
| 30 | + }); |
| 31 | + |
| 32 | + async function projectWith(title: string, ...videoPaths: string[]): Promise<string> { |
| 33 | + const doc = await documents.createProject(title); |
| 34 | + for (const videoPath of videoPaths) { |
| 35 | + await documents.addAsset(doc.project.id, { |
| 36 | + path: videoPath, |
| 37 | + label: path.basename(videoPath), |
| 38 | + }); |
| 39 | + } |
| 40 | + return doc.project.id; |
| 41 | + } |
| 42 | + |
| 43 | + it("owns every file recorded beside the screen video", async () => { |
| 44 | + const video = await touch(path.join(recordingsDir, "recording-12.mp4"), "12345"); |
| 45 | + const companions = [ |
| 46 | + await touch(`${video}.cursor.json`), |
| 47 | + await touch(path.join(recordingsDir, "recording-12.session.json")), |
| 48 | + await touch(path.join(recordingsDir, "recording-12-webcam.webm")), |
| 49 | + ]; |
| 50 | + await touch(path.join(recordingsDir, "recording-123.mp4")); |
| 51 | + const id = await projectWith("Mine", video); |
| 52 | + |
| 53 | + const { owned, keptFor } = await findProjectRecordings(id, await documents.readAllProjects(), [ |
| 54 | + recordingsDir, |
| 55 | + ]); |
| 56 | + |
| 57 | + expect(owned).toHaveLength(1); |
| 58 | + expect(owned[0].files.sort()).toEqual([video, ...companions].sort()); |
| 59 | + expect(owned[0].sizeBytes).toBe(5 + 3); |
| 60 | + expect(keptFor).toEqual([]); |
| 61 | + }); |
| 62 | + |
| 63 | + it("keeps a recording another project uses, and names that project", async () => { |
| 64 | + const shared = await touch(path.join(recordingsDir, "recording-1.mp4")); |
| 65 | + const own = await touch(path.join(recordingsDir, "recording-2.mp4")); |
| 66 | + const id = await projectWith("Mine", shared, own); |
| 67 | + await projectWith("Demo v2", shared); |
| 68 | + |
| 69 | + const { owned, keptFor } = await findProjectRecordings(id, await documents.readAllProjects(), [ |
| 70 | + recordingsDir, |
| 71 | + ]); |
| 72 | + |
| 73 | + expect(owned.map((set) => set.stem)).toEqual(["recording-2"]); |
| 74 | + expect(keptFor).toEqual(["Demo v2"]); |
| 75 | + }); |
| 76 | + |
| 77 | + it("never owns a video outside the recordings folders", async () => { |
| 78 | + const imported = await touch(path.join(elsewhere, "recording-5.mp4")); |
| 79 | + const id = await projectWith("Imported", imported); |
| 80 | + |
| 81 | + const { owned } = await findProjectRecordings(id, await documents.readAllProjects(), [ |
| 82 | + recordingsDir, |
| 83 | + ]); |
| 84 | + |
| 85 | + expect(owned).toEqual([]); |
| 86 | + }); |
| 87 | + |
| 88 | + it("trashes each file, or removes it for good when asked", async () => { |
| 89 | + const video = await touch(path.join(recordingsDir, "recording-7.mp4")); |
| 90 | + const cursor = await touch(`${video}.cursor.json`); |
| 91 | + const id = await projectWith("Mine", video); |
| 92 | + const { owned } = await findProjectRecordings(id, await documents.readAllProjects(), [ |
| 93 | + recordingsDir, |
| 94 | + ]); |
| 95 | + |
| 96 | + const trashItem = vi.fn(async (_filePath: string) => {}); |
| 97 | + await removeRecordingSets(owned, "trash", trashItem); |
| 98 | + expect(trashItem.mock.calls.map(([p]) => p).sort()).toEqual([video, cursor].sort()); |
| 99 | + await expect(fs.access(video)).resolves.toBeUndefined(); |
| 100 | + |
| 101 | + await removeRecordingSets(owned, "permanent", trashItem); |
| 102 | + await expect(fs.access(video)).rejects.toThrow(); |
| 103 | + await expect(fs.access(cursor)).rejects.toThrow(); |
| 104 | + }); |
| 105 | +}); |
0 commit comments