Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/bundled/bestie/BestieTab.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ afterEach(() => {

const OWNER = "f".repeat(64);
const SCOPE = `https://relay.example:${OWNER}`;
const NO_CHANNELS = { channels: [] };
const member = (name: string, digit: string): Agent =>
({
key: digit.repeat(64),
Expand Down Expand Up @@ -64,6 +65,12 @@ function community(saved: string | undefined, agents = [ada, bea]) {
status: "ready",
scope: SCOPE,
session: {
viewer: OWNER,
channels: {
list: () => NO_CHANNELS,
subscribeList: () => () => {},
ensureList: () => () => {},
},
mainAgent,
agentChoices: {
snapshot: () => choices,
Expand Down Expand Up @@ -111,7 +118,7 @@ it("shows on the saved Bestie only, and moves when Change picks another", async
expect(screen.getByText("This is your Bestie")).toBeVisible();
// A type without instructions has nothing to switch.
expect(screen.getByText("Remembers you")).toBeVisible();
expect(screen.queryByRole("switch")).toBeNull();
expect(screen.queryByRole("switch", { name: "Remembers you" })).toBeNull();
changed.mockClear();
await user.click(screen.getByRole("button", { name: "Change" }));
// Change offers every other agent you can make Bestie.
Expand Down Expand Up @@ -250,7 +257,7 @@ it("says why a save failed and lets the switch be tried again", async () => {
"Connect to the community to save",
);
expect(remembers).not.toBeChecked();
expect(remembers).toBeEnabled();
expect(remembers).not.toHaveAttribute("aria-disabled", "true");
// A config with no instructions yet gets the section alone.
expect(save).toHaveBeenCalledWith(ada.key, {
config: { instructions: withFeature("", memory, true) },
Expand All @@ -272,7 +279,7 @@ it("offers no switch while the agent's instructions can't be read here", () => {
}
/>,
);
expect(screen.queryByRole("switch")).toBeNull();
expect(screen.queryByRole("switch", { name: "Remembers you" })).toBeNull();
expect(
screen.getByText("This agent's instructions could not be read"),
).toBeVisible();
Expand Down
6 changes: 2 additions & 4 deletions src/bundled/bestie/BestieTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
restoredFeature,
withFeature,
} from "./features";
import { PrivateChannels } from "./PrivateChannels";

type TabAgents = Pick<Agents, "save" | "types">;

Expand Down Expand Up @@ -169,11 +170,8 @@ function BestieTab({
title="Check-ins"
subtitle="Every hour. Not set up yet."
/>
<PreferenceRow
title="Private channels Bestie can read"
subtitle="Not set up yet. Bestie can already read every public channel."
/>
</SettingsGroup>
<PrivateChannels session={session} bestie={agent.pubkey} />
{bestie.view}
</div>
);
Expand Down
174 changes: 174 additions & 0 deletions src/bundled/bestie/PrivateChannels.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// @vitest-environment jsdom
import "@testing-library/jest-dom/vitest";
import { cleanup, render, screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, expect, it, vi } from "vitest";
import type { ChannelSummary } from "../../features/relay/contracts";
import type { RelaySession } from "../../features/relay/session";
import { PrivateChannels, privateChannels } from "./PrivateChannels";

afterEach(cleanup);

const YOU = "f".repeat(64);
const BESTIE = "b".repeat(64);
const OTHER = "c".repeat(64);
const channel = (
name: string,
members: readonly string[],
extra: Partial<ChannelSummary> = {},
): ChannelSummary => ({
id: `id-${name}`,
name,
channelType: "stream",
private: true,
members,
...extra,
});

it("keeps only your private channels, split by Bestie's membership, most recent first", () => {
const { joined, others } = privateChannels(
[
channel("quiet", [YOU]),
channel("busy", [YOU], { lastActivityAt: 20 }),
channel("older", [YOU], { lastActivityAt: 10 }),
channel("shared", [YOU, BESTIE]),
channel("forum", [YOU], { channelType: "forum" }),
{
id: "id-public",
name: "public",
channelType: "stream",
members: [YOU],
},
channel("not-yours", [OTHER, BESTIE]),
channel("dm", [YOU, BESTIE], { channelType: "dm" }),
channel("solo", [YOU, BESTIE], { channelType: "solo" }),
channel("archived", [YOU], { archived: true }),
channel("huddle", [YOU], { huddle: true }),
],
YOU,
BESTIE,
);
expect(joined.map(({ name }) => name)).toEqual(["shared"]);
expect(others.map(({ name }) => name)).toEqual([
"busy",
"older",
"forum",
"quiet",
]);
});

function session(channels: ChannelSummary[], removal = "confirmed") {
const roles: Record<string, string> = { [BESTIE]: "bot" };
const add = vi.fn(async (id: string) => {
if (id === "id-beta") throw new Error("Not confirmed. Try again.");
});
const run = vi.fn(async () => {});
const list = { channels };
const value = {
viewer: YOU,
outbox: { supports: () => true },
channels: {
list: () => list,
subscribeList: () => () => {},
ensureList: () => () => {},
},
memberAdditions: { add },
memberAdministration: {
refresh: vi.fn(async () => {}),
run,
snapshot: vi.fn(() =>
run.mock.calls.length
? {
status: "ready",
authority: { roles: {} },
operation: {
change: { pubkey: BESTIE, role: "remove" },
status: removal,
},
...(removal !== "confirmed" && {
error: "The change may have taken effect. Refresh to check.",
}),
}
: { status: "ready", authority: { roles } },
),
},
} as unknown as RelaySession;
return { value, add, run };
}

it("removes Bestie from a channel it is in, as the Members dialog does", async () => {
const user = userEvent.setup();
const { value, run } = session([channel("shared", [YOU, BESTIE])]);
render(<PrivateChannels session={value} bestie={BESTIE} />);
await user.click(
screen.getByRole("button", { name: "Remove Bestie from #shared" }),
);
expect(run).toHaveBeenCalledWith("id-shared", {
pubkey: BESTIE,
expectedRole: "bot",
role: "remove",
});
expect(screen.queryByRole("alert")).toBeNull();
// Creating channels with Bestie is shown, not offered yet.
expect(
screen.getByRole("switch", {
name: "Also add Bestie to private channels I create",
}),
).toHaveAttribute("aria-disabled", "true");
});

it("adds Bestie to one channel, or to all after asking, and reports what didn't confirm", async () => {
const user = userEvent.setup();
const { value, add } = session([
channel("alpha", [YOU], { lastActivityAt: 5 }),
channel("beta", [YOU], { lastActivityAt: 9 }),
]);
render(<PrivateChannels session={value} bestie={BESTIE} />);
expect(screen.getByText(/Not in any of your private channels/)).toBeVisible();

await user.click(screen.getByRole("button", { name: "Choose…" }));
const items = await screen.findAllByRole("menuitem");
expect(items.map((item) => item.textContent)).toEqual(["#beta", "#alpha"]);
await user.click(screen.getByRole("menuitem", { name: "#alpha" }));
expect(add).toHaveBeenCalledWith("id-alpha", BESTIE);

add.mockClear();
expect(screen.getByText(/2 channels without Bestie/)).toBeVisible();
expect(
screen.getByRole("button", { name: "Add to all" }),
).toHaveAccessibleDescription("Add to all my private channels");
await user.click(screen.getByRole("button", { name: "Add to all" }));
const ask = await screen.findByRole("alertdialog", {
name: "Add Bestie to 2 private channels?",
});
expect(add).not.toHaveBeenCalled();
await user.click(within(ask).getByRole("button", { name: "Add to all" }));
expect(add.mock.calls).toEqual([
["id-beta", BESTIE],
["id-alpha", BESTIE],
]);
expect(await screen.findByRole("alert")).toHaveTextContent(
"#beta: Not confirmed. Try again.",
);
});

it("says when a removal isn't confirmed, and offers none from a cached roster", async () => {
const user = userEvent.setup();
const { value } = session(
[
channel("shared", [YOU, BESTIE]),
channel("offline", [YOU, BESTIE], { cached: true }),
],
"uncertain",
);
render(<PrivateChannels session={value} bestie={BESTIE} />);
expect(
screen.getByRole("button", { name: "Remove Bestie from #offline" }),
).toBeDisabled();
await user.click(
screen.getByRole("button", { name: "Remove Bestie from #shared" }),
);
expect(await screen.findByRole("alert")).toHaveTextContent(
"#shared: The change may have taken effect. Refresh to check.",
);
});
Loading
Loading