ic-mops 2.9.0 → 2.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/bundle/cli.tgz +0 -0
  3. package/cli.ts +34 -10
  4. package/commands/build.ts +16 -22
  5. package/commands/check-stable.ts +141 -17
  6. package/commands/check.ts +195 -101
  7. package/commands/migrate.ts +165 -0
  8. package/declarations/main/main.did +38 -0
  9. package/declarations/main/main.did.d.ts +36 -0
  10. package/declarations/main/main.did.js +36 -0
  11. package/dist/cli.js +28 -10
  12. package/dist/commands/build.js +7 -13
  13. package/dist/commands/check-stable.d.ts +8 -1
  14. package/dist/commands/check-stable.js +101 -19
  15. package/dist/commands/check.d.ts +1 -1
  16. package/dist/commands/check.js +136 -78
  17. package/dist/commands/migrate.d.ts +2 -0
  18. package/dist/commands/migrate.js +104 -0
  19. package/dist/declarations/main/main.did +38 -0
  20. package/dist/declarations/main/main.did.d.ts +36 -0
  21. package/dist/declarations/main/main.did.js +36 -0
  22. package/dist/helpers/migrations.d.ts +10 -0
  23. package/dist/helpers/migrations.js +109 -0
  24. package/dist/helpers/resolve-canisters.d.ts +3 -1
  25. package/dist/helpers/resolve-canisters.js +34 -5
  26. package/dist/package.json +1 -1
  27. package/dist/tests/check-stable.test.js +18 -0
  28. package/dist/tests/check.test.js +23 -5
  29. package/dist/tests/migrate.test.d.ts +1 -0
  30. package/dist/tests/migrate.test.js +160 -0
  31. package/dist/types.d.ts +7 -0
  32. package/helpers/migrations.ts +166 -0
  33. package/helpers/resolve-canisters.ts +53 -5
  34. package/package.json +1 -1
  35. package/tests/__snapshots__/check.test.ts.snap +2 -2
  36. package/tests/__snapshots__/migrate.test.ts.snap +119 -0
  37. package/tests/check/canisters-canister-args/Warning.mo +5 -0
  38. package/tests/check/canisters-canister-args/mops.toml +9 -0
  39. package/tests/check-stable/canister-args/migrations/20250101_000000_Init.mo +8 -0
  40. package/tests/check-stable/canister-args/migrations/20250201_000000_AddField.mo +9 -0
  41. package/tests/check-stable/canister-args/mops.toml +9 -0
  42. package/tests/check-stable/canister-args/old.most +8 -0
  43. package/tests/check-stable/canister-args/src/main.mo +11 -0
  44. package/tests/check-stable.test.ts +21 -0
  45. package/tests/check.test.ts +26 -5
  46. package/tests/migrate/basic/deployed.most +12 -0
  47. package/tests/migrate/basic/migrations/20250101_000000_Init.mo +5 -0
  48. package/tests/migrate/basic/migrations/20250201_000000_AddName.mo +5 -0
  49. package/tests/migrate/basic/migrations/20250301_000000_AddEmail.mo +9 -0
  50. package/tests/migrate/basic/mops.toml +15 -0
  51. package/tests/migrate/basic/next-migration/.gitkeep +0 -0
  52. package/tests/migrate/basic/src/main.mo +11 -0
  53. package/tests/migrate/with-next/deployed.most +12 -0
  54. package/tests/migrate/with-next/migrations/20250101_000000_Init.mo +5 -0
  55. package/tests/migrate/with-next/migrations/20250201_000000_AddName.mo +5 -0
  56. package/tests/migrate/with-next/migrations/20250301_000000_AddEmail.mo +9 -0
  57. package/tests/migrate/with-next/mops.toml +15 -0
  58. package/tests/migrate/with-next/next-migration/20250401_000000_RenameId.mo +9 -0
  59. package/tests/migrate/with-next/src/main.mo +11 -0
  60. package/tests/migrate.test.ts +228 -0
  61. package/types.ts +8 -0
@@ -0,0 +1,160 @@
1
+ import { describe, expect, test, afterEach } from "@jest/globals";
2
+ import { readdirSync, readFileSync } from "node:fs";
3
+ import { cp, rm, writeFile } from "node:fs/promises";
4
+ import path from "path";
5
+ import { cli, cliSnapshot, normalizePaths } from "./helpers";
6
+ const normalizeTimestamp = (text) => text.replace(/\d{8}_\d{6}/g, "<TIMESTAMP>");
7
+ const fixturesDir = path.join(import.meta.dirname, "migrate");
8
+ describe("migrate", () => {
9
+ const tempDirs = [];
10
+ async function makeTempFixture(fixture) {
11
+ const src = path.join(fixturesDir, fixture);
12
+ const dest = path.join(fixturesDir, `_tmp_${fixture}_${Date.now()}`);
13
+ await cp(src, dest, { recursive: true });
14
+ tempDirs.push(dest);
15
+ return dest;
16
+ }
17
+ async function patchMigrations(cwd, extra) {
18
+ const tomlPath = path.join(cwd, "mops.toml");
19
+ const toml = readFileSync(tomlPath, "utf-8");
20
+ await writeFile(tomlPath, toml.replace('next = "next-migration"', `next = "next-migration"\n${extra}`));
21
+ }
22
+ afterEach(async () => {
23
+ for (const dir of tempDirs) {
24
+ await rm(dir, { recursive: true, force: true });
25
+ }
26
+ tempDirs.length = 0;
27
+ });
28
+ describe("migrate new", () => {
29
+ test("creates a migration file with timestamp and template", async () => {
30
+ const cwd = await makeTempFixture("basic");
31
+ const result = await cli(["migrate", "new", "AddPhone"], { cwd });
32
+ expect(result.exitCode).toBe(0);
33
+ const nextDir = path.join(cwd, "next-migration");
34
+ const files = readdirSync(nextDir).filter((f) => f.endsWith(".mo"));
35
+ expect(files).toHaveLength(1);
36
+ expect(files[0]).toMatch(/^\d{8}_\d{6}_AddPhone\.mo$/);
37
+ const content = readFileSync(path.join(nextDir, files[0]), "utf-8");
38
+ expect({
39
+ exitCode: result.exitCode,
40
+ stdout: normalizeTimestamp(normalizePaths(result.stdout)),
41
+ stderr: normalizePaths(result.stderr),
42
+ template: content,
43
+ }).toMatchSnapshot();
44
+ });
45
+ test("errors when next already has a file", async () => {
46
+ const cwd = await makeTempFixture("basic");
47
+ await cli(["migrate", "new", "First"], { cwd });
48
+ const result = await cli(["migrate", "new", "Second"], { cwd });
49
+ expect(result.exitCode).toBe(1);
50
+ expect(result.stderr).toMatch(/next migration already exists/i);
51
+ });
52
+ test("errors on invalid migration name", async () => {
53
+ const cwd = await makeTempFixture("basic");
54
+ for (const name of ["../evil", "has space", "123start", "foo/bar"]) {
55
+ const result = await cli(["migrate", "new", name], { cwd });
56
+ expect(result.exitCode).toBe(1);
57
+ expect(result.stderr).toMatch(/invalid migration name/i);
58
+ }
59
+ });
60
+ test("errors when [migrations] not configured", async () => {
61
+ const cwd = await makeTempFixture("basic");
62
+ const tomlPath = path.join(cwd, "mops.toml");
63
+ const toml = readFileSync(tomlPath, "utf-8");
64
+ await writeFile(tomlPath, toml.replace(/\[canisters\.backend\.migrations\][\s\S]*$/, ""));
65
+ const result = await cli(["migrate", "new", "Test"], { cwd });
66
+ expect(result.exitCode).toBe(1);
67
+ expect(result.stderr).toMatch(/\[migrations\]/i);
68
+ });
69
+ });
70
+ describe("migrate freeze", () => {
71
+ test("moves the file from next to chain", async () => {
72
+ const cwd = await makeTempFixture("with-next");
73
+ const result = await cli(["migrate", "freeze"], { cwd });
74
+ expect(result.exitCode).toBe(0);
75
+ const nextFiles = readdirSync(path.join(cwd, "next-migration")).filter((f) => f.endsWith(".mo"));
76
+ expect(nextFiles).toHaveLength(0);
77
+ const chainFiles = readdirSync(path.join(cwd, "migrations")).filter((f) => f.endsWith(".mo"));
78
+ expect(chainFiles).toContain("20250401_000000_RenameId.mo");
79
+ expect({
80
+ exitCode: result.exitCode,
81
+ stdout: normalizePaths(result.stdout),
82
+ stderr: normalizePaths(result.stderr),
83
+ }).toMatchSnapshot();
84
+ });
85
+ test("errors when next is empty", async () => {
86
+ const cwd = await makeTempFixture("basic");
87
+ const result = await cli(["migrate", "freeze"], { cwd });
88
+ expect(result.exitCode).toBe(1);
89
+ expect(result.stderr).toMatch(/no next migration/i);
90
+ });
91
+ test("errors when next-migration does not sort last", async () => {
92
+ const cwd = await makeTempFixture("basic");
93
+ await writeFile(path.join(cwd, "next-migration", "00000000_000000_Early.mo"), "module {\n public func migration(_ : {}) : {} {\n {}\n }\n}\n");
94
+ const result = await cli(["migrate", "freeze"], { cwd });
95
+ expect(result.exitCode).toBe(1);
96
+ expect(result.stderr).toMatch(/must sort after/i);
97
+ });
98
+ });
99
+ describe("check", () => {
100
+ test("check fails without next migration, passes with it", async () => {
101
+ const cwd = await makeTempFixture("with-next");
102
+ const nextFile = readdirSync(path.join(cwd, "next-migration")).find((f) => f.endsWith(".mo"));
103
+ const nextPath = path.join(cwd, "next-migration", nextFile);
104
+ const nextContent = readFileSync(nextPath, "utf-8");
105
+ await rm(nextPath);
106
+ await cliSnapshot(["check"], { cwd }, 1);
107
+ await writeFile(nextPath, nextContent);
108
+ await cliSnapshot(["check"], { cwd }, 0);
109
+ });
110
+ test("check with trimming shows reduced chain", async () => {
111
+ const cwd = await makeTempFixture("basic");
112
+ await patchMigrations(cwd, "check-limit = 2");
113
+ await cliSnapshot(["check", "--verbose"], { cwd }, 0);
114
+ });
115
+ });
116
+ describe("build", () => {
117
+ test("build produces .most with full migration chain", async () => {
118
+ const cwd = await makeTempFixture("basic");
119
+ const result = await cli(["build"], { cwd });
120
+ expect(result.exitCode).toBe(0);
121
+ const most = readFileSync(path.join(cwd, ".mops", ".build", "backend.most"), "utf-8");
122
+ expect(most).toMatchSnapshot();
123
+ });
124
+ test("build with build-limit produces trimmed .most", async () => {
125
+ const cwd = await makeTempFixture("basic");
126
+ await patchMigrations(cwd, "build-limit = 2");
127
+ const result = await cli(["build"], { cwd });
128
+ expect(result.exitCode).toBe(0);
129
+ const most = readFileSync(path.join(cwd, ".mops", ".build", "backend.most"), "utf-8");
130
+ expect(most).toMatchSnapshot();
131
+ });
132
+ test("build-limit counts next migration as part of the chain", async () => {
133
+ const cwd = await makeTempFixture("with-next");
134
+ await patchMigrations(cwd, "build-limit = 2");
135
+ const result = await cli(["build"], { cwd });
136
+ expect(result.exitCode).toBe(0);
137
+ const most = readFileSync(path.join(cwd, ".mops", ".build", "backend.most"), "utf-8");
138
+ expect(most).toMatchSnapshot();
139
+ });
140
+ });
141
+ describe("stable check hint", () => {
142
+ test("stable check fails with hint when deployed.most is incompatible", async () => {
143
+ const cwd = await makeTempFixture("basic");
144
+ await writeFile(path.join(cwd, "deployed.most"), "// Version: 1.0.0\nactor {\n stable var a : Nat;\n stable var name : Int\n};\n");
145
+ await cliSnapshot(["check"], { cwd }, 1);
146
+ });
147
+ });
148
+ describe("conflict detection", () => {
149
+ test("errors when both [migrations] and --enhanced-migration in args", async () => {
150
+ const cwd = await makeTempFixture("basic");
151
+ const tomlPath = path.join(cwd, "mops.toml");
152
+ const toml = readFileSync(tomlPath, "utf-8");
153
+ await writeFile(tomlPath, toml.replace("[canisters.backend.migrations]", 'args = ["--enhanced-migration=migrations"]\n\n[canisters.backend.migrations]'));
154
+ const result = await cli(["check"], { cwd });
155
+ expect(result.exitCode).toBe(1);
156
+ expect(result.stderr).toMatch(/--enhanced-migration/);
157
+ expect(result.stderr).toMatch(/managed automatically/i);
158
+ });
159
+ });
160
+ });
package/dist/types.d.ts CHANGED
@@ -34,6 +34,12 @@ export type Config = {
34
34
  extra?: Record<string, string[]>;
35
35
  };
36
36
  };
37
+ export type MigrationsConfig = {
38
+ chain: string;
39
+ next?: string;
40
+ "check-limit"?: number;
41
+ "build-limit"?: number;
42
+ };
37
43
  export type CanisterConfig = {
38
44
  main?: string;
39
45
  args?: string[];
@@ -43,6 +49,7 @@ export type CanisterConfig = {
43
49
  path: string;
44
50
  skipIfMissing?: boolean;
45
51
  };
52
+ migrations?: MigrationsConfig;
46
53
  };
47
54
  export type Dependencies = Record<string, Dependency>;
48
55
  export type Dependency = {
@@ -0,0 +1,166 @@
1
+ import { existsSync, mkdirSync, readdirSync, symlinkSync } from "node:fs";
2
+ import { join, resolve } from "node:path";
3
+ import { rm } from "node:fs/promises";
4
+ import chalk from "chalk";
5
+ import { cliError } from "../error.js";
6
+ import { resolveConfigPath } from "../mops.js";
7
+ import { MigrationsConfig } from "../types.js";
8
+
9
+ const MIGRATIONS_TEMP_DIR = ".mops/.migrations";
10
+
11
+ export interface MigrationArgsResult {
12
+ migrationArgs: string[];
13
+ cleanup: () => Promise<void>;
14
+ }
15
+
16
+ export function getMigrationFiles(dir: string): string[] {
17
+ if (!existsSync(dir)) {
18
+ return [];
19
+ }
20
+ return readdirSync(dir)
21
+ .filter((f) => f.endsWith(".mo"))
22
+ .sort();
23
+ }
24
+
25
+ export function getNextMigrationFile(nextDir: string): string | null {
26
+ if (!existsSync(nextDir)) {
27
+ return null;
28
+ }
29
+ const files = readdirSync(nextDir).filter((f) => f.endsWith(".mo"));
30
+ if (files.length > 1) {
31
+ cliError(
32
+ `next-migration directory must contain at most 1 .mo file, found ${files.length} in ${nextDir}`,
33
+ );
34
+ }
35
+ return files[0] ?? null;
36
+ }
37
+
38
+ export function validateNextMigrationOrder(
39
+ chainDirOrFiles: string | string[],
40
+ nextFile: string,
41
+ ): void {
42
+ const chainFiles =
43
+ typeof chainDirOrFiles === "string"
44
+ ? getMigrationFiles(chainDirOrFiles)
45
+ : chainDirOrFiles;
46
+ const lastChainFile = chainFiles[chainFiles.length - 1];
47
+ if (lastChainFile && nextFile <= lastChainFile) {
48
+ cliError(
49
+ `Next migration "${nextFile}" must sort after all files in the chain.\n` +
50
+ `Last chain file: "${lastChainFile}".\n` +
51
+ "Use a timestamp prefix (e.g. YYYYMMDD_HHMMSS_Name.mo) to ensure correct ordering.",
52
+ );
53
+ }
54
+ }
55
+
56
+ export function validateMigrationsConfig(
57
+ migrations: MigrationsConfig,
58
+ canisterName: string,
59
+ ): void {
60
+ if (!migrations.chain) {
61
+ cliError(
62
+ `[canisters.${canisterName}.migrations] is missing required field "chain"`,
63
+ );
64
+ }
65
+ for (const field of ["check-limit", "build-limit"] as const) {
66
+ const value = migrations[field];
67
+ if (value !== undefined && (!Number.isInteger(value) || value <= 0)) {
68
+ cliError(
69
+ `[canisters.${canisterName}.migrations] ${field} must be a positive integer`,
70
+ );
71
+ }
72
+ }
73
+ }
74
+
75
+ export async function prepareMigrationArgs(
76
+ migrations: MigrationsConfig | undefined,
77
+ canisterName: string,
78
+ mode: "check" | "build",
79
+ verbose?: boolean,
80
+ ): Promise<MigrationArgsResult> {
81
+ const noOp: MigrationArgsResult = {
82
+ migrationArgs: [],
83
+ cleanup: async () => {},
84
+ };
85
+
86
+ if (!migrations) {
87
+ return noOp;
88
+ }
89
+
90
+ validateMigrationsConfig(migrations, canisterName);
91
+
92
+ const chainDir = resolveConfigPath(migrations.chain);
93
+ const nextDir = migrations.next
94
+ ? resolveConfigPath(migrations.next)
95
+ : undefined;
96
+ const nextFile = nextDir ? getNextMigrationFile(nextDir) : null;
97
+
98
+ if (!existsSync(chainDir) && !nextFile) {
99
+ cliError(
100
+ `Migration chain directory not found: ${chainDir}\n` +
101
+ "Run `mops migrate new <Name>` to initialize the migration chain.",
102
+ );
103
+ }
104
+
105
+ const chainFiles = getMigrationFiles(chainDir);
106
+
107
+ if (nextFile) {
108
+ validateNextMigrationOrder(chainFiles, nextFile);
109
+ }
110
+
111
+ // Treat chain + next as one virtual merged list
112
+ type MigrationEntry = { file: string; dir: string };
113
+ const allMigrations: MigrationEntry[] = chainFiles.map((f) => ({
114
+ file: f,
115
+ dir: chainDir,
116
+ }));
117
+ if (nextFile && nextDir) {
118
+ allMigrations.push({ file: nextFile, dir: nextDir });
119
+ }
120
+
121
+ const limit =
122
+ mode === "check" ? migrations["check-limit"] : migrations["build-limit"];
123
+ const isTrimming = limit !== undefined && limit < allMigrations.length;
124
+ const needsTempDir = nextFile !== null || isTrimming;
125
+
126
+ if (!needsTempDir) {
127
+ return {
128
+ migrationArgs: [`--enhanced-migration=${chainDir}`],
129
+ cleanup: async () => {},
130
+ };
131
+ }
132
+
133
+ const tempDir = join(MIGRATIONS_TEMP_DIR, canisterName);
134
+ await rm(tempDir, { recursive: true, force: true });
135
+ mkdirSync(tempDir, { recursive: true });
136
+
137
+ const filesToInclude = isTrimming
138
+ ? allMigrations.slice(-limit)
139
+ : allMigrations;
140
+
141
+ for (const { file, dir } of filesToInclude) {
142
+ symlinkSync(resolve(dir, file), join(tempDir, file));
143
+ }
144
+
145
+ if (verbose) {
146
+ console.log(
147
+ chalk.blue("migrations"),
148
+ chalk.gray(
149
+ `Prepared ${filesToInclude.length} migration(s) for ${canisterName}` +
150
+ (isTrimming ? ` (trimmed from ${allMigrations.length})` : ""),
151
+ ),
152
+ );
153
+ }
154
+
155
+ const migrationArgs = [`--enhanced-migration=${tempDir}`];
156
+ if (isTrimming) {
157
+ migrationArgs.push("-A=M0254");
158
+ }
159
+
160
+ return {
161
+ migrationArgs,
162
+ cleanup: async () => {
163
+ await rm(tempDir, { recursive: true, force: true });
164
+ },
165
+ };
166
+ }
@@ -14,11 +14,22 @@ export function resolveCanisterConfigs(
14
14
  );
15
15
  }
16
16
 
17
- export function resolveCanisterEntrypoints(config: Config): string[] {
18
- const canisters = resolveCanisterConfigs(config);
19
- return Object.values(canisters)
20
- .map((c) => c.main)
21
- .filter((main): main is string => Boolean(main));
17
+ export function filterCanisters(
18
+ canisters: Record<string, CanisterConfig>,
19
+ names?: string[],
20
+ ): Record<string, CanisterConfig> {
21
+ if (!names) {
22
+ return canisters;
23
+ }
24
+ const invalidNames = names.filter((name) => !(name in canisters));
25
+ if (invalidNames.length) {
26
+ cliError(
27
+ `Canister(s) not found in mops.toml: ${invalidNames.join(", ")}. Available: ${Object.keys(canisters).join(", ")}`,
28
+ );
29
+ }
30
+ return Object.fromEntries(
31
+ Object.entries(canisters).filter(([name]) => names.includes(name)),
32
+ );
22
33
  }
23
34
 
24
35
  export function resolveSingleCanister(
@@ -50,3 +61,40 @@ export function resolveSingleCanister(
50
61
 
51
62
  return { name: names[0]!, canister: canisters[names[0]!]! };
52
63
  }
64
+
65
+ export function looksLikeFile(arg: string): boolean {
66
+ return (
67
+ arg.endsWith(".mo") ||
68
+ arg.endsWith(".most") ||
69
+ arg.includes("/") ||
70
+ arg.includes("\\")
71
+ );
72
+ }
73
+
74
+ export function validateCanisterArgs(
75
+ canister: CanisterConfig,
76
+ canisterName: string,
77
+ config?: Config,
78
+ ): void {
79
+ if (canister.args && typeof canister.args === "string") {
80
+ cliError(
81
+ `Canister config 'args' should be an array of strings for canister ${canisterName}`,
82
+ );
83
+ }
84
+ if (!canister.migrations) {
85
+ return;
86
+ }
87
+ const flagSources: [string, string[] | undefined][] = [
88
+ [`[canisters.${canisterName}].args`, canister.args],
89
+ ["[moc].args", config?.moc?.args],
90
+ ["[build].args", config?.build?.args],
91
+ ];
92
+ for (const [section, args] of flagSources) {
93
+ if (args?.some((a) => a.startsWith("--enhanced-migration"))) {
94
+ cliError(
95
+ `Canister '${canisterName}' has [migrations] config but --enhanced-migration in ${section}.\n` +
96
+ "Remove --enhanced-migration — it is managed automatically when [migrations] is configured.",
97
+ );
98
+ }
99
+ }
100
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ic-mops",
3
- "version": "2.9.0",
3
+ "version": "2.11.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "mops": "dist/bin/mops.js",
@@ -32,11 +32,11 @@ exports[`check error 2`] = `
32
32
  }
33
33
  `;
34
34
 
35
- exports[`check no args falls back to [canisters] entrypoints 1`] = `
35
+ exports[`check no args checks all canisters 1`] = `
36
36
  {
37
37
  "exitCode": 0,
38
38
  "stderr": "",
39
- "stdout": "✓ Ok.mo",
39
+ "stdout": "✓ backend",
40
40
  }
41
41
  `;
42
42
 
@@ -0,0 +1,119 @@
1
+ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2
+
3
+ exports[`migrate build build produces .most with full migration chain 1`] = `
4
+ "// Version: 4.0.0
5
+ {
6
+ "20250101_000000_Init" : {} -> {a : Nat};
7
+ "20250201_000000_AddName" : (old : {a : Nat}) -> {a : Nat; name : Text};
8
+ "20250301_000000_AddEmail" :
9
+ (old : {a : Nat; name : Text}) -> {a : Nat; email : Text; name : Text}
10
+ }
11
+ actor {
12
+ stable a : Nat;
13
+ stable email : Text;
14
+ stable name : Text
15
+ };
16
+ "
17
+ `;
18
+
19
+ exports[`migrate build build with build-limit produces trimmed .most 1`] = `
20
+ "// Version: 4.0.0
21
+ {
22
+ "20250201_000000_AddName" : (old : {a : Nat}) -> {a : Nat; name : Text};
23
+ "20250301_000000_AddEmail" :
24
+ (old : {a : Nat; name : Text}) -> {a : Nat; email : Text; name : Text}
25
+ }
26
+ actor {
27
+ stable a : Nat;
28
+ stable email : Text;
29
+ stable name : Text
30
+ };
31
+ "
32
+ `;
33
+
34
+ exports[`migrate build build-limit counts next migration as part of the chain 1`] = `
35
+ "// Version: 4.0.0
36
+ {
37
+ "20250301_000000_AddEmail" :
38
+ (old : {a : Nat; name : Text}) -> {a : Nat; email : Text; name : Text};
39
+ "20250401_000000_RenameId" :
40
+ (old : {a : Nat; email : Text; name : Text}) ->
41
+ {email : Text; id : Nat; name : Text}
42
+ }
43
+ actor {
44
+ stable email : Text;
45
+ stable id : Nat;
46
+ stable name : Text
47
+ };
48
+ "
49
+ `;
50
+
51
+ exports[`migrate check check fails without next migration, passes with it 1`] = `
52
+ {
53
+ "exitCode": 1,
54
+ "stderr": "src/main.mo:3.1-11.2: Compatibility error [M0169], the stable variable \`a\` of version \`20250301_000000_AddEmail\` cannot be implicitly discarded. The variable can only be dropped by an explicit migration function, please see https://internetcomputer.org/docs/motoko/fundamentals/actors/compatibility#explicit-migration-using-a-migration-function
55
+ migrations/20250101_000000_Init.mo:0.1: warning [M0254], initial actor requires field \`id\` of type
56
+ Nat
57
+ ✗ Check failed for canister backend (exit code: 1)",
58
+ "stdout": "",
59
+ }
60
+ `;
61
+
62
+ exports[`migrate check check fails without next migration, passes with it 2`] = `
63
+ {
64
+ "exitCode": 0,
65
+ "stderr": "",
66
+ "stdout": "✓ backend
67
+ ✓ Stable compatibility check passed for canister 'backend'",
68
+ }
69
+ `;
70
+
71
+ exports[`migrate check check with trimming shows reduced chain 1`] = `
72
+ {
73
+ "exitCode": 0,
74
+ "stderr": "",
75
+ "stdout": "check Using --all-libs for richer diagnostics
76
+ migrations Prepared 2 migration(s) for backend (trimmed from 3)
77
+ check Checking canister backend:
78
+ <CACHE>moc-wrapper ["src/main.mo","--check","--all-libs","--default-persistent-actors","--enhanced-migration=.mops/.migrations/backend","-A=M0254"]
79
+ ✓ backend
80
+ check-stable Generating stable types for src/main.mo
81
+ <CACHE>moc-wrapper ["--stable-types","-o",".mops/.check-stable/new.wasm","src/main.mo","--default-persistent-actors","--enhanced-migration=.mops/.migrations/backend","-A=M0254"]
82
+ check-stable Comparing deployed.most ↔ .mops/.check-stable/new.most
83
+ <CACHE>moc-wrapper ["--stable-compatible","deployed.most",".mops/.check-stable/new.most"]
84
+ ✓ Stable compatibility check passed for canister 'backend'",
85
+ }
86
+ `;
87
+
88
+ exports[`migrate migrate freeze moves the file from next to chain 1`] = `
89
+ {
90
+ "exitCode": 0,
91
+ "stderr": "",
92
+ "stdout": "✓ Frozen migration: 20250401_000000_RenameId.mo → migrations/",
93
+ }
94
+ `;
95
+
96
+ exports[`migrate migrate new creates a migration file with timestamp and template 1`] = `
97
+ {
98
+ "exitCode": 0,
99
+ "stderr": "",
100
+ "stdout": "✓ Created migration: next-migration/<TIMESTAMP>_AddPhone.mo",
101
+ "template": "module {
102
+ public func migration(old : {}) : {} {
103
+ {}
104
+ }
105
+ }
106
+ ",
107
+ }
108
+ `;
109
+
110
+ exports[`migrate stable check hint stable check fails with hint when deployed.most is incompatible 1`] = `
111
+ {
112
+ "exitCode": 1,
113
+ "stderr": "(unknown location): Compatibility error [M0169], the stable variable \`a\` of the previous version cannot be implicitly discarded. The variable can only be dropped by an explicit migration function, please see https://internetcomputer.org/docs/motoko/fundamentals/actors/compatibility#explicit-migration-using-a-migration-function
114
+ (unknown location): Compatibility error [M0169], the stable variable \`name\` of the previous version cannot be implicitly discarded. The variable can only be dropped by an explicit migration function, please see https://internetcomputer.org/docs/motoko/fundamentals/actors/compatibility#explicit-migration-using-a-migration-function
115
+ Hint: You may need a migration. Run \`mops migrate new <Name>\` to create one.
116
+ ✗ Stable compatibility check failed for canister 'backend'",
117
+ "stdout": "✓ backend",
118
+ }
119
+ `;
@@ -0,0 +1,5 @@
1
+ actor {
2
+ public func example() : async () {
3
+ let unused = 123;
4
+ };
5
+ };
@@ -0,0 +1,9 @@
1
+ [toolchain]
2
+ moc = "1.3.0"
3
+
4
+ [moc]
5
+ args = ["--default-persistent-actors"]
6
+
7
+ [canisters.backend]
8
+ main = "Warning.mo"
9
+ args = ["-Werror"]
@@ -0,0 +1,8 @@
1
+ module {
2
+ public func migration(_ : {}) : { a : Nat; b : Text } {
3
+ {
4
+ a = 42;
5
+ b = "hello";
6
+ };
7
+ };
8
+ };
@@ -0,0 +1,9 @@
1
+ module {
2
+ public func migration(old : { a : Nat; b : Text }) : {
3
+ a : Nat;
4
+ b : Text;
5
+ c : Bool;
6
+ } {
7
+ { old with c = true };
8
+ };
9
+ };
@@ -0,0 +1,9 @@
1
+ [toolchain]
2
+ moc = "1.5.0"
3
+
4
+ [moc]
5
+ args = ["--default-persistent-actors"]
6
+
7
+ [canisters.backend]
8
+ main = "src/main.mo"
9
+ args = ["--enhanced-migration=migrations"]
@@ -0,0 +1,8 @@
1
+ // Version: 4.0.0
2
+ {
3
+ "20250101_000000_Init" : {} -> {a : Nat; b : Text}
4
+ }
5
+ actor {
6
+ stable a : Nat;
7
+ stable b : Text
8
+ };
@@ -0,0 +1,11 @@
1
+ import Prim "mo:prim";
2
+
3
+ actor {
4
+ let a : Nat;
5
+ let b : Text;
6
+ let c : Bool;
7
+
8
+ public func check() : async () {
9
+ Prim.debugPrint(debug_show { a; b; c });
10
+ };
11
+ };
@@ -59,6 +59,27 @@ describe("check-stable", () => {
59
59
  expect(existsSync(path.join(cwd, "new.wasm"))).toBe(false);
60
60
  });
61
61
 
62
+ test("[canisters.X].args are passed to moc (enhanced migration)", async () => {
63
+ const cwd = path.join(import.meta.dirname, "check-stable/canister-args");
64
+ const result = await cli(["check-stable", "old.most"], { cwd });
65
+ expect(result.exitCode).toBe(0);
66
+ expect(result.stdout).toMatch(/Stable compatibility check passed/);
67
+ });
68
+
69
+ test("no args checks all canisters with [check-stable] config", async () => {
70
+ const cwd = path.join(import.meta.dirname, "check/deployed-compatible");
71
+ const result = await cli(["check-stable"], { cwd });
72
+ expect(result.exitCode).toBe(0);
73
+ expect(result.stdout).toMatch(/Stable compatibility check passed/);
74
+ });
75
+
76
+ test("canister name filters to specific canister", async () => {
77
+ const cwd = path.join(import.meta.dirname, "check/deployed-compatible");
78
+ const result = await cli(["check-stable", "backend"], { cwd });
79
+ expect(result.exitCode).toBe(0);
80
+ expect(result.stdout).toMatch(/Stable compatibility check passed/);
81
+ });
82
+
62
83
  test("errors when old file does not exist", async () => {
63
84
  const cwd = path.join(import.meta.dirname, "check-stable/compatible");
64
85
  const result = await cli(["check-stable", "nonexistent.mo"], { cwd });