summaryrefslogtreecommitdiff
path: root/apps/web/src/project/transfer/__tests__/migrate.test.ts
blob: af4bb37ec66260701a3b4b5b07277ee6340c44f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { describe, expect, test } from "bun:test";
import {
	StorageMigration,
	type StorageMigrationRunArgs,
} from "@/services/storage/migrations/base";
import type {
	MigrationResult,
	ProjectRecord,
} from "@/services/storage/migrations/transformers/types";
import {
	CURRENT_PROJECT_VERSION,
	migrations,
} from "@/services/storage/migrations";
import { v5Project } from "@/services/storage/migrations/__tests__/fixtures";
import { getRecordVersion, migrateProjectRecord } from "../migrate";

class StepMigration extends StorageMigration {
	from: number;
	to: number;
	private transform: ({ project }: { project: ProjectRecord }) => ProjectRecord;
	private shouldSkip: boolean;

	constructor({
		from,
		to,
		transform,
		shouldSkip = false,
	}: {
		from: number;
		to: number;
		transform: ({ project }: { project: ProjectRecord }) => ProjectRecord;
		shouldSkip?: boolean;
	}) {
		super();
		this.from = from;
		this.to = to;
		this.transform = transform;
		this.shouldSkip = shouldSkip;
	}

	async run({
		project,
	}: StorageMigrationRunArgs): Promise<MigrationResult<ProjectRecord>> {
		if (this.shouldSkip) {
			return { project, skipped: true, reason: "skip requested" };
		}
		return { project: this.transform({ project }), skipped: false };
	}
}

function setVersion({
	project,
	version,
}: {
	project: ProjectRecord;
	version: number;
}): ProjectRecord {
	return { ...project, version };
}

describe("getRecordVersion", () => {
	test("uses the explicit version field", () => {
		expect(getRecordVersion({ project: { version: 7 } })).toBe(7);
	});

	test("detects v1 projects by their scenes array", () => {
		expect(getRecordVersion({ project: { scenes: [{ id: "s" }] } })).toBe(1);
	});

	test("falls back to v0", () => {
		expect(getRecordVersion({ project: {} })).toBe(0);
		expect(getRecordVersion({ project: { scenes: [] } })).toBe(0);
	});
});

describe("migrateProjectRecord", () => {
	test("applies migrations in version order regardless of registration order", async () => {
		const registry = [
			new StepMigration({
				from: 1,
				to: 2,
				transform: ({ project }) => setVersion({ project, version: 2 }),
			}),
			new StepMigration({
				from: 0,
				to: 1,
				transform: ({ project }) => setVersion({ project, version: 1 }),
			}),
		];

		const result = await migrateProjectRecord({
			project: { version: 0, steps: [] },
			migrations: registry,
		});

		expect(result.fromVersion).toBe(0);
		expect(result.toVersion).toBe(2);
		expect(result.complete).toBe(true);
		expect(result.project.version).toBe(2);
	});

	test("reports incomplete when a migration skips", async () => {
		const registry = [
			new StepMigration({
				from: 0,
				to: 1,
				transform: ({ project }) => project,
				shouldSkip: true,
			}),
			new StepMigration({
				from: 1,
				to: 2,
				transform: ({ project }) => setVersion({ project, version: 2 }),
			}),
		];

		const result = await migrateProjectRecord({
			project: { version: 0 },
			migrations: registry,
		});

		expect(result.toVersion).toBe(0);
		expect(result.complete).toBe(false);
	});

	test("is a no-op for current-version documents", async () => {
		const registry = [
			new StepMigration({
				from: 0,
				to: 1,
				transform: ({ project }) => setVersion({ project, version: 1 }),
			}),
		];

		const project = { version: 1, marker: "untouched" };
		const result = await migrateProjectRecord({ project, migrations: registry });

		expect(result.complete).toBe(true);
		expect(result.project).toBe(project);
	});

	test("migrates a real v5 fixture to the current version with the app registry", async () => {
		const result = await migrateProjectRecord({
			project: structuredClone(v5Project),
			migrations,
		});

		expect(result.fromVersion).toBe(5);
		expect(result.complete).toBe(true);
		expect(result.toVersion).toBe(CURRENT_PROJECT_VERSION);
		expect(result.project.version).toBe(CURRENT_PROJECT_VERSION);
	});
});