test: fix and add tests for the yt-dlp auto-updater

This commit is contained in:
Jelle Glebbeek
2021-10-17 23:53:16 +02:00
parent b28ead4ad3
commit 7c82a19877

View File

@@ -15,7 +15,7 @@ describe("writeVersionInfo", () => {
const instance = new BinaryUpdater({ ytdlVersion: "a/test/path" });
instance.writeVersionInfo("v2.0.0-test1");
expect(fs.promises.writeFile).toBeCalledTimes(1);
expect(fs.promises.writeFile).toBeCalledWith("a/test/path", "{\"version\":\"v2.0.0-test1\"}");
expect(fs.promises.writeFile).toBeCalledWith("a/test/path", "{\"version\":\"v2.0.0-test1\",\"ytdlp\":true}");
});
});
@@ -28,13 +28,21 @@ describe("getLocalVersion", () => {
});
});
it('returns the version property from the json file', () => {
jest.spyOn(fs.promises, 'readFile').mockResolvedValue("{\"version\": \"v2.0.0-test1\"}")
jest.spyOn(fs.promises, 'readFile').mockResolvedValue("{\"version\": \"v2.0.0-test1\",\"ytdlp\":true}")
jest.spyOn(fs.promises, 'access').mockResolvedValue("");
const instance = new BinaryUpdater({ ytdlVersion: "a/test/path" });
return instance.getLocalVersion().then((data) => {
expect(data).toBe("v2.0.0-test1");
});
});
it('returns null when ytdlp is unset or false', () => {
jest.spyOn(fs.promises, 'readFile').mockResolvedValue("{\"version\": \"v2.0.0-test1\"}")
jest.spyOn(fs.promises, 'access').mockResolvedValue("");
const instance = new BinaryUpdater({ ytdlVersion: "a/test/path" });
return instance.getLocalVersion().then((data) => {
expect(data).toBe(null);
});
});
});
describe('getRemoteVersion', () => {
@@ -42,7 +50,7 @@ describe('getRemoteVersion', () => {
const axiosGetSpy = jest.spyOn(axios, 'get').mockRejectedValue({response: {status: 200}});
const instance = new BinaryUpdater({platform: "win32"});
return instance.getRemoteVersion().then((data) => {
expect(data).toEqual([null, null]);
expect(data).toEqual(null);
expect(axiosGetSpy).toBeCalledTimes(1);
});
});
@@ -50,16 +58,27 @@ describe('getRemoteVersion', () => {
const axiosGetSpy = jest.spyOn(axios, 'get').mockRejectedValue({response: null});
const instance = new BinaryUpdater({platform: "darwin"});
return instance.getRemoteVersion().then((data) => {
expect(data).toEqual([null, null]);
expect(data).toEqual(null);
expect(axiosGetSpy).toBeCalledTimes(1);
});
});
it('returns array with the link and the version', () => {
const redirectURL = "http://yt-dl.org/downloads/2021.03.14/youtube-dl.exe"
const axiosGetSpy = jest.spyOn(axios, 'get').mockRejectedValue({response: {status: 302, headers: {location: redirectURL}}});
const redirectURL = "https://github.com/yt-dlp/yt-dlp/releases/download/2021.10.10/yt-dlp.exe"
const axiosGetSpy = jest.spyOn(axios, 'get').mockRejectedValue({
response: {
status: 302,
data: "<a href=\"https://github.com/yt-dlp/yt-dlp/releases/download/2021.10.10/yt-dlp.exe\">",
headers: {
location: redirectURL
}
}
});
const instance = new BinaryUpdater({platform: "win32"});
return instance.getRemoteVersion().then((data) => {
expect(data).toEqual([redirectURL, "2021.03.14"]);
expect(data).toEqual({
remoteUrl: redirectURL,
remoteVersion: "2021.10.10"
});
expect(axiosGetSpy).toBeCalledTimes(1);
});
});
@@ -103,8 +122,8 @@ describe('checkUpdate', () => {
const win = {webContents: {send: jest.fn()}};
const instance = new BinaryUpdater({platform: "win32", ytdl: "a/path/to"}, win);
const downloadUpdateSpy = jest.spyOn(instance, 'downloadUpdate').mockResolvedValue("");
jest.spyOn(instance, 'getLocalVersion').mockResolvedValue("v1.8.7");
jest.spyOn(instance, 'getRemoteVersion').mockResolvedValue(["link", "v2.0.0"]);
jest.spyOn(instance, 'getLocalVersion').mockResolvedValue("2021.03.10");
jest.spyOn(instance, 'getRemoteVersion').mockResolvedValue({ remoteUrl: "link", remoteVersion: "2021.10.10" });
return instance.checkUpdate().then(() => {
expect(downloadUpdateSpy).toBeCalledTimes(1);
expect(instance.win.webContents.send).toBeCalledTimes(1);