Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ function open(search = SIGNED) {
}

function renderPage(ui: ReactNode = <AgentConsent email="ada@example.test" />) {
// The read's own retries (a server or network failure) come at once.
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false } },
defaultOptions: { queries: { retry: false, retryDelay: 0 } },
});
return render(
<QueryClientProvider client={queryClient}>{ui}</QueryClientProvider>,
Expand Down Expand Up @@ -1058,6 +1059,9 @@ describe('what stands in the way', () => {
expect(
screen.queryByText(/belongs to one organization/),
).not.toBeInTheDocument();
expect(
screen.queryByRole('button', { name: 'Try again' }),
).not.toBeInTheDocument();
});

it('says an app Tale could not read cannot be connected', async () => {
Expand All @@ -1069,6 +1073,73 @@ describe('what stands in the way', () => {
await screen.findByText(/Tale could not read this app’s details/),
).toBeVisible();
expect(screen.getByRole('button', { name: 'Connect' })).toBeDisabled();
// The server's answer is final: asking again would not change it.
expect(
screen.queryByRole('button', { name: 'Try again' }),
).not.toBeInTheDocument();
});

describe('when the server or the network failed to answer', () => {
/** The read's own retries are used up; the person asks again. */
function failThrice(failure: unknown) {
h.context
.mockRejectedValueOnce(failure)
.mockRejectedValueOnce(failure)
.mockRejectedValueOnce(failure);
}

it.each([
['the server failed', new BackendApiError(503, 'down', 'UNAVAILABLE')],
['the network failed', new TypeError('Failed to fetch')],
])(
'offers Try again in place of Connect, and reads the app again, when %s',
async (_case, failure) => {
failThrice(failure);
const { user } = renderPage();
expect(
await screen.findByText(/Tale could not read this app’s details/),
).toBeVisible();
expect(
screen.queryByRole('button', { name: 'Connect' }),
).not.toBeInTheDocument();

await user.click(screen.getByRole('button', { name: 'Try again' }));

expect(
await screen.findByRole('radio', { name: /Edit automations/ }),
).toBeVisible();
expect(
screen.queryByRole('button', { name: 'Try again' }),
).not.toBeInTheDocument();
expect(await connectButton()).toBeEnabled();
expect(h.context).toHaveBeenCalledTimes(4);
},
);

it('keeps Try again while the answer keeps failing, and Cancel all the while', async () => {
h.context.mockRejectedValue(new BackendApiError(502, 'bad', 'GATEWAY'));
const { user } = renderPage();
await screen.findByText(/Tale could not read this app’s details/);

await user.click(screen.getByRole('button', { name: 'Try again' }));

expect(
await screen.findByRole('button', { name: 'Try again' }),
).toBeVisible();
expect(screen.getByRole('button', { name: 'Cancel' })).toBeEnabled();
await user.click(screen.getByRole('button', { name: 'Cancel' }));
expect(h.consent).toHaveBeenCalledExactlyOnceWith({
accept: false,
oauth_query: SIGNED,
});
});

it('passes an accessibility audit', async () => {
failThrice(new BackendApiError(503, 'down', 'UNAVAILABLE'));
const { container } = renderPage();
await screen.findByRole('button', { name: 'Try again' });
await checkAccessibility(container);
});
});

it('stops saying it is loading once it has the answer', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { useEffect, useId, useMemo, useState } from 'react';
import { useConfirmPassword } from '@/app/features/auth/hooks/use-confirm-password';
import { isSessionFresh } from '@/app/lib/auth/session-freshness';
import { sessionQueryOptions } from '@/app/lib/auth/session-query';
import { agentConsentContextQuery } from '@/app/lib/backend/agent-connections';
import {
agentConsentContextQuery,
isRetryableFailure,
} from '@/app/lib/backend/agent-connections';
import { BackendApiError } from '@/app/lib/backend/api-client';
import { authClient } from '@/lib/auth-client';
import { useT } from '@/lib/i18n/client';
Expand Down Expand Up @@ -266,6 +269,10 @@ export function AgentConsent({ email }: { email: string }) {
? t('consent.errors.notEligible')
: null;
const loading = !expired && context === undefined && loadFailure === null;
// The app's details could not be read because the server or the network
// failed, which asking again can change; an answer that refused is final.
const canRetry =
!expired && contextQuery.isError && isRetryableFailure(contextQuery.error);
const nothingAllowed =
context !== undefined && context.eligible && organizationId === null;
// A choice exists only for an organization the person may connect to.
Expand Down Expand Up @@ -437,14 +444,20 @@ export function AgentConsent({ email }: { email: string }) {
) : null}
{!expired ? (
<Stack gap={2}>
<Button
fullWidth
disabled={!canConnect}
aria-describedby={mustConfirm ? confirmId : undefined}
onClick={() => void connect()}
>
{busy ? t('consent.connecting') : t('consent.connect')}
</Button>
{canRetry ? (
<Button fullWidth onClick={() => void contextQuery.refetch()}>
{tAuth('oauth.retry')}
</Button>
) : (
<Button
fullWidth
disabled={!canConnect}
aria-describedby={mustConfirm ? confirmId : undefined}
onClick={() => void connect()}
>
{busy ? t('consent.connecting') : t('consent.connect')}
</Button>
)}
<Button
variant="secondary"
fullWidth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,104 @@ describe('AgentCell [AGCONN-R24]', () => {
});
});

/** What a phone still has to tell, under the agent's name. */
describe('AgentCell where the table has no column for it [AGCONN-R24]', () => {
const HOUR = 60 * 60 * 1000;

it('carries what the agent may do and when it last called', () => {
render(
<AgentCell
connection={connection({
scopes: ['tale:mcp', 'tale:automations.write', 'tale:settings.write'],
lastUsedAt: Date.now() - 3 * HOUR,
})}
carries={{ access: true }}
/>,
);
expect(screen.getByText('Claude Code')).toBeInTheDocument();
expect(screen.getByText('Edit automations')).toBeInTheDocument();
expect(screen.getByText('Change settings')).toBeInTheDocument();
expect(screen.getByText('Last used 3 hours ago')).toBeInTheDocument();
});

it('says so when the agent has never called', () => {
render(
<AgentCell
connection={connection({ lastUsedAt: null })}
carries={{ access: true }}
/>,
);
expect(screen.getByText('Never used')).toBeInTheDocument();
expect(screen.queryByText(/^Last used/)).not.toBeInTheDocument();
});

it('says whose agent it is when the person has no column either', () => {
const { unmount } = render(
<AgentCell
connection={connection()}
viewerUserId="olav"
carries={{ person: true }}
/>,
);
expect(screen.getByText('Ada Lovelace')).toBeInTheDocument();
expect(screen.getByText('ada@example.test')).toBeInTheDocument();
// Asked for only the person, it adds nothing about access.
expect(screen.queryByText('Read and test')).not.toBeInTheDocument();
expect(screen.queryByText('Never used')).not.toBeInTheDocument();
unmount();

render(
<AgentCell
connection={connection()}
viewerUserId="ada"
carries={{ person: true }}
/>,
);
expect(screen.getByText('You')).toBeInTheDocument();
});

it('adds nothing where the table has the columns itself', () => {
render(
<AgentCell
connection={connection({ lastUsedAt: Date.now() - HOUR })}
carries={{ person: false, access: false }}
/>,
);
expect(screen.queryByText('Read and test')).not.toBeInTheDocument();
expect(screen.queryByText(/Last used|Never used/)).not.toBeInTheDocument();
expect(screen.queryByText('Ada Lovelace')).not.toBeInTheDocument();
});

it('keeps an ended connection’s ending beside what it held', () => {
render(
<AgentCell
connection={connection({
revokedAt: Date.UTC(2026, 9, 12, 12),
revokeReason: 'admin',
scopes: ['tale:mcp', 'tale:automations.write'],
})}
carries={{ access: true }}
/>,
);
expect(screen.getByText(/^Disconnected /)).toHaveTextContent('by an Admin');
expect(screen.getByText('Edit automations')).toBeInTheDocument();
});

it('passes an accessibility audit with everything carried', async () => {
const { container } = render(
<AgentCell
connection={connection({
scopes: ['tale:mcp', 'tale:automations.write', 'tale:settings.write'],
lastUsedAt: Date.now() - HOUR,
})}
viewerUserId="olav"
carries={{ person: true, access: true }}
/>,
);
await checkAccessibility(container);
});
});

/** Whose agent it is. */
describe('AgentPersonCell [AGCONN-R24]', () => {
it('names the person and, under their name, their address', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ function useEndReason() {

interface AgentCellProps {
connection: AgentConnection;
/** The signed-in person, so their own agents read as theirs. */
viewerUserId?: string | undefined;
/**
* What the table has no column for at this width, carried under the
* agent's name instead: on a phone a person still has to tell three
* "Claude Code" rows apart — whose they are, what they may do, whether
* they are in use — before cutting one off.
*/
carries?: { person?: boolean; access?: boolean };
}

/**
Expand All @@ -40,7 +49,11 @@ interface AgentCellProps {
* registered itself and chose its own name. An ended connection says when
* and why instead.
*/
export function AgentCell({ connection }: AgentCellProps) {
export function AgentCell({
connection,
viewerUserId,
carries,
}: AgentCellProps) {
const { t } = useT('agentConnections');
const { formatDate } = useFormatDate();
const endReason = useEndReason();
Expand All @@ -67,14 +80,46 @@ export function AgentCell({ connection }: AgentCellProps) {
caption = <Badge variant="orange">{t('list.unverified')}</Badge>;
}

return (
const cell = (
<TableIconCell
icon={<Bot />}
label={connection.clientName}
title={connection.clientName}
caption={caption}
/>
);
if (!carries?.person && !carries?.access) return cell;

const lastUsed =
connection.lastUsedAt === null
? t('list.neverUsed')
: t('list.lastUsedAt', {
when: formatDate(new Date(connection.lastUsedAt), 'relative'),
});

return (
<div className="flex min-w-0 flex-col gap-1.5">
{cell}
{/* Under the name, not the mark: the mark's 20px slot and the 8px gap
are the cell's own. */}
<div className="flex min-w-0 flex-col gap-1 pl-7">
{carries.person ? (
<AgentPersonCell
connection={connection}
viewerUserId={viewerUserId}
/>
) : null}
{carries.access ? (
<>
<AgentAccessCell scopes={connection.scopes} />
<Text as="span" variant="caption">
{lastUsed}
</Text>
</>
) : null}
</div>
</div>
);
}

interface AgentPersonCellProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@ describe('the connected agents fit the screen [AGCONN-R24]', () => {
screen.queryByRole('columnheader', { name: 'Last used' }),
).not.toBeInTheDocument();
});

it('tells the agents apart on a phone: what each may do and when it last called, under its name', async () => {
await page.viewport(320, 700);
renderPage();
// The first agent holds one level and called three hours ago; the second
// holds everything, an app that registered itself, and never called.
expect(screen.getByText('Edit automations')).toBeVisible();
expect(screen.getAllByText('Last used 3 hours ago')[0]).toBeVisible();
expect(screen.getByText('Connect AI subscriptions')).toBeVisible();
expect(screen.getByText('Never used')).toBeVisible();
});
});

describe('the connected agents in both themes [AGCONN-R24]', () => {
Expand Down
Loading