Colota - Android GPS Location tracker
refactor(backup): move the backup captions into the form and make restore a row (#867)
Max Dietrich9ff37bef378f
diff --git a/apps/mobile/src/screens/BackupRestoreScreen.tsx b/apps/mobile/src/screens/BackupRestoreScreen.tsxindex 8da97124..e60c024c 100644--- a/apps/mobile/src/screens/BackupRestoreScreen.tsx+++ b/apps/mobile/src/screens/BackupRestoreScreen.tsx@@ -13,6 +13,7 @@ import {Container,Divider,FieldMessage,+ ListItem,LoadingOverlay,SectionTitle,StateLine,@@ -33,7 +34,6 @@ import {NO_RECOVERY_LINE,OPEN_FILE_LINE,passwordLine,- PASSWORD_HINT_LINE,PICKED_NOT_OPENED_CAPTION,restoreCaveat,restoreConfirm,@@ -221,10 +221,6 @@ export function BackupRestoreScreen({}: ScreenProps) {return (<Container><ScrollView contentContainerStyle={styles.scrollContent} keyboardShouldPersistTaps="handled">- <Text style={[styles.intro, { color: colors.textSecondary }]}>- One encrypted file holding everything this device has recorded and everything it is set up to do.- </Text>-{loaded && (<Card rows style={styles.section}><StateLine@@ -240,9 +236,21 @@ export function BackupRestoreScreen({}: ScreenProps) {)}<View style={styles.section}>- <SectionTitle>Make a backup</SectionTitle>- <Card>+ <SectionTitle>Backup</SectionTitle>+ <Card testID="backup-form"><View style={styles.fields}>+ {loaded ? (+ <View>+ <Text style={[styles.description, { color: colors.textSecondary }]}>+ {backupScopeLine(stats.total)}+ </Text>+ {excludes ? (+ <Text style={[styles.description, styles.descriptionNext, { color: colors.textSecondary }]}>+ {excludes}+ </Text>+ ) : null}+ </View>+ ) : null}<View><TextFieldlabel="Password"@@ -254,9 +262,7 @@ export function BackupRestoreScreen({}: ScreenProps) {autoCorrect={false}disabled={busy !== null}/>- <FieldMessage variant={password.length > 0 ? pwLine.variant : "info"}>- {password.length > 0 ? pwLine.text : PASSWORD_HINT_LINE}- </FieldMessage>+ {password.length > 0 ? <FieldMessage variant={pwLine.variant}>{pwLine.text}</FieldMessage> : null}</View><View><TextField@@ -283,27 +289,32 @@ export function BackupRestoreScreen({}: ScreenProps) {disabled={busy !== null || blocked !== null}onPress={onCreateBackup}/>- <FieldMessage variant={backupMessage?.tone === "error" ? "error" : "info"}>- {backupMessage?.text ?? blocked ?? backupScopeLine(stats.total)}- </FieldMessage>- {!backupMessage && !blocked && excludes ? <FieldMessage>{excludes}</FieldMessage> : null}+ {backupMessage ? (+ <FieldMessage variant={backupMessage.tone === "error" ? "error" : "info"}>+ {backupMessage.text}+ </FieldMessage>+ ) : blocked ? (+ <FieldMessage>{blocked}</FieldMessage>+ ) : null}</View><View style={styles.section}>- <SectionTitle>Restore from a backup</SectionTitle>+ <SectionTitle>Restore</SectionTitle>{!picked && (- <View>- <Button- variant="secondary"- icon={Download}- title="Choose a backup file"- testID="choose-backup-btn"+ <Card rows>+ <ListItem+ testID="choose-backup-row"+ icon={FileLock}+ trailingIcon={Download}+ label="Choose a backup file"+ sub={RESTORE_IDLE_LINE}+ subLines={2}+ accessibilityHint="Opens the file picker"disabled={busy !== null}onPress={onChooseFile}/>- <FieldMessage>{RESTORE_IDLE_LINE}</FieldMessage>- </View>+ </Card>)}{picked && !manifest && (@@ -329,6 +340,7 @@ export function BackupRestoreScreen({}: ScreenProps) {error={restoreMessage?.tone === "error" ? restoreMessage.text : undefined}disabled={busy !== null}/>+ {restoreMessage?.tone !== "error" && <FieldMessage>{OPEN_FILE_LINE}</FieldMessage>}</View></Card><Button@@ -345,7 +357,6 @@ export function BackupRestoreScreen({}: ScreenProps) {testID="repick-backup-btn"onPress={onChooseFile}/>- <FieldMessage>{OPEN_FILE_LINE}</FieldMessage></>)}@@ -359,8 +370,12 @@ export function BackupRestoreScreen({}: ScreenProps) {caption={archiveLine(manifest).caption}testID="archive-state"/>+ {caveat ? (+ <View style={styles.caveat}>+ <FieldMessage variant="warning">{caveat}</FieldMessage>+ </View>+ ) : null}</Card>- {caveat && <FieldMessage variant="warning">{caveat}</FieldMessage>}<Buttonvariant="danger"title="Replace all data"@@ -396,12 +411,6 @@ const styles = StyleSheet.create({paddingTop: space.lg,paddingBottom: space.xxl},- intro: {- fontSize: fontSizes.body,- ...fonts.regular,- lineHeight: lineHeights.body,- marginBottom: space.lg- },section: {marginBottom: space.xl},@@ -410,5 +419,16 @@ const styles = StyleSheet.create({},pickedField: {paddingTop: space.lg+ },+ description: {+ fontSize: fontSizes.description,+ ...fonts.regular,+ lineHeight: lineHeights.description+ },+ descriptionNext: {+ marginTop: space.sm+ },+ caveat: {+ paddingBottom: space.lg}})diff --git a/apps/mobile/src/screens/__tests__/BackupRestoreScreen.test.tsx b/apps/mobile/src/screens/__tests__/BackupRestoreScreen.test.tsxindex 5a05625e..e5192a3b 100644--- a/apps/mobile/src/screens/__tests__/BackupRestoreScreen.test.tsx+++ b/apps/mobile/src/screens/__tests__/BackupRestoreScreen.test.tsx@@ -1,5 +1,5 @@import React from "react"-import { render, fireEvent, waitFor, act } from "@testing-library/react-native"+import { render, fireEvent, waitFor, act, within } from "@testing-library/react-native"jest.mock("@react-navigation/native", () => ({useFocusEffect: (cb: () => (() => void) | void) => {@@ -73,7 +73,7 @@ jest.mock("../../components", () => {return {Container: ({ children }: any) => R.createElement(View, null, children),SectionTitle: ({ children }: any) => R.createElement(Text, null, children),- Card: ({ children }: any) => R.createElement(View, null, children),+ Card: ({ children, testID }: any) => R.createElement(View, { testID }, children),Divider: () => R.createElement(View, null),StateLine: ({ label, caption, testID }: any) =>R.createElement(View, { testID }, R.createElement(Text, null, label), R.createElement(Text, null, caption)),@@ -99,6 +99,19 @@ jest.mock("../../components", () => {},R.createElement(Text, null, title)),+ ListItem: ({ label, sub, testID, onPress, disabled, trailingIcon }: any) =>+ R.createElement(+ Pressable,+ {+ testID,+ onPress,+ disabled,+ accessibilityState: { disabled: !!disabled },+ accessibilityValue: { text: trailingIcon?.displayName ?? "chevron" }+ },+ R.createElement(Text, null, label),+ R.createElement(Text, null, sub)+ ),LoadingOverlay: ({ visible, title }: any) => (visible ? R.createElement(Text, null, title) : null)}})@@ -118,7 +131,7 @@ const typeGoodPassword = (api: ReturnType<typeof renderScreen>) => {}const openArchive = async (api: ReturnType<typeof renderScreen>) => {- fireEvent.press(api.getByTestId("choose-backup-btn"))+ fireEvent.press(api.getByTestId("choose-backup-row"))await waitFor(() => expect(api.getByTestId("picked-file")).toBeTruthy())fireEvent.changeText(api.getByTestId("restore-password"), "the password")fireEvent.press(api.getByTestId("open-backup-btn"))@@ -159,25 +172,46 @@ describe("BackupRestoreScreen", () => {})// The restore half must not open on a red button.- it("starts restore on a secondary button, not a destructive one", async () => {+ it("starts restore on a row that picks a file, not a destructive button", async () => {const api = renderScreen()await api.findByText(/^You last backed up /)- expect(api.getByTestId("choose-backup-btn").props.accessibilityValue.text).toBe("secondary")+ expect(api.getByTestId("choose-backup-row").props.accessibilityValue.text).toBe("Download")expect(api.queryByTestId("restore-btn")).toBeNull()})++ it("says what a backup holds inside the form, once", async () => {+ mockGetClientCertInfo.mockResolvedValue({ subject: "phone" })+ const api = renderScreen()+ await api.findByText(/^Writes 12,481 locations, your geofences/)++ const form = api.getByTestId("backup-form")+ expect(within(form).getByText(/^Writes 12,481 locations/)).toBeTruthy()+ expect(within(form).getByText(/client certificate is not included/)).toBeTruthy()+ expect(api.getAllByText(/^Writes 12,481 locations/)).toHaveLength(1)+ })})describe("making a backup", () => {- it("says why Create backup is disabled instead of leaving it dead", async () => {+ it("says under Create backup why it is disabled, one reason at a time", async () => {const api = renderScreen()await api.findByText(/^You last backed up /)+ const disabled = () => api.getByTestId("create-backup-btn").props.accessibilityState.disabledexpect(api.getByText("Choose a password first.")).toBeTruthy()- expect(api.getByTestId("create-backup-btn").props.accessibilityState.disabled).toBe(true)+ expect(disabled()).toBe(true)fireEvent.changeText(api.getByTestId("backup-password"), "correct horse battery staple")- await waitFor(() => expect(api.getByText("Type the password a second time.")).toBeTruthy())+ expect(await api.findByText("Type the password a second time.")).toBeTruthy()+ expect(api.queryByText("Choose a password first.")).toBeNull()++ fireEvent.changeText(api.getByTestId("backup-password-confirm"), "correct horse")+ expect(api.getAllByText("The two passwords do not match.").length).toBeGreaterThan(0)+ expect(disabled()).toBe(true)++ fireEvent.changeText(api.getByTestId("backup-password-confirm"), "correct horse battery staple")+ await waitFor(() => expect(disabled()).toBe(false))+ expect(api.queryByText("The two passwords do not match.")).toBeNull()})it("blocks a weak password on the score native returned", async () => {@@ -187,7 +221,8 @@ describe("BackupRestoreScreen", () => {typeGoodPassword(api)- await waitFor(() => expect(api.getByText("This password is too easy to guess.")).toBeTruthy())+ await waitFor(() => expect(api.getByText("Weak. Make it longer or less predictable.")).toBeTruthy())+ expect(api.getByText("This password is too easy to guess.")).toBeTruthy()expect(api.getByTestId("create-backup-btn").props.accessibilityState.disabled).toBe(true)})@@ -243,12 +278,13 @@ describe("BackupRestoreScreen", () => {const api = renderScreen()await api.findByText(/^You last backed up /)- fireEvent.press(api.getByTestId("choose-backup-btn"))+ fireEvent.press(api.getByTestId("choose-backup-row"))await waitFor(() => expect(api.getByTestId("picked-file")).toBeTruthy())fireEvent.changeText(api.getByTestId("restore-password"), "wrong")fireEvent.press(api.getByTestId("open-backup-btn"))expect(await api.findByText(/That password did not open this file/)).toBeTruthy()+ expect(api.queryByText(/^Checks the password/)).toBeNull()expect(mockRestoreBackup).not.toHaveBeenCalled()// The file stays picked, so retrying is one press.expect(api.getByTestId("picked-file")).toBeTruthy()@@ -258,11 +294,12 @@ describe("BackupRestoreScreen", () => {const api = renderScreen()await api.findByText(/^You last backed up /)- fireEvent.press(api.getByTestId("choose-backup-btn"))+ fireEvent.press(api.getByTestId("choose-backup-row"))await waitFor(() => expect(api.getByTestId("picked-file")).toBeTruthy())expect(api.queryByTestId("restore-btn")).toBeNull()expect(api.getByText("Not opened yet.")).toBeTruthy()+ expect(api.getByText(/^Checks the password and reads what the file holds/)).toBeTruthy()})it("names the archive's date once it is open, and offers the replace", async () => {diff --git a/apps/mobile/src/utils/__tests__/backupState.test.ts b/apps/mobile/src/utils/__tests__/backupState.test.tsindex 93d45f1f..8d4a3d98 100644--- a/apps/mobile/src/utils/__tests__/backupState.test.ts+++ b/apps/mobile/src/utils/__tests__/backupState.test.ts@@ -98,7 +98,7 @@ describe("passwordLine", () => {it("prints the verdict native computed, and the advice that fits it", () => {expect(passwordLine(strength(0, 0, "Too short"), false).text).toBe("Too short. Make it longer.")expect(passwordLine(strength(1, 38, "Weak"), false).text).toBe("Weak. Make it longer or less predictable.")- expect(passwordLine(strength(2, 55, "OK"), false).text).toBe("OK. 55 bits.")+ expect(passwordLine(strength(2, 55, "OK"), false).text).toBe("OK.")})})diff --git a/apps/mobile/src/utils/backupState.ts b/apps/mobile/src/utils/backupState.tsindex da7e701c..3d361ca6 100644--- a/apps/mobile/src/utils/backupState.ts+++ b/apps/mobile/src/utils/backupState.ts@@ -55,7 +55,7 @@ export function passwordLine(failed: boolean): { text: string; variant: "info" | "warning" } {if (failed) return { text: "Could not check this password. Try again.", variant: "warning" }- if (strength.score >= 2) return { text: `${strength.label}. ${Math.round(strength.bits)} bits.`, variant: "info" }+ if (strength.score >= 2) return { text: `${strength.label}.`, variant: "info" }// Native's own label, so the reason can never disagree with the gate and no length lives in JS.const advice = strength.score === 0 ? "Make it longer." : "Make it longer or less predictable."return { text: strength.label ? `${strength.label}. ${advice}` : advice, variant: "warning" }@@ -74,7 +74,7 @@ export function submitBlockedReason(return null}-/** What a new archive would contain. Stated once, where the button that writes it lives. */+/** What a new archive would contain, stated at the top of the backup form. */export function backupScopeLine(total: number): string {return `Writes ${plural(total, "location")}, your geofences, profiles, settings and your stored server credentials to a file you pick.`}@@ -99,8 +99,6 @@ export const PICKED_NOT_OPENED_CAPTION = "Not opened yet."export const OPEN_FILE_LINE = "Checks the password and reads what the file holds. Nothing on this device changes."-export const PASSWORD_HINT_LINE = "Several unrelated words work better than one short, clever one."-/** The opened archive, as a state line. */export function archiveLine(manifest: BackupManifest): { label: string; caption: string } {const seconds = Date.parse(manifest.createdAt) / 1000