|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import fsExtra from "fs-extra" |
| 6 | + |
| 7 | +import path from "path" |
| 8 | +import os from "os" |
| 9 | + |
| 10 | +import disableLogger from "src/test/unit/helpers/disableLogger" |
| 11 | +import {Migrations} from "./migrations" |
| 12 | +import {AppState} from "./app-state" |
| 13 | +import states from "src/test/unit/states" |
| 14 | + |
| 15 | +const dir = path.join(os.tmpdir(), "session.test.ts") |
| 16 | +const file = path.join(dir, "appState.json") |
| 17 | +const backupDir = path.join(dir, "backups") |
| 18 | + |
| 19 | +disableLogger() |
| 20 | +beforeEach(() => fsExtra.ensureDir(dir)) |
| 21 | +afterEach(() => fsExtra.remove(dir)) |
| 22 | + |
| 23 | +function init() { |
| 24 | + return new AppState({path: file, backupDir}) |
| 25 | +} |
| 26 | + |
| 27 | +test("app state file that doesn't exist", () => { |
| 28 | + expect(fsExtra.existsSync(file)).toBe(false) |
| 29 | + const appState = init() |
| 30 | + expect(appState.data).toEqual(undefined) |
| 31 | + expect(appState.version).toEqual(Migrations.latestVersion) |
| 32 | +}) |
| 33 | + |
| 34 | +test("app state file that is empty", () => { |
| 35 | + fsExtra.createFileSync(file) |
| 36 | + const appState = init() |
| 37 | + expect(appState.data).toEqual(undefined) |
| 38 | + expect(appState.version).toEqual(Migrations.latestVersion) |
| 39 | +}) |
| 40 | + |
| 41 | +test("app state file that does not parse to JSON", () => { |
| 42 | + fsExtra.writeFileSync(file, "---\nthis_is_yaml: true\n---") |
| 43 | + expect(fsExtra.existsSync(file)).toBe(true) |
| 44 | + expect(() => { |
| 45 | + init() |
| 46 | + }).toThrow(/The application state file could not be parsed as JSON/) |
| 47 | +}) |
| 48 | + |
| 49 | +test("app state file that is JSON but has not version number", async () => { |
| 50 | + const v8 = { |
| 51 | + order: [], |
| 52 | + windows: {}, |
| 53 | + globalState: {investigation: [], pools: {zqd: {}}, version: "6"}, |
| 54 | + } |
| 55 | + fsExtra.writeJSONSync(file, v8) |
| 56 | + |
| 57 | + expect(() => { |
| 58 | + init() |
| 59 | + }).toThrow( |
| 60 | + /The application state file is a JSON object but is missing the top-level version key of type number/ |
| 61 | + ) |
| 62 | +}) |
| 63 | + |
| 64 | +test("app state is migrated if migrations are pending", () => { |
| 65 | + const needsMigration = states.getPath("v1.18.0.json") |
| 66 | + const oldState = fsExtra.readJSONSync(needsMigration) |
| 67 | + expect(oldState.version).not.toEqual(Migrations.latestVersion) |
| 68 | + |
| 69 | + fsExtra.cpSync(needsMigration, file) |
| 70 | + const appState = init() |
| 71 | + |
| 72 | + expect(appState.version).toBe(Migrations.latestVersion) |
| 73 | +}) |
| 74 | + |
| 75 | +test("app state is backed if migration is needed", () => { |
| 76 | + const needsMigration = states.getPath("v1.18.0.json") |
| 77 | + const oldState = fsExtra.readJSONSync(needsMigration) |
| 78 | + fsExtra.cpSync(needsMigration, file) |
| 79 | + init() |
| 80 | + expect(fsExtra.existsSync(backupDir)).toBe(true) |
| 81 | + const backup = fsExtra.readdirSync(backupDir)[0] |
| 82 | + expect(backup).toMatch(/^\d{12}_backup.json$/) |
| 83 | + const backupFile = path.join(backupDir, backup) |
| 84 | + expect(fsExtra.readJSONSync(backupFile)).toEqual(oldState) |
| 85 | +}) |
| 86 | + |
| 87 | +test("app state is not backed up if no migration is needed", () => { |
| 88 | + init() |
| 89 | + expect(fsExtra.existsSync(backupDir)).toBe(true) |
| 90 | + expect(fsExtra.readdirSync(backupDir)).toEqual([]) |
| 91 | +}) |
| 92 | + |
| 93 | +test("backing up the same version twice creates distict backups", () => { |
| 94 | + fsExtra.cpSync(states.getPath("v1.18.0.json"), file) |
| 95 | + init() |
| 96 | + expect(fsExtra.readdirSync(backupDir)).toEqual(["202407221450_backup.json"]) |
| 97 | + fsExtra.cpSync(states.getPath("v1.18.0.json"), file) |
| 98 | + init() |
| 99 | + expect(fsExtra.readdirSync(backupDir)).toEqual([ |
| 100 | + "202407221450_backup.json", |
| 101 | + "202407221450_backup_2.json", |
| 102 | + ]) |
| 103 | + fsExtra.cpSync(states.getPath("v1.18.0.json"), file) |
| 104 | + init() |
| 105 | + expect(fsExtra.readdirSync(backupDir)).toEqual([ |
| 106 | + "202407221450_backup.json", |
| 107 | + "202407221450_backup_2.json", |
| 108 | + "202407221450_backup_3.json", |
| 109 | + ]) |
| 110 | +}) |
| 111 | + |
| 112 | +test("a migration error does not affect the state file", () => { |
| 113 | + const fixture = states.getPath("v1.18.0.json") |
| 114 | + fsExtra.cpSync(fixture, file) |
| 115 | + Migrations.all.push({ |
| 116 | + version: 9999_99_99_99_99, |
| 117 | + migrate: (bang) => bang.boom.boom, |
| 118 | + }) |
| 119 | + expect(() => { |
| 120 | + init() |
| 121 | + }).toThrow(/Cannot read properties of undefined \(reading 'boom'\)/) |
| 122 | + Migrations.all.pop() |
| 123 | + expect(fsExtra.readJSONSync(file)).toEqual(fsExtra.readJSONSync(fixture)) |
| 124 | +}) |
| 125 | + |
| 126 | +test("app state saves new data", () => { |
| 127 | + const appState = init() |
| 128 | + appState.save({hello: "test"}) |
| 129 | + |
| 130 | + expect(fsExtra.readJSONSync(file)).toEqual({ |
| 131 | + version: Migrations.latestVersion, |
| 132 | + data: {hello: "test"}, |
| 133 | + }) |
| 134 | + expect(appState.data).toEqual({hello: "test"}) |
| 135 | + expect(appState.version).toEqual(Migrations.latestVersion) |
| 136 | +}) |
| 137 | + |
| 138 | +test("app state reset", () => { |
| 139 | + const appState = init() |
| 140 | + appState.save({hello: "test"}) |
| 141 | + |
| 142 | + appState.reset() |
| 143 | + expect(fsExtra.readJSONSync(file)).toEqual({ |
| 144 | + version: Migrations.latestVersion, |
| 145 | + }) |
| 146 | +}) |
0 commit comments