Registry Mirror
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 156 additions & 2 deletions electron/ai-edition/project-recordings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { DocumentService } from "./document-service";
import { findProjectRecordings, removeRecordingSets } from "./project-recordings";
import {
findProjectRecordings,
listRecordings,
openProjectRecordingKeys,
removeRecordingSets,
resolveRecordingIds,
} from "./project-recordings";

async function touch(filePath: string, content = "x"): Promise<string> {
await fs.writeFile(filePath, content);
Expand All @@ -17,7 +23,10 @@ describe("project recordings", () => {
let documents: DocumentService;

beforeEach(async () => {
root = await fs.mkdtemp(path.join(os.tmpdir(), "openscreen-project-recordings-"));
// Real path: takes are keyed by where they really are, and the temp folder may be a link.
root = await fs.realpath(
await fs.mkdtemp(path.join(os.tmpdir(), "openscreen-project-recordings-")),
);
recordingsDir = path.join(root, "recordings");
elsewhere = path.join(root, "Desktop");
await fs.mkdir(recordingsDir);
Expand Down Expand Up @@ -137,4 +146,149 @@ describe("project recordings", () => {
await expect(fs.access(video)).rejects.toThrow();
await expect(fs.access(cursor)).rejects.toThrow();
});

describe("listing every take", () => {
async function at(filePath: string, isoDate: string): Promise<string> {
await touch(filePath);
const when = new Date(isoDate);
await fs.utimes(filePath, when, when);
return filePath;
}

it("lists each take once with its files, newest first, and who uses it", async () => {
const older = await at(path.join(recordingsDir, "recording-1.mp4"), "2026-01-01");
await at(`${older}.cursor.json`, "2026-01-01");
const newer = await at(path.join(recordingsDir, "recording-2.mp4"), "2026-02-01");
await touch(path.join(recordingsDir, "media-links.registry.json"));
await touch(path.join(recordingsDir, "notes.txt"));
await projectWith("Demo", older);
await projectWith("Demo v2", older);

const { takes, unreadable } = await listRecordings(
await documents.readAllProjectsReport(),
// The same folder twice, as when the current folder is the default one.
[recordingsDir, `${recordingsDir}${path.sep}`],
);

expect(takes.map((take) => take.stem)).toEqual(["recording-2", "recording-1"]);
expect(takes[0].usedBy).toEqual([]);
expect(takes[0].files).toEqual([newer]);
expect(takes[1].usedBy.sort()).toEqual(["Demo", "Demo v2"]);
expect(takes[1].files).toHaveLength(2);
expect(takes[1].id).toBe(path.join(recordingsDir, "recording-1"));
expect(unreadable).toBe(0);
});

it("counts project files that do not parse", async () => {
await touch(path.join(recordingsDir, "recording-3.mp4"));
await fs.mkdir(path.join(root, "projects"), { recursive: true });
await touch(path.join(root, "projects", "broken.openscreen"), "{ not json");

const { takes, unreadable } = await listRecordings(await documents.readAllProjectsReport(), [
recordingsDir,
]);

expect(takes).toHaveLength(1);
expect(unreadable).toBe(1);
});

it("marks the open project's takes, saved or only in its unsaved edits", async () => {
const saved = await touch(path.join(recordingsDir, "recording-5.mp4"));
const edited = await touch(path.join(recordingsDir, "recording-6.mp4"));
await touch(path.join(recordingsDir, "recording-7.mp4"));
const id = await projectWith("Open", saved);
// The unsaved edit swapped the video: the saved file still names the old take.
const open = structuredClone(await documents.getProject(id));
open.assets[0].originalPath = edited;

const { takes } = await listRecordings(
await documents.readAllProjectsReport(),
[recordingsDir],
open,
);

const locked = Object.fromEntries(takes.map((take) => [take.stem, take.inOpenProject]));
expect(locked).toEqual({ "recording-5": true, "recording-6": true, "recording-7": false });
});

it("marks nothing when no project is open", async () => {
const video = await touch(path.join(recordingsDir, "recording-8.mp4"));
await projectWith("Saved", video);

const { takes } = await listRecordings(await documents.readAllProjectsReport(), [
recordingsDir,
]);

expect(takes[0].inOpenProject).toBe(false);
});

it("treats a folder's alias as the folder, so the open project's take stays locked", async () => {
const alias = path.join(root, "alias");
// A junction on Windows needs no privilege; elsewhere the type is ignored.
await fs.symlink(recordingsDir, alias, "junction");
await touch(path.join(recordingsDir, "recording-3.mp4"));
// The project names the take through the alias.
const id = await projectWith("Open", path.join(alias, "recording-3.mp4"));
const open = await documents.getProject(id);
const dirs = [recordingsDir, alias];

const { takes } = await listRecordings(await documents.readAllProjectsReport(), dirs, open);
expect(takes).toHaveLength(1);
expect(takes[0]).toMatchObject({ usedBy: ["Open"], inOpenProject: true });

const locked = await openProjectRecordingKeys(open, [], dirs);
for (const named of [recordingsDir, alias]) {
await expect(
resolveRecordingIds([path.join(named, "recording-3")], dirs, locked),
).rejects.toThrow(/open project/);
}
});

it("skips a recordings folder that is gone", async () => {
const { takes } = await listRecordings(await documents.readAllProjectsReport(), [
path.join(root, "unplugged"),
]);

expect(takes).toEqual([]);
});
});

describe("resolving the ids a delete names", () => {
it("resolves a take in a recordings folder to its files", async () => {
const video = await touch(path.join(recordingsDir, "recording-4.mp4"));

const sets = await resolveRecordingIds(
[path.join(recordingsDir, "recording-4")],
[recordingsDir],
);

expect(sets.map((set) => set.files)).toEqual([[video]]);
});

it("refuses the whole request when one take is the open project's", async () => {
await touch(path.join(recordingsDir, "recording-4.mp4"));
await touch(path.join(recordingsDir, "recording-9.mp4"));
const lockedId = path.join(recordingsDir, "recording-9");

await expect(
resolveRecordingIds(
[path.join(recordingsDir, "recording-4"), lockedId],
[recordingsDir],
new Set([lockedId]),
),
).rejects.toThrow(/open project/);
});

it.each([
["a take outside the recordings folders", () => path.join(elsewhere, "recording-4")],
["a file name instead of a take", () => path.join(recordingsDir, "recording-4.mp4")],
["something that is not a take", () => path.join(recordingsDir, "media-links")],
["a path that climbs out", () => path.join(recordingsDir, "..", "recording-4")],
])("refuses %s", async (_label, id) => {
await touch(path.join(elsewhere, "recording-4.mp4"));
await touch(path.join(root, "recording-4.mp4"));

await expect(resolveRecordingIds([id()], [recordingsDir])).rejects.toThrow(/Not a recording/);
});
});
});
Loading
Loading