import { configureRequestOptionsFromUrl, GithubOptions } from "builder-util-runtime"
import { MacUpdater } from "electron-updater/out/MacUpdater"
import { EventEmitter } from "events"
import { assertThat } from "../helpers/fileAssert"
import { createTestAppAdapter, httpExecutor, trackEvents, tuneTestUpdater, writeUpdateConfig } from "../helpers/updaterTestUtil"
import { mockForNodeRequire } from "vitest-mock-commonjs"
class TestNativeUpdater extends EventEmitter {
private updateUrl: string | null = null
checkForUpdates() {
console.log("TestNativeUpdater.checkForUpdates")
this.download().catch(error => {
this.emit("error", error)
})
}
private async download() {
const data = JSON.parse((await httpExecutor.request(configureRequestOptionsFromUrl(this.updateUrl!, {})))!)
await httpExecutor.request(configureRequestOptionsFromUrl(data.url, {}))
}
setFeedURL(updateUrl: any) {
this.updateUrl = updateUrl.url
}
}
test.ifMac("mac updates", async ({ expect }) => {
const mockNativeUpdater = new TestNativeUpdater()
mockForNodeRequire("electron", {
autoUpdater: mockNativeUpdater,
})
const updater = new MacUpdater(undefined, await createTestAppAdapter())
const options: GithubOptions = {
provider: "github",
owner: "develar",
repo: "onshape-desktop-shell",
}
updater.updateConfigPath = await writeUpdateConfig(options)
updater.on("download-progress", () => {
})
tuneTestUpdater(updater)
;(updater as any)._testOnlyOptions.platform = process.platform
const actualEvents = trackEvents(updater)
const updateCheckResult = await updater.checkForUpdates()
const files = await updateCheckResult?.downloadPromise
expect(files!.length).toEqual(1)
await assertThat(expect, files![0]).isFile()
expect(actualEvents).toMatchSnapshot()
})