diff --git a/app/apps/evenhub/store-layer.ts b/app/apps/evenhub/store-layer.ts index c2c044e6..a19a8c15 100644 --- a/app/apps/evenhub/store-layer.ts +++ b/app/apps/evenhub/store-layer.ts @@ -124,9 +124,16 @@ export class EvenHubStoreLayer implements Layer { paint(ctx: LayerContext): GrayImage { if (!this.started) { this.started = true; - if (this.showingLogin) { - this.openCredentialEditor(ctx); - } else { + // Do NOT open the phone credential editor here. This runs on the first + // paint, which includes the restore of a still-open EvenHub at app + // startup -- so an unfinished sign-in re-opened the form on every launch, + // and the form has no way out: it has no Cancel control, its onCancel is + // dropped before it reaches the controller, and Back leaves the app + // rather than closing it. The login pane already says "Enter your Even + // account email and password in the phone app" and already opens the + // editor on click (handleInput), so the editor is still one tap away for + // someone who actually wants it. + if (!this.showingLogin) { void this.ensureTabLoaded(ctx, this.activeTab); } } @@ -650,7 +657,11 @@ export class EvenHubStoreLayer implements Layer { return true; } - private enterLogin(ctx: LayerContext, status = "Sign in to browse public apps."): void { + private enterLogin( + ctx: LayerContext, + status = "Sign in to browse public apps.", + openEditor = true, + ): void { const wasShowingLogin = this.showingLogin; this.closePhoneEditor(ctx.actions, "search"); this.showingLogin = true; @@ -659,7 +670,7 @@ export class EvenHubStoreLayer implements Layer { for (const tab of TABS) this.tabs[tab.id] = emptyTab(); this.focus = "list"; if (!wasShowingLogin) resetEvenHubLoginForm(); - this.openCredentialEditor(ctx); + if (openEditor) this.openCredentialEditor(ctx); ctx.actions.requestRender(); } @@ -699,7 +710,19 @@ export class EvenHubStoreLayer implements Layer { const email = evenHubLoginEmailSetting.get(); const password = evenHubLoginPasswordSetting.get(); if (!email.trim() || !password) { - this.enterLogin(ctx, "Email and password are required."); + // The phone editor has no Cancel control, so submitting an untouched form + // with the keyboard's Done key is the only way someone can back out of + // it. Reopening the editor there makes that a loop with no exit, so a + // wholly empty submission is treated as a cancel and leaves the editor + // closed -- the login pane still says how to get back in, and a click + // reopens the editor. A half-filled form is a real mistake, so that + // still reopens with the message. + const cancelled = !email.trim() && !password; + this.enterLogin( + ctx, + cancelled ? "Sign in to browse public apps." : "Email and password are required.", + !cancelled, + ); return; } this.loginBusy = true; diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 519d9a71..caa893c8 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -204,6 +204,14 @@ class DashboardController { private activeTextSettings: ConfigSettingString[] = []; private activeTextEditorTitle = ""; private activeTextEditorOnFinish: (() => void) | null = null; + /** + * The caller's cancel callback. LayerActions has always declared this fifth + * parameter, but neither the wiring below nor startTextSettingsEdit accepted + * it, so it was dropped silently -- a 4-parameter function is assignable to a + * 5-parameter type, so nothing failed to compile. A caller that tracks its own + * "editor is open" state therefore had no way to learn the editor closed. + */ + private activeTextEditorOnCancel: (() => void) | null = null; private activeTextEditorToggle: TextSettingsEditToggle | null = null; private evenNotificationActive = false; private evenAppConflictMessage = ""; @@ -321,7 +329,8 @@ class DashboardController { title: string, onFinish?: () => void, toggle?: TextSettingsEditToggle, - ) => this.startTextSettingsEdit(settings, title, onFinish, toggle), + onCancel?: () => void, + ) => this.startTextSettingsEdit(settings, title, onFinish, toggle, onCancel), endTextSettingEdit: () => this.endTextSettingEdit(), startVoiceCapture: (endpointing = false) => this.startVoiceCapture(endpointing), stopVoiceCapture: () => this.stopVoiceCapture(), @@ -1884,11 +1893,13 @@ class DashboardController { title: string, onFinish?: () => void, toggle?: TextSettingsEditToggle, + onCancel?: () => void, ): void { this.activeTextSettings = Array.from(settings.slice(0, 2)); this.activeTextEditorTitle = title; this.activeTextEditorOnFinish = onFinish ?? null; this.activeTextEditorToggle = toggle ?? null; + this.activeTextEditorOnCancel = onCancel ?? null; this.emit(); } @@ -2005,11 +2016,16 @@ class DashboardController { private endTextSettingEdit(): void { const finishedSettings = this.activeTextSettings; + // Closing without the done key is a cancel from the caller's point of view. + // finishTextSettingEdit clears this first, so a completed edit never fires it. + const onCancel = this.activeTextEditorOnCancel; this.activeTextSettings = []; this.activeTextEditorTitle = ""; this.activeTextEditorOnFinish = null; this.activeTextEditorToggle = null; + this.activeTextEditorOnCancel = null; this.emit(); + onCancel?.(); if (finishedSettings.includes(nightscoutSiteUrlSetting) || finishedSettings.includes(nightscoutApiTokenSetting)) { void this.refreshNightscoutAfterSettingsChange(); } @@ -2023,6 +2039,9 @@ class DashboardController { finishActiveTextSettingEdit(): void { if (!this.activeTextSettings.length) return; const onFinish = this.activeTextEditorOnFinish; + // Completing is not cancelling: drop the cancel callback before + // endTextSettingEdit, which fires whatever is still set. + this.activeTextEditorOnCancel = null; const closesGlassesEditor = this.activeTextSettings.length === 1; this.endTextSettingEdit(); if (closesGlassesEditor && this.textEditorHost?.closeTextEditor()) { diff --git a/tests/evenhub-signin.test.cjs b/tests/evenhub-signin.test.cjs new file mode 100644 index 00000000..6350a6fe --- /dev/null +++ b/tests/evenhub-signin.test.cjs @@ -0,0 +1,126 @@ +// The EvenHub sign-in form's open/close contract, which is what made it +// impossible to get rid of: it used to open itself on the store layer's first +// paint (which includes the restore of a still-open EvenHub at startup), and +// submitting an untouched form reopened it, so with no Cancel control on the +// panel there was no way out. +// +// The real store layer runs here with its dependencies mocked, so these assert +// the layer's actual behavior rather than a reimplementation of it. +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { loader } = require('./helpers/load-typescript.cjs'); + +const setting = (initial = '') => { + let value = initial; + return { get: () => value, set: (next) => { value = next; } }; +}; + +/** Load the store layer with a signed-out account and no stored credentials. */ +const loadStoreLayer = () => { + const email = setting(''); + const password = setting(''); + const font = { lineHeight: 10 }; + const load = loader({}, { + '../../graphics/ui-fonts': { getDefaultSmallFont: () => font }, + '../../graphics/image': { GrayImage: class { drawText() {} } }, + '../../graphics/textwrap': { truncateText: (_f, text) => text, wrapText: (_f, text) => [text] }, + '../../util/numeric-util': { clamp: (v, lo, hi) => Math.min(hi, Math.max(lo, v)) }, + '../../ui/gestures': { GESTURE_CLICK: 'click' }, + '../../ui/menu': { drawListScrollbar() {}, drawSelectionHighlight() {}, scrollToKeepSelectionVisible() {} }, + '../../ui/shell/shell': { shell: { yieldFocusToSidebar() {} } }, + '../../ui/metrics': { lineStep: () => 12 }, + '../../ui/dashboard-settings': { + ConfigSettingString: class { constructor(options) { this.value = options.defaultValue ?? ''; } + get() { return this.value; } set(next) { this.value = next; } }, + }, + './even-api': { + evenHubApi: { signIn: async () => {} }, + EvenHubAuthenticationError: class extends Error {}, + isEvenHubStoreConfigured: () => false, + }, + './credentials': { + evenHubLoginEmailSetting: email, + evenHubLoginPasswordSetting: password, + evenHubRememberMeSetting: { get: () => true, set() {} }, + clearEvenHubLoginForm() { email.set(''); password.set(''); }, + resetEvenHubLoginForm() { email.set(''); password.set(''); }, + clearEvenHubSession() {}, clearTransientEvenHubSession() {}, + }, + './installed-apps': { getInstalledEvenHubApps: () => [], uninstallEvenHubPackage: async () => {} }, + './store-detail-layer': { EvenHubStoreDetailLayer: class {} }, + './store-install': { cleanError: (error) => String(error), installStoreApp: async () => {} }, + './updates': { checkForEvenHubUpdates: async () => [], describeUpdate: () => '' }, + }); + const { EvenHubStoreLayer } = load('app/apps/evenhub/store-layer.ts'); + return { EvenHubStoreLayer, email, password }; +}; + +/** A LayerContext that records every editor the layer asks the phone to open. */ +const makeCtx = () => { + const opened = []; + return { + opened, + stack: { getBaseSize: () => ({ width: 576, height: 136 }) }, + actions: { + requestRender() {}, + endTextSettingEdit() {}, + startTextSettingsEdit(settings, title, onFinish, toggle, onCancel) { + opened.push({ title, onFinish, onCancel }); + }, + }, + }; +}; + +const newLayer = () => { + const { EvenHubStoreLayer } = loadStoreLayer(); + return new EvenHubStoreLayer({ launchApp: async () => {}, appendLog: () => {} }); +}; + +const settle = () => new Promise((resolve) => setImmediate(resolve)); + +test('painting the signed-out store does not open the phone sign-in form by itself', () => { + const layer = newLayer(), ctx = makeCtx(); + layer.paint(ctx); + layer.paint(ctx); + assert.deepEqual(ctx.opened, [], 'first paint runs on app-start restore too, so it must not open the editor'); +}); + +test('a click on the login pane opens the sign-in form', () => { + const layer = newLayer(), ctx = makeCtx(); + layer.paint(ctx); + layer.handleInput({ type: 'click' }, ctx); + assert.equal(ctx.opened.length, 1); + assert.equal(ctx.opened[0].title, 'Sign in to EvenHub'); + assert.equal(typeof ctx.opened[0].onCancel, 'function', 'the editor must hand back a way to close it'); +}); + +test('submitting an untouched sign-in form closes it instead of reopening it', async () => { + const layer = newLayer(), ctx = makeCtx(); + layer.paint(ctx); + layer.handleInput({ type: 'click' }, ctx); + ctx.opened[0].onFinish(); + await settle(); + assert.equal(ctx.opened.length, 1, 'an empty submission is the only way out, so it must not reopen the editor'); +}); + +test('a half-filled sign-in form is a mistake and reopens with the reason', async () => { + const { EvenHubStoreLayer, email } = loadStoreLayer(); + const layer = new EvenHubStoreLayer({ launchApp: async () => {}, appendLog: () => {} }); + const ctx = makeCtx(); + layer.paint(ctx); + layer.handleInput({ type: 'click' }, ctx); + email.set('someone@example.com'); + ctx.opened[0].onFinish(); + await settle(); + assert.equal(ctx.opened.length, 2); + assert.equal(ctx.opened[1].title, 'Sign in to EvenHub'); +}); + +test('cancelling the sign-in form leaves it reopenable', () => { + const layer = newLayer(), ctx = makeCtx(); + layer.paint(ctx); + layer.handleInput({ type: 'click' }, ctx); + ctx.opened[0].onCancel(); + layer.handleInput({ type: 'click' }, ctx); + assert.equal(ctx.opened.length, 2, 'a dropped onCancel latches the guard and no later open is allowed'); +});