|
| 1 | +import { beforeEach, expect, test, vi } from "vitest"; |
| 2 | +import type { ConfigType } from "@/types/config/config.schema.ts"; |
| 3 | + |
| 4 | +const defaultConfig: ConfigType = { |
| 5 | + "__do_not_touch_VERSION": 1, |
| 6 | + "customization" : { |
| 7 | + "theme" : "dark", |
| 8 | + "accent" : "rose", |
| 9 | + "background": "none", |
| 10 | + }, |
| 11 | + "locale" : "system", |
| 12 | + "minecraftWindowHeight": 480, |
| 13 | + "minecraftWindowWidth" : 854, |
| 14 | +}; |
| 15 | + |
| 16 | +vi.mock("@/lib/handlers/log.ts", async () => { |
| 17 | + return await vi.importActual("@/__mocks__/log.cjs"); |
| 18 | +}); |
| 19 | +vi.mock("@/lib/main/get-default-config.ts", async () => { |
| 20 | + return { |
| 21 | + "getDefaultConfig": async (): Promise<ConfigType> => defaultConfig, |
| 22 | + }; |
| 23 | +}); |
| 24 | +vi.mock("@/lib/main/initialize-config-file.ts", async () => { |
| 25 | + return { |
| 26 | + "initializeConfigFile": async (): Promise<void> => {}, |
| 27 | + }; |
| 28 | +}); |
| 29 | + |
| 30 | +const tests: Array<{ |
| 31 | + "arguments": { |
| 32 | + "exists" : boolean; |
| 33 | + "fetchedConfig": string; |
| 34 | + }; |
| 35 | + "output": unknown; |
| 36 | +}> = [ |
| 37 | + { |
| 38 | + "arguments": { |
| 39 | + "exists" : true, |
| 40 | + "fetchedConfig": JSON.stringify({}), |
| 41 | + }, |
| 42 | + "output": defaultConfig, |
| 43 | + }, |
| 44 | + { |
| 45 | + "arguments": { |
| 46 | + "exists" : true, |
| 47 | + "fetchedConfig": JSON.stringify({ |
| 48 | + ...defaultConfig, |
| 49 | + "TUYU": "is awesome", |
| 50 | + }), |
| 51 | + }, |
| 52 | + "output": defaultConfig, |
| 53 | + }, |
| 54 | + { |
| 55 | + "arguments": { |
| 56 | + "exists" : true, |
| 57 | + "fetchedConfig": JSON.stringify({ |
| 58 | + ...defaultConfig, |
| 59 | + "__do_not_touch_VERSION": "-1", |
| 60 | + "customization" : { |
| 61 | + "theme": "blue", |
| 62 | + }, |
| 63 | + }), |
| 64 | + }, |
| 65 | + "output": defaultConfig, |
| 66 | + }, |
| 67 | + { |
| 68 | + "arguments": { |
| 69 | + "exists" : true, |
| 70 | + "fetchedConfig": JSON.stringify({ |
| 71 | + ...defaultConfig, |
| 72 | + "__do_not_touch_VERSION": -1, |
| 73 | + "customization" : { |
| 74 | + "theme" : "light", |
| 75 | + "accent" : "red", |
| 76 | + "background": "some-url", |
| 77 | + "nyaa" : "si", |
| 78 | + }, |
| 79 | + "locale" : 2, |
| 80 | + "minecraftWindowHeight" : "480", |
| 81 | + "minecraftWindowHeight ": 500, |
| 82 | + }), |
| 83 | + }, |
| 84 | + "output": defaultConfig, |
| 85 | + }, |
| 86 | + { |
| 87 | + "arguments": { |
| 88 | + "exists" : true, |
| 89 | + "fetchedConfig": JSON.stringify({ |
| 90 | + ...defaultConfig, |
| 91 | + "__do_not_touch_VERSION": -1, |
| 92 | + "customization" : { |
| 93 | + "theme" : "light", |
| 94 | + "accent" : "red", |
| 95 | + "background": "some-url", |
| 96 | + }, |
| 97 | + }), |
| 98 | + }, |
| 99 | + "output": { |
| 100 | + "__do_not_touch_VERSION": -1, |
| 101 | + "customization" : { |
| 102 | + "theme" : "light", |
| 103 | + "accent" : "red", |
| 104 | + "background": "some-url", |
| 105 | + }, |
| 106 | + "locale" : "system", |
| 107 | + "minecraftWindowHeight": 480, |
| 108 | + "minecraftWindowWidth" : 854, |
| 109 | + }, |
| 110 | + }, |
| 111 | + { |
| 112 | + "arguments": { |
| 113 | + "exists" : true, |
| 114 | + "fetchedConfig": "0", |
| 115 | + }, |
| 116 | + "output": defaultConfig, |
| 117 | + }, |
| 118 | + { |
| 119 | + "arguments": { |
| 120 | + "exists" : false, |
| 121 | + // If 'exists' were 'true', it would throw an error (expected behaviour) |
| 122 | + "fetchedConfig": "", |
| 123 | + }, |
| 124 | + "output": defaultConfig, |
| 125 | + }, |
| 126 | +]; |
| 127 | +let index = 0; |
| 128 | +let currentTestBeforeEach = tests[index]; |
| 129 | + |
| 130 | +beforeEach(() => { |
| 131 | + vi.doMock("@tauri-apps/plugin-fs", async () => { |
| 132 | + return { |
| 133 | + "BaseDirectory": { |
| 134 | + "AppData": 14, |
| 135 | + }, |
| 136 | + "exists" : async (): Promise<boolean> => currentTestBeforeEach.arguments.exists, |
| 137 | + "readTextFile": async (): Promise<string> => { |
| 138 | + console.log(index, tests[index], currentTestBeforeEach); |
| 139 | + |
| 140 | + return currentTestBeforeEach.arguments.fetchedConfig; |
| 141 | + }, |
| 142 | + }; |
| 143 | + }); |
| 144 | + |
| 145 | + return () => { |
| 146 | + index++; |
| 147 | + currentTestBeforeEach = tests[index]; |
| 148 | + }; |
| 149 | +}); |
| 150 | + |
| 151 | +for (const currentTest of tests) { |
| 152 | + const testName = `Get Config File: ${JSON.stringify(currentTest.arguments)}`; |
| 153 | + |
| 154 | + test(testName, async () => { |
| 155 | + /* |
| 156 | + * Original import (top-level) will not be mocked, because 'vi.doMock' is evaluated |
| 157 | + * after all imports and 'vi.mock' can't be called dynamically |
| 158 | + */ |
| 159 | + const { getConfigFile } = await import("./get-config-file.ts"); |
| 160 | + |
| 161 | + expect( |
| 162 | + JSON.stringify(await getConfigFile()), |
| 163 | + ).toBe( |
| 164 | + JSON.stringify(currentTest.output), |
| 165 | + ); |
| 166 | + }); |
| 167 | +} |
0 commit comments