|
| 1 | +import { CommandType } from "../types/macros/CommandType"; |
1 | 2 | import { describe, expect, it } from "vitest"; |
2 | 3 | import migration from "./consolidateFileExistsBehavior"; |
3 | 4 |
|
@@ -51,7 +52,52 @@ describe("consolidateFileExistsBehavior migration", () => { |
51 | 52 | }); |
52 | 53 | }); |
53 | 54 |
|
54 | | - it("normalizes nested macro command template choices", async () => { |
| 55 | + it("normalizes template choices nested inside Macro choice commands", async () => { |
| 56 | + const plugin = { |
| 57 | + settings: { |
| 58 | + choices: [ |
| 59 | + { |
| 60 | + id: "macro-choice", |
| 61 | + name: "Macro Choice", |
| 62 | + type: "Macro", |
| 63 | + macro: { |
| 64 | + id: "macro-1", |
| 65 | + name: "Macro", |
| 66 | + commands: [ |
| 67 | + { |
| 68 | + type: CommandType.NestedChoice, |
| 69 | + choice: { |
| 70 | + id: "template-choice", |
| 71 | + name: "Template", |
| 72 | + type: "Template", |
| 73 | + setFileExistsBehavior: true, |
| 74 | + fileExistsMode: "Append duplicate suffix", |
| 75 | + }, |
| 76 | + }, |
| 77 | + ], |
| 78 | + }, |
| 79 | + }, |
| 80 | + ], |
| 81 | + macros: [], |
| 82 | + }, |
| 83 | + } as any; |
| 84 | + |
| 85 | + await migration.migrate(plugin); |
| 86 | + |
| 87 | + expect( |
| 88 | + plugin.settings.choices[0].macro.commands[0].choice, |
| 89 | + ).toMatchObject({ |
| 90 | + fileExistsBehavior: { kind: "apply", mode: "duplicateSuffix" }, |
| 91 | + }); |
| 92 | + expect( |
| 93 | + plugin.settings.choices[0].macro.commands[0].choice.fileExistsMode, |
| 94 | + ).toBeUndefined(); |
| 95 | + expect( |
| 96 | + plugin.settings.choices[0].macro.commands[0].choice.setFileExistsBehavior, |
| 97 | + ).toBeUndefined(); |
| 98 | + }); |
| 99 | + |
| 100 | + it("normalizes nested macro command template choices in legacy macros", async () => { |
55 | 101 | const plugin = { |
56 | 102 | settings: { |
57 | 103 | choices: [], |
@@ -82,5 +128,97 @@ describe("consolidateFileExistsBehavior migration", () => { |
82 | 128 | expect(plugin.settings.macros[0].commands[0].choice).toMatchObject({ |
83 | 129 | fileExistsBehavior: { kind: "prompt" }, |
84 | 130 | }); |
| 131 | + expect( |
| 132 | + plugin.settings.macros[0].commands[0].choice.fileExistsMode, |
| 133 | + ).toBeUndefined(); |
| 134 | + expect( |
| 135 | + plugin.settings.macros[0].commands[0].choice.setFileExistsBehavior, |
| 136 | + ).toBeUndefined(); |
| 137 | + }); |
| 138 | + |
| 139 | + it("normalizes template choices nested in conditional macro branches", async () => { |
| 140 | + const plugin = { |
| 141 | + settings: { |
| 142 | + choices: [], |
| 143 | + macros: [ |
| 144 | + { |
| 145 | + id: "macro-1", |
| 146 | + name: "Macro", |
| 147 | + commands: [ |
| 148 | + { |
| 149 | + type: CommandType.Conditional, |
| 150 | + thenCommands: [ |
| 151 | + { |
| 152 | + type: CommandType.NestedChoice, |
| 153 | + choice: { |
| 154 | + id: "template-then", |
| 155 | + name: "Then Template", |
| 156 | + type: "Template", |
| 157 | + setFileExistsBehavior: true, |
| 158 | + fileExistsMode: "Append duplicate suffix", |
| 159 | + }, |
| 160 | + }, |
| 161 | + ], |
| 162 | + elseCommands: [ |
| 163 | + { |
| 164 | + type: CommandType.NestedChoice, |
| 165 | + choice: { |
| 166 | + id: "template-else", |
| 167 | + name: "Else Template", |
| 168 | + type: "Template", |
| 169 | + setFileExistsBehavior: false, |
| 170 | + fileExistsMode: "Overwrite the file", |
| 171 | + }, |
| 172 | + }, |
| 173 | + ], |
| 174 | + }, |
| 175 | + ], |
| 176 | + }, |
| 177 | + ], |
| 178 | + }, |
| 179 | + } as any; |
| 180 | + |
| 181 | + await migration.migrate(plugin); |
| 182 | + |
| 183 | + expect( |
| 184 | + plugin.settings.macros[0].commands[0].thenCommands[0].choice, |
| 185 | + ).toMatchObject({ |
| 186 | + fileExistsBehavior: { kind: "apply", mode: "duplicateSuffix" }, |
| 187 | + }); |
| 188 | + expect( |
| 189 | + plugin.settings.macros[0].commands[0].thenCommands[0].choice |
| 190 | + .fileExistsMode, |
| 191 | + ).toBeUndefined(); |
| 192 | + expect( |
| 193 | + plugin.settings.macros[0].commands[0].thenCommands[0].choice |
| 194 | + .setFileExistsBehavior, |
| 195 | + ).toBeUndefined(); |
| 196 | + expect( |
| 197 | + plugin.settings.macros[0].commands[0].elseCommands[0].choice, |
| 198 | + ).toMatchObject({ |
| 199 | + fileExistsBehavior: { kind: "prompt" }, |
| 200 | + }); |
| 201 | + expect( |
| 202 | + plugin.settings.macros[0].commands[0].elseCommands[0].choice |
| 203 | + .fileExistsMode, |
| 204 | + ).toBeUndefined(); |
| 205 | + expect( |
| 206 | + plugin.settings.macros[0].commands[0].elseCommands[0].choice |
| 207 | + .setFileExistsBehavior, |
| 208 | + ).toBeUndefined(); |
| 209 | + }); |
| 210 | + |
| 211 | + it("treats malformed persisted choice and macro collections as empty arrays", async () => { |
| 212 | + const plugin = { |
| 213 | + settings: { |
| 214 | + choices: { invalid: true }, |
| 215 | + macros: "invalid", |
| 216 | + }, |
| 217 | + } as any; |
| 218 | + |
| 219 | + await migration.migrate(plugin); |
| 220 | + |
| 221 | + expect(plugin.settings.choices).toEqual([]); |
| 222 | + expect(plugin.settings.macros).toEqual([]); |
85 | 223 | }); |
86 | 224 | }); |
0 commit comments