os update btn fixes and refactoring

pull/978/head
gabrielburnworth 2018-09-10 13:20:00 -07:00
parent ecd9ab9959
commit 5c4b1a9566
2 changed files with 291 additions and 158 deletions

View File

@ -13,138 +13,238 @@ import { OsUpdateButton } from "../os_update_button";
import { OsUpdateButtonProps } from "../interfaces";
describe("<OsUpdateButton/>", () => {
beforeEach(function () {
beforeEach(() => {
bot.currentOSVersion = "3.1.6";
bot.hardware.configuration.beta_opt_in = false;
});
const fakeProps = (): OsUpdateButtonProps => {
return {
bot,
sourceFbosConfig: (x) => {
return { value: bot.hardware.configuration[x], consistent: true };
},
botOnline: true,
};
const fakeProps = (): OsUpdateButtonProps => ({
bot,
sourceFbosConfig: x =>
({ value: bot.hardware.configuration[x], consistent: true }),
botOnline: true,
});
interface TestProps {
installedVersion: string | undefined;
installedCommit: string;
availableVersion: string | undefined;
availableBetaVersion: string | undefined;
availableBetaCommit: string | undefined;
betaOptIn: boolean | undefined;
onBeta: boolean | undefined;
}
const defaultTestProps = (): TestProps => ({
installedVersion: "3.1.6",
installedCommit: "",
availableVersion: "3.1.6",
availableBetaVersion: undefined,
availableBetaCommit: undefined,
betaOptIn: false,
onBeta: false,
});
interface Results {
text: string;
title: string | undefined;
color: string;
disabled: boolean;
}
const updateNeeded = (title: string | undefined): Results =>
({
text: "UPDATE",
title,
color: "green",
disabled: false,
});
const upToDate = (title: string | undefined): Results =>
({
text: "UP TO DATE",
title,
color: "gray",
disabled: false,
});
const cantConnect = (entity: string): Results =>
({
text: `Can't connect to ${entity}`,
title: undefined,
color: "yellow",
disabled: false,
});
const updating = (progress: string): Results =>
({
text: progress,
title: undefined,
color: "gray",
disabled: true,
});
const testButtonState = (
testProps: TestProps,
expected: Results) => {
const {
installedVersion, installedCommit, onBeta,
availableVersion, availableBetaVersion, availableBetaCommit, betaOptIn
} = testProps;
bot.hardware.informational_settings.controller_version = installedVersion;
bot.hardware.informational_settings.commit = installedCommit;
bot.hardware.informational_settings.currently_on_beta = onBeta;
bot.currentOSVersion = availableVersion;
bot.currentBetaOSVersion = availableBetaVersion;
bot.currentBetaOSCommit = availableBetaCommit;
bot.hardware.configuration.beta_opt_in = betaOptIn;
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").first();
expect(osUpdateButton.text()).toBe(expected.text);
expect(osUpdateButton.props().title).toBe(expected.title);
expect(osUpdateButton.hasClass(expected.color)).toBe(true);
expect((osUpdateButton.props().disabled)).toBe(expected.disabled);
};
it("renders buttons: not connected", () => {
bot.hardware.informational_settings.controller_version = undefined;
bot.currentOSVersion = undefined;
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
expect(buttons.find("button").length).toBe(1);
const autoUpdate = buttons.find("button").first();
expect(autoUpdate.hasClass("yellow")).toBeTruthy();
const osUpdateButton = buttons.find("button").last();
expect(osUpdateButton.text()).toBe("Can't connect to bot");
const testProps = defaultTestProps();
testProps.installedVersion = undefined;
testProps.availableVersion = undefined;
const expectedResults = cantConnect("bot");
testButtonState(testProps, expectedResults);
});
it("renders buttons: connected to releases but not bot", () => {
bot.hardware.informational_settings.controller_version = undefined;
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
expect(buttons.find("button").length).toBe(1);
const autoUpdate = buttons.find("button").first();
expect(autoUpdate.hasClass("yellow")).toBeTruthy();
const osUpdateButton = buttons.find("button").last();
expect(osUpdateButton.text()).toBe("Can't connect to bot");
const testProps = defaultTestProps();
testProps.installedVersion = undefined;
const expectedResults = cantConnect("bot");
expectedResults.title = "3.1.6";
testButtonState(testProps, expectedResults);
});
it("renders buttons: no releases available", () => {
bot.hardware.informational_settings.controller_version = "3.1.6";
bot.hardware.configuration.beta_opt_in = true;
bot.currentOSVersion = undefined;
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
expect(osUpdateButton.text()).toBe("Can't connect to release server");
const testProps = defaultTestProps();
testProps.installedVersion = "3.1.6";
testProps.availableVersion = undefined;
testProps.betaOptIn = true;
const expectedResults = cantConnect("release server");
testButtonState(testProps, expectedResults);
});
it("renders buttons: only beta release available", () => {
bot.hardware.informational_settings.controller_version = "3.1.6";
bot.hardware.configuration.beta_opt_in = true;
bot.currentOSVersion = undefined;
bot.currentBetaOSVersion = "3.1.7-beta";
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
expect(osUpdateButton.text()).toBe("UPDATE");
const testProps = defaultTestProps();
testProps.installedVersion = "3.1.6";
testProps.availableVersion = undefined;
testProps.availableBetaVersion = "3.1.7-beta";
testProps.betaOptIn = true;
const expectedResults = updateNeeded("3.1.7-beta");
testButtonState(testProps, expectedResults);
});
it("renders buttons: no beta release available", () => {
bot.hardware.informational_settings.controller_version = "3.1.6";
bot.hardware.configuration.beta_opt_in = true;
bot.currentBetaOSVersion = undefined;
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
expect(osUpdateButton.text()).toBe("UP TO DATE");
const testProps = defaultTestProps();
testProps.installedVersion = "3.1.6";
testProps.availableBetaVersion = undefined;
testProps.betaOptIn = true;
const expectedResults = upToDate("3.1.6");
testButtonState(testProps, expectedResults);
});
it("up to date", () => {
bot.hardware.informational_settings.controller_version = "3.1.6";
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
expect(osUpdateButton.text()).toBe("UP TO DATE");
expect(osUpdateButton.props().title).toBe("3.1.6");
const testProps = defaultTestProps();
testProps.installedVersion = "3.1.6";
const expectedResults = upToDate("3.1.6");
testButtonState(testProps, expectedResults);
});
it("up to date: newer", () => {
bot.hardware.informational_settings.controller_version = "5.0.0";
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
expect(osUpdateButton.text()).toBe("UP TO DATE");
expect(osUpdateButton.props().title).toBe("3.1.6");
const testProps = defaultTestProps();
testProps.installedVersion = "5.0.0";
const expectedResults = upToDate("3.1.6");
testButtonState(testProps, expectedResults);
});
it("update available", () => {
bot.hardware.informational_settings.controller_version = "3.1.5";
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
expect(osUpdateButton.text()).toBe("UPDATE");
expect(osUpdateButton.props().title).toBe("3.1.6");
const testProps = defaultTestProps();
testProps.installedVersion = "3.1.5";
const expectedResults = updateNeeded("3.1.6");
testButtonState(testProps, expectedResults);
});
it("beta update available", () => {
bot.hardware.informational_settings.controller_version = "3.1.5";
bot.hardware.configuration.beta_opt_in = true;
bot.currentBetaOSVersion = "5.0.0-beta";
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
expect(osUpdateButton.text()).toBe("UPDATE");
expect(osUpdateButton.props().title).toBe("5.0.0-beta");
const testProps = defaultTestProps();
testProps.installedVersion = "3.1.5";
testProps.availableBetaVersion = "5.0.0-beta";
testProps.betaOptIn = true;
const expectedResults = updateNeeded("5.0.0-beta");
testButtonState(testProps, expectedResults);
});
it("latest newer than beta update: latest installed", () => {
bot.hardware.informational_settings.controller_version = "3.1.6";
bot.hardware.configuration.beta_opt_in = true;
bot.currentBetaOSVersion = "3.1.6-beta";
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
expect(osUpdateButton.text()).toBe("UP TO DATE");
expect(osUpdateButton.props().title).toBe("3.1.6");
const testProps = defaultTestProps();
testProps.installedVersion = "3.1.6";
testProps.availableBetaVersion = "3.1.6-beta";
testProps.betaOptIn = true;
const expectedResults = upToDate("3.1.6");
testButtonState(testProps, expectedResults);
});
it("latest newer than beta update: beta installed", () => {
bot.hardware.informational_settings.controller_version = "3.1.6";
bot.hardware.configuration.beta_opt_in = true;
bot.hardware.informational_settings.currently_on_beta = true;
bot.currentBetaOSVersion = "3.1.6-beta";
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
expect(osUpdateButton.text()).toBe("UPDATE");
expect(osUpdateButton.props().title).toBe("3.1.6");
const testProps = defaultTestProps();
testProps.installedVersion = "3.1.6";
testProps.availableBetaVersion = "3.1.6-beta";
testProps.betaOptIn = true;
testProps.onBeta = true;
const expectedResults = updateNeeded("3.1.6");
testButtonState(testProps, expectedResults);
});
it("latest newer than beta update: beta installed (beta disabled)", () => {
const testProps = defaultTestProps();
testProps.installedVersion = "3.1.6";
testProps.availableBetaVersion = "3.1.6-beta";
testProps.betaOptIn = false;
testProps.onBeta = true;
const expectedResults = updateNeeded("3.1.6");
testButtonState(testProps, expectedResults);
});
it("on latest beta update", () => {
bot.hardware.informational_settings.controller_version = "3.1.7";
bot.hardware.configuration.beta_opt_in = true;
bot.hardware.informational_settings.currently_on_beta = true;
bot.currentBetaOSVersion = "3.1.7-beta";
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
expect(osUpdateButton.text()).toBe("UP TO DATE");
expect(osUpdateButton.props().title).toBe("3.1.7-beta");
const testProps = defaultTestProps();
testProps.installedVersion = "3.1.7";
testProps.availableBetaVersion = "3.1.7-beta";
testProps.betaOptIn = true;
testProps.onBeta = true;
const expectedResults = upToDate("3.1.7-beta");
testButtonState(testProps, expectedResults);
});
it("beta update has same numeric version: newer commit", () => {
bot.hardware.informational_settings.controller_version = "5.0.0";
bot.hardware.informational_settings.commit = "old commit";
bot.hardware.configuration.beta_opt_in = true;
bot.currentBetaOSVersion = "5.0.0-beta";
bot.currentBetaOSCommit = "new commit";
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
expect(osUpdateButton.text()).toBe("UPDATE");
expect(osUpdateButton.props().title).toBe("5.0.0-beta");
const testProps = defaultTestProps();
testProps.installedVersion = "5.0.0";
testProps.installedCommit = "old commit";
testProps.availableBetaVersion = "5.0.0-beta";
testProps.availableBetaCommit = "new commit";
testProps.betaOptIn = true;
testProps.onBeta = true;
const expectedResults = updateNeeded("5.0.0-beta");
testButtonState(testProps, expectedResults);
});
it("handles installed version newer than available (beta enabled)", () => {
const testProps = defaultTestProps();
testProps.installedVersion = "3.1.7";
testProps.betaOptIn = true;
testProps.onBeta = false;
testProps.availableBetaVersion = "3.1.7-beta";
const expectedResults = upToDate("3.1.7-beta");
testButtonState(testProps, expectedResults);
});
it("calls checkUpdates", () => {
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
const osUpdateButton = buttons.find("button").first();
osUpdateButton.simulate("click");
expect(mockDevice.checkUpdates).toHaveBeenCalledTimes(1);
});
@ -155,7 +255,7 @@ describe("<OsUpdateButton/>", () => {
"FBOS_OTA": { status: "working", bytes: progress, unit: "bytes" }
};
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
const osUpdateButton = buttons.find("button").first();
expect(osUpdateButton.text()).toBe(text);
});
}
@ -167,34 +267,33 @@ describe("<OsUpdateButton/>", () => {
bot.hardware.jobs = {
"FBOS_OTA": { status: "working", percent: 10, unit: "percent" }
};
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
expect(osUpdateButton.text()).toBe("10%");
const expectedResults = updating("10%");
expectedResults.title = "3.1.6";
testButtonState(defaultTestProps(), expectedResults);
});
it("update success", () => {
bot.hardware.jobs = {
"FBOS_OTA": { status: "complete", percent: 100, unit: "percent" }
};
bot.hardware.informational_settings.controller_version = "3.1.6";
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
expect(osUpdateButton.text()).toBe("UP TO DATE");
testButtonState(defaultTestProps(), upToDate("3.1.6"));
});
it("update failed", () => {
bot.hardware.jobs = {
"FBOS_OTA": { status: "error", percent: 10, unit: "percent" }
};
bot.hardware.informational_settings.controller_version = "3.1.5";
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
expect(osUpdateButton.text()).toBe("UPDATE");
const testProps = defaultTestProps();
testProps.installedVersion = "3.1.5";
testButtonState(testProps, updateNeeded("3.1.6"));
});
it("is disabled", () => {
bot.hardware.jobs = {
"FBOS_OTA": { status: "working", percent: 10, unit: "percent" }
};
const buttons = mount(<OsUpdateButton {...fakeProps()} />);
const osUpdateButton = buttons.find("button").last();
const osUpdateButton = buttons.find("button").first();
osUpdateButton.simulate("click");
expect(mockDevice.checkUpdates).not.toHaveBeenCalled();
});

View File

@ -5,25 +5,32 @@ import { SemverResult, semverCompare } from "../../../util";
import { OsUpdateButtonProps } from "./interfaces";
import { checkControllerUpdates } from "../../actions";
import { isString } from "lodash";
import { BotState } from "../../interfaces";
/** FBOS update button states. */
enum UpdateButton { upToDate, needsUpdate, unknown, none }
interface ButtonProps {
color: "green" | "gray" | "yellow";
text: string;
hoverText: string | undefined;
}
/** FBOS update button state => props. */
const buttonProps = (status: UpdateButton): {
color: "green" | "gray" | "yellow", text: string
} => {
switch (status) {
case UpdateButton.needsUpdate:
return { color: "green", text: t("UPDATE") };
case UpdateButton.upToDate:
return { color: "gray", text: t("UP TO DATE") };
case UpdateButton.unknown:
return { color: "yellow", text: t("Can't connect to release server") };
default:
return { color: "yellow", text: t("Can't connect to bot") };
}
};
const buttonProps =
(status: UpdateButton, hoverText: string | undefined): ButtonProps => {
switch (status) {
case UpdateButton.needsUpdate:
return { color: "green", text: t("UPDATE"), hoverText };
case UpdateButton.upToDate:
return { color: "gray", text: t("UP TO DATE"), hoverText };
case UpdateButton.unknown:
const text = t("Can't connect to release server");
return { color: "yellow", text, hoverText };
default:
return { color: "yellow", text: t("Can't connect to bot"), hoverText };
}
};
/** FBOS update download in progress. */
const isWorking = (job: JobProgress | undefined) =>
@ -62,6 +69,15 @@ const getLatestVersion = (
}
};
/** Determine the installed version. */
const getInstalledVersion = (
controllerVersion: string | undefined,
currentlyOnBeta: boolean,
): string | undefined => {
if (!isString(controllerVersion)) { return undefined; }
return currentlyOnBeta ? controllerVersion + "-beta" : controllerVersion;
};
/** Unequal beta commits => needs update. */
const betaCommitsAreEqual = (
fbosCommit: string | undefined,
@ -72,12 +88,13 @@ const betaCommitsAreEqual = (
/** Determine the FBOS update button state. */
const compareWithBotVersion = (
candidate: string | undefined,
controller_version: string | undefined
installedVersion: string | undefined
): UpdateButton => {
if (!isString(controller_version)) { return UpdateButton.none; }
if (!isString(installedVersion)) { return UpdateButton.none; }
if (!isString(candidate)) { return UpdateButton.unknown; }
switch (semverCompare(candidate, controller_version)) {
// If all values are known, match comparison result with button state.
switch (semverCompare(candidate, installedVersion)) {
case SemverResult.RIGHT_IS_GREATER:
case SemverResult.EQUAL:
return UpdateButton.upToDate;
@ -86,46 +103,63 @@ const compareWithBotVersion = (
}
};
/** Installed version equal to latest. */
const equalToLatest = (
latest: string | undefined,
installedVersion: string | undefined
): boolean =>
isString(installedVersion) && isString(latest) &&
semverCompare(installedVersion, latest) === SemverResult.EQUAL;
/** Color, text, and hover text for update button: release version status. */
const buttonVersionStatus =
({ bot, betaOptIn }: { bot: BotState, betaOptIn: boolean, }): ButtonProps => {
// Information about available releases.
const { currentOSVersion, currentBetaOSVersion, currentBetaOSCommit } = bot;
// Currently installed FBOS version data.
const botInfo = bot.hardware.informational_settings;
const { controller_version, commit, currently_on_beta } = botInfo;
/** Newest release version, given settings and data available. */
const latestReleaseV =
getLatestVersion(currentOSVersion, currentBetaOSVersion, betaOptIn);
/** Installed version. */
const installedVersion =
getInstalledVersion(controller_version, !!currently_on_beta);
/** FBOS update button status. */
const btnStatus = compareWithBotVersion(latestReleaseV, installedVersion);
/** Beta update special cases. */
const uncertainty = (btnStatus === UpdateButton.upToDate) &&
equalToLatest(latestReleaseV, installedVersion) && betaOptIn;
/** `1.0.0-beta vs 1.0.0-beta`: installed beta is older. */
const oldBetaCommit = (latestReleaseV === currentBetaOSVersion) &&
!betaCommitsAreEqual(commit, currentBetaOSCommit);
/** Button status modification required for beta release edge cases. */
const updateStatusOverride = uncertainty && oldBetaCommit;
return buttonProps(
updateStatusOverride ? UpdateButton.needsUpdate : btnStatus,
latestReleaseV);
};
/** Shows update availability or download progress. Updates FBOS on click. */
export const OsUpdateButton = (props: OsUpdateButtonProps) => {
const { bot, sourceFbosConfig, botOnline } = props;
let buttonStatus = UpdateButton.none;
/** FBOS beta release opt-in setting. */
const betaOptIn = sourceFbosConfig("beta_opt_in").value;
// Information about available releases.
const { currentOSVersion, currentBetaOSVersion, currentBetaOSCommit } = bot;
// Currently installed FBOS version data.
const { controller_version, commit } = bot.hardware.informational_settings;
/** Newest release version, given settings and data available. */
const latestReleaseV =
getLatestVersion(currentOSVersion, currentBetaOSVersion, !!betaOptIn);
// Set FBOS update button status.
buttonStatus = compareWithBotVersion(latestReleaseV, controller_version);
/** `1.0.0-beta vs 1.0.0` could be disguised as `1.0.0 vs 1.0.0`
* since `controller_version` is truncated.
*/
const uncertainty = (buttonStatus === UpdateButton.upToDate) && betaOptIn;
/** It's actually `1.0.0-beta vs 1.0.0-beta`, but installed beta is old. */
const oldBetaCommit = latestReleaseV === currentBetaOSVersion &&
!betaCommitsAreEqual(commit, currentBetaOSCommit);
/** It's actually `1.0.0-beta vs 1.0.0` therefore needs update to `1.0.0`. */
const oldBeta = latestReleaseV === currentOSVersion &&
bot.hardware.informational_settings.currently_on_beta;
// Button status modification for beta release edge cases.
if (uncertainty && (oldBetaCommit || oldBeta)) {
buttonStatus = UpdateButton.needsUpdate;
}
const betaOptIn = !!sourceFbosConfig("beta_opt_in").value;
/** FBOS update availability. */
const buttonStatusProps = buttonVersionStatus({ bot, betaOptIn });
/** FBOS update download progress data. */
const osUpdateJob = (bot.hardware.jobs || {})["FBOS_OTA"];
return <button
className={"fb-button " + buttonProps(buttonStatus).color}
title={latestReleaseV}
className={"fb-button " + buttonStatusProps.color}
title={buttonStatusProps.hoverText}
disabled={isWorking(osUpdateJob) || !botOnline}
onClick={checkControllerUpdates}>
{downloadProgress(osUpdateJob) || buttonProps(buttonStatus).text}
{downloadProgress(osUpdateJob) || buttonStatusProps.text}
</button>;
};