diff --git a/src/bundled/bestie/BestieTab.test.tsx b/src/bundled/bestie/BestieTab.test.tsx index 51c2c57563..db5c9bc33b 100644 --- a/src/bundled/bestie/BestieTab.test.tsx +++ b/src/bundled/bestie/BestieTab.test.tsx @@ -20,7 +20,7 @@ import { startHostedRead, } from "./former"; import { validateObject } from "../../features/agents/attention"; -import { checkinHourly, memory, withFeature } from "./features"; +import { memory, notes, withFeature } from "./features"; afterEach(() => { cleanup(); @@ -373,12 +373,11 @@ it("turns a hosted Bestie's memory on and off through its provider, and never to expect(set.mock.calls[1]?.[0]).toBe("Be brief."); expect(save).not.toHaveBeenCalled(); // Its timers never run here. - expect( - screen.queryByRole("switch", { name: "Every hour" }), - ).not.toBeInTheDocument(); + for (const name of ["Hourly notes", "Nightly review"]) + expect(screen.queryByRole("switch", { name })).not.toBeInTheDocument(); expect( screen.getAllByText("Not available with remote agents yet."), - ).toHaveLength(1); + ).toHaveLength(2); }); it("never replaces a hosted Bestie's instructions edited elsewhere since the tab read them", async () => { @@ -532,7 +531,7 @@ it("turns memory on and off as a marked section of the instructions", async () = base, }); expect(on).toMatch( - /^Be brief\.\n\n\n# Memory\n/, + /^Be brief\.\n\n\n# Memory\n/, ); // The saved instructions are what the switch shows. @@ -585,10 +584,11 @@ it("keeps memory on through the owner's edits, and offers Restore for edited mar expect(screen.queryByRole("button", { name: "Restore" })).toBeNull(); expect(screen.getByText(/Fix its start and end lines there/)).toBeVisible(); // Check-ins wait on memory. - expect(screen.getByRole("switch", { name: "Every hour" })).toHaveAttribute( - "aria-disabled", - "true", - ); + for (const name of ["Hourly notes", "Nightly review"]) + expect(screen.getByRole("switch", { name })).toHaveAttribute( + "aria-disabled", + "true", + ); }); it("says why a save failed and lets the switch be tried again", async () => { @@ -630,11 +630,11 @@ it("offers no switch while the agent's instructions can't be read here", () => { // Each setting's row says why, check-ins too. expect( screen.getAllByText("This agent's instructions could not be read"), - ).toHaveLength(2); + ).toHaveLength(3); expect(save).not.toHaveBeenCalled(); }); -it("checks in every hour only while memory is on, with a timer in the owner's DM", async () => { +it("checks in only while memory is on, hourly and nightly apart, each as an Interest and a timer in the owner's DM", async () => { const user = userEvent.setup(); const { relay } = community(ada.pubkey); const { agents, save } = host(); @@ -647,28 +647,28 @@ it("checks in every hour only while memory is on, with a timer in the owner's DM configBase: base, }; const { rerender } = render(); - const hourly = () => screen.getByRole("switch", { name: "Every hour" }); + const hourly = () => screen.getByRole("switch", { name: "Hourly notes" }); + const nightly = () => screen.getByRole("switch", { name: "Nightly review" }); // Memory is off, so check-ins can't be turned on. expect(hourly()).toHaveAttribute("aria-disabled", "true"); - expect(screen.getByText("Turn on Remembers you first.")).toBeVisible(); + expect(nightly()).toHaveAttribute("aria-disabled", "true"); + expect(screen.getAllByText("Turn on Remembers you first.")).toHaveLength(2); const on = { ...off, config: { instructions: remembering } } as Agent; rerender(); expect(hourly()).not.toBeChecked(); await user.click(hourly()); - // The timer first, then the instructions that say what it is for. - expect(save).toHaveBeenCalledTimes(2); - const [[, timers], [, instructions]] = save.mock.calls as [ - [string, AgentChange], - [string, AgentChange], - ]; - expect(instructions).toEqual({ - config: { instructions: withFeature(remembering, checkinHourly, true) }, - base, + // The Interest and timer only: what every turn carries is left alone. + expect(save).toHaveBeenCalledTimes(1); + const [[, timers]] = save.mock.calls as [[string, AgentChange]]; + expect(timers.config).toBeUndefined(); + expect(timers.attention?.["interest/bestie-notes"]).toEqual({ + type: "interest", + instructions: notes.timer?.interest, }); - expect(timers.attention?.["watch/bestie-hourly-check-in"]).toMatchObject({ + expect(timers.attention?.["watch/bestie-notes"]).toMatchObject({ type: "timer", - interest_id: "bestie-hourly-check-in", + interest_id: "bestie-notes", interval_secs: 3600, max_occurrences: null, expires_at: null, @@ -679,15 +679,12 @@ it("checks in every hour only while memory is on, with a timer in the owner's DM for (const [slug, value] of Object.entries(timers.attention ?? {})) expect(validateObject(slug, value)).toBeUndefined(); - // On means the section and the timer are both there. + // On means the Interest and the timer are both there. const checking = { ...on, - config: { instructions: withFeature(remembering, checkinHourly, true) }, attention: { - "interest/bestie-hourly-check-in": { value: { type: "interest" } }, - "watch/bestie-hourly-check-in": { - value: { type: "timer", enabled: true }, - }, + "interest/bestie-notes": { value: { type: "interest" } }, + "watch/bestie-notes": { value: { type: "timer", enabled: true } }, }, } as unknown as Agent; rerender(); @@ -700,46 +697,80 @@ it("checks in every hour only while memory is on, with a timer in the owner's DM expect(hourly()).not.toBeChecked(); rerender(); - const removed = { + // Nightly review is its own switch: Hourly notes off leaves it alone. + save.mockClear(); + rerender(); + await user.click(nightly()); + expect(save).toHaveBeenCalledTimes(1); + const [[, review]] = save.mock.calls as [[string, AgentChange]]; + expect(Object.keys(review.attention ?? {})).toEqual([ + "interest/bestie-dream", + "watch/bestie-dream", + ]); + expect(review.attention?.["watch/bestie-dream"]).toMatchObject({ + interest_id: "bestie-dream", + interval_secs: 86_400, + channel: DM, + }); + const dreamTimer = review.attention?.["watch/bestie-dream"] as + | { armed_at: number } + | undefined; + expect(new Date((dreamTimer?.armed_at ?? 0) * 1000).getHours()).toBe(3); + for (const [slug, value] of Object.entries(review.attention ?? {})) + expect(validateObject(slug, value)).toBeUndefined(); + + const both = { + ...checking, attention: { - "interest/bestie-hourly-check-in": null, - "watch/bestie-hourly-check-in": null, + ...checking.attention, + "interest/bestie-dream": { value: { type: "interest" } }, + "watch/bestie-dream": { value: { type: "timer", enabled: true } }, }, - }; - // Memory off takes check-ins with it, and leaves Attention's switch alone. + } as unknown as Agent; + rerender(); + expect(hourly()).toBeChecked(); + expect(nightly()).toBeChecked(); + // Hourly notes off removes only its Interest and timer. save.mockClear(); - await user.click(screen.getByRole("switch", { name: "Remembers you" })); + await user.click(hourly()); expect(save.mock.calls).toEqual([ - [ada.key, removed], - [ada.key, { config: { instructions: "Be brief." }, base }], + [ + ada.key, + { + attention: { + "interest/bestie-notes": null, + "watch/bestie-notes": null, + }, + }, + ], ]); - - // Check-ins off removes only their section and timer. + // Memory off takes both with it, and leaves Attention's switch alone. save.mockClear(); - await user.click(hourly()); + await user.click(screen.getByRole("switch", { name: "Remembers you" })); expect(save.mock.calls).toEqual([ - [ada.key, removed], - [ada.key, { config: { instructions: remembering.trimEnd() }, base }], + [ + ada.key, + { + attention: { + "interest/bestie-notes": null, + "watch/bestie-notes": null, + "interest/bestie-dream": null, + "watch/bestie-dream": null, + }, + }, + ], + [ada.key, { config: { instructions: "Be brief." }, base }], ]); // Left on after memory went some other way, it can still be turned off. rerender( - , + , ); expect(hourly()).toBeChecked(); expect(hourly()).not.toHaveAttribute("aria-disabled", "true"); }); -/** Ada with memory on and check-ins off, ready to turn Every hour on. */ +/** Ada with memory on and check-ins off, ready to turn Hourly notes on. */ function remembering(attentionEnabled = false) { return { ...ada, @@ -747,75 +778,63 @@ function remembering(attentionEnabled = false) { attentionEnabled, } as Agent; } -const TIMER = "watch/bestie-hourly-check-in"; -const INTEREST = "interest/bestie-hourly-check-in"; +const TIMER = "watch/bestie-notes"; +const INTEREST = "interest/bestie-notes"; -it("undoes a failed check-in's timer, so none runs behind a switch that reads off", async () => { +it("says why a check-in couldn't be saved and lets its switch be tried again", async () => { const user = userEvent.setup(); const { relay } = community(ada.pubkey); const { agents, save } = host(); const Tab = bestieTab(relay, agents).component; render(); - const hourly = screen.getByRole("switch", { name: "Every hour" }); - save - .mockResolvedValueOnce() - .mockRejectedValueOnce(new Error("Instructions changed elsewhere")); + const hourly = screen.getByRole("switch", { name: "Hourly notes" }); + save.mockRejectedValueOnce(new Error("Connect to the community to save")); await user.click(hourly); expect(await screen.findByRole("alert")).toHaveTextContent( - "Instructions changed elsewhere", + "Connect to the community to save", ); - // The timer and its Interest are removed again. - expect(save).toHaveBeenCalledTimes(3); - expect(save.mock.calls[2]).toEqual([ - ada.key, - { attention: { [INTEREST]: null, [TIMER]: null } }, - ]); expect(hourly).not.toBeChecked(); + expect(save).toHaveBeenCalledTimes(1); // Trying again saves it all again. save.mockClear(); await user.click(hourly); - expect(save).toHaveBeenCalledTimes(2); + expect(save).toHaveBeenCalledTimes(1); expect(screen.queryByRole("alert")).toBeNull(); }); -it("offers to remove a timer left without its section when undoing fails too", async () => { +it("offers to remove what is left of a check-in whose Interest or timer is gone", async () => { const user = userEvent.setup(); const { relay } = community(ada.pubkey); const { agents, save } = host(); const Tab = bestieTab(relay, agents).component; - const { rerender } = render(); - save - .mockResolvedValueOnce() - .mockRejectedValueOnce(new Error("Instructions changed elsewhere")) - .mockRejectedValueOnce(new Error("Offline")); - await user.click(screen.getByRole("switch", { name: "Every hour" })); - expect(await screen.findByRole("alert")).toHaveTextContent( - "Instructions changed elsewhere Its timer may still be saved; remove it below.", + const left = (slug: string, value: object) => + ({ + ...remembering(true), + attention: { [slug]: { slug, value, modifiedAt: 1 } }, + }) as unknown as Agent; + const { rerender } = render( + , ); - // The agent as saved: the timer is there, its section isn't. - const [[, timers]] = save.mock.calls as [[string, AgentChange]]; - const left = { - ...remembering(), - attentionEnabled: true, - attention: Object.fromEntries( - Object.entries(timers.attention ?? {}).map(([slug, value]) => [ - slug, - { slug, value, modifiedAt: 1 }, - ]), - ), - } as unknown as Agent; - rerender(); - expect(screen.getByRole("switch", { name: "Every hour" })).not.toBeChecked(); - save.mockClear(); - await user.click( + expect( + screen.getByRole("switch", { name: "Hourly notes" }), + ).not.toBeChecked(); + const remove = () => screen.getByRole("button", { name: "Remove", - description: "Every hour's timer is still saved", - }), - ); + description: "Hourly notes is partly saved", + }); + await user.click(remove()); expect(save.mock.calls[0]).toEqual([ ada.key, - { attention: { [INTEREST]: null, [TIMER]: null } }, + { attention: { [TIMER]: null } }, + ]); + // The same when the Interest is what is left, which holds the steps. + save.mockClear(); + rerender(); + await user.click(remove()); + expect(save.mock.calls[0]).toEqual([ + ada.key, + { attention: { [INTEREST]: null } }, ]); }); @@ -826,25 +845,18 @@ it("says a check-in paused in Attention is paused, and how to restart it", async const Tab = bestieTab(relay, agents).component; const paused = { ...remembering(true), - config: { - instructions: withFeature( - withFeature("Be brief.", memory, true), - checkinHourly, - true, - ), - }, attention: { [INTEREST]: { value: { type: "interest" } }, [TIMER]: { value: { type: "timer", enabled: false } }, }, } as unknown as Agent; render(); - const hourly = screen.getByRole("switch", { name: "Every hour" }); + const hourly = screen.getByRole("switch", { name: "Hourly notes" }); // Still on, so Off still removes it. expect(hourly).toBeChecked(); expect( screen.getByText( - "Paused in Attention. Turn Every hour off and on again to restart it.", + "Paused in Attention. Turn Hourly notes off and on again to restart it.", ), ).toBeVisible(); await user.click(hourly); @@ -878,9 +890,9 @@ it("shows a Former Bestie tab while a former Bestie has a setting on, with a way screen.getByText("Ada is no longer your Bestie. These are still on."), ).toBeVisible(); expect(screen.getByRole("switch", { name: "Remembers you" })).toBeChecked(); - const hourly = screen.getByRole("switch", { name: "Every hour" }); + const hourly = screen.getByRole("switch", { name: "Hourly notes" }); expect(hourly).toHaveAttribute("aria-disabled", "true"); - expect(screen.getByText("Only your Bestie checks in.")).toBeVisible(); + expect(screen.getAllByText("Only your Bestie checks in.")).toHaveLength(2); expect(screen.getByText("Private channels Ada can read")).toBeVisible(); await user.click( @@ -1276,24 +1288,18 @@ it("lets a former Bestie's leftover check-in be turned off", async () => { const Tab = formerBestieTab(relay, agents).component; const checking = { ...ada, - config: { - instructions: withFeature( - withFeature("Be brief.", memory, true), - checkinHourly, - true, - ), - }, + config: { instructions: withFeature("Be brief.", memory, true) }, attention: { - "interest/bestie-hourly-check-in": {}, - "watch/bestie-hourly-check-in": {}, + "interest/bestie-notes": {}, + "watch/bestie-notes": {}, }, } as unknown as Agent; render(); - const hourly = screen.getByRole("switch", { name: "Every hour" }); + const hourly = screen.getByRole("switch", { name: "Hourly notes" }); expect(hourly).toBeChecked(); expect(hourly).not.toHaveAttribute("aria-disabled", "true"); await user.click(hourly); expect(save).toHaveBeenLastCalledWith(ada.key, { - config: { instructions: withFeature("Be brief.", memory, true) }, + attention: { "interest/bestie-notes": null, "watch/bestie-notes": null }, }); }); diff --git a/src/bundled/bestie/BestieTab.tsx b/src/bundled/bestie/BestieTab.tsx index 6f6b178566..277ba80cb9 100644 --- a/src/bundled/bestie/BestieTab.tsx +++ b/src/bundled/bestie/BestieTab.tsx @@ -1,5 +1,5 @@ import { useEffect, useRef, useState } from "react"; -import { watchSlug } from "../../features/agents/attention"; +import { interestSlug, watchSlug } from "../../features/agents/attention"; import type { Agent, AgentAddedTab, @@ -334,6 +334,8 @@ function FeatureSwitch({ ); const instructions = instructionsOf(agent); const timer = feature.timer && agent.attention[watchSlug(feature.timer.id)]; + const part = + feature.timer && (timer || agent.attention[interestSlug(feature.timer.id)]); const required = FEATURES.find(({ id }) => id === feature.requires); const blocked = !!required && !featureOn(agent, required); const checked = featureOn(agent, feature); @@ -425,12 +427,13 @@ function FeatureSwitch({ disabled={saving || ((blocked || formerCheckIn) && !checked)} onCheckedChange={(on) => void save(on)} /> - {/* A timer left without its section, as when undoing a failed On - failed too, still runs: Remove takes it away. */} - {timer && !checked && ( + {/* A timer or Interest left without the rest, as when undoing a failed + On failed too, or one was deleted in Attention: Remove takes it + away. */} + {part && !checked && ( (