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 @@ -50,6 +50,10 @@ const {
),
deleteProjectNote: vi.fn(async (_noteId: string): Promise<void> => {}),
cancelSession: vi.fn(async (_sessionId: string): Promise<void> => {}),
// "Mark as read" on a note's context menu.
setProjectNoteRead: vi.fn(
async (_noteId: string, _read: boolean): Promise<ProjectNote | null> => null
),
listChildSessions: vi.fn(async (_sessionId: string): Promise<ChildSessionInfo[]> => []),
// What the note dialog's `#note:` references are built from once it opens.
listChildNotes: vi.fn(async (_projectNoteId: string) => []),
Expand Down Expand Up @@ -149,6 +153,7 @@ vi.mock('../../commands', async (importOriginal) => ({
import ProjectSection from './ProjectSection.svelte';
import { reactiveProps } from '../sessions/testStubs/reactiveProps.svelte';
import { sessionWakeupsStore } from '../../stores/sessionWakeups.svelte';
import { replyDraftPresence } from '../sessions/replyDrafts';

const project = {
id: 'project-1',
Expand Down Expand Up @@ -296,6 +301,23 @@ function rowFor(sessionId: string): HTMLElement {
return row;
}

/** What the context menu holds for a session's row, found by the row's menu key. */
function menuActionFor(sessionId: string): HTMLElement {
const key = rowFor(sessionId).dataset.contextMenuKey;
const action = document.querySelector<HTMLElement>(
`[data-testid="context-menu-action"][data-key="${key}"]`
);
if (!action) throw new Error(`no context-menu action for ${sessionId}`);
return action;
}

/** The row's menu item marking its note `state`, when the menu offers it. */
function menuMarkButton(sessionId: string, state: 'read' | 'unread'): HTMLButtonElement | null {
return menuActionFor(sessionId).querySelector<HTMLButtonElement>(
`[data-testid="menu-mark-${state}"]`
);
}

function resumeButton(sessionId: string): HTMLButtonElement | null {
return rowFor(sessionId).querySelector<HTMLButtonElement>('[data-testid="row-resume"]');
}
Expand Down Expand Up @@ -926,7 +948,10 @@ describe('ProjectSection sessions waiting on repo sessions', () => {
content: '',
});

it('shows a session that waits before writing its note as waiting, with the badges of what it waits on', async () => {
/** Everything a row says in words: its title and its subtitle. */
const rowText = (row: HTMLElement) => `${row.dataset.title} ${row.dataset.secondaryMeta}`;

it('shows a session that waits before writing its note as running, with the badges of what it waits on', async () => {
listNotes(unwritten);
commandMocks.getSessionWakeup.mockImplementation(async () =>
armedWakeup('child-1', 'child-2', 'child-3')
Expand All @@ -941,25 +966,48 @@ describe('ProjectSection sessions waiting on repo sessions', () => {
// Asked for once: the wake-up was armed before this section was listening.
expect(commandMocks.getSessionWakeup.mock.calls).toEqual([['session-waiting']]);
const row = rowFor('session-waiting');
expect(row.dataset.type).toBe('waiting-note');
expect(row.dataset.title).toBe('Waiting on 2 repo sessions');
// The row a running session has: the spinner's type, and the badges
// alone as the subtitle. Nothing on it says the session is waiting.
expect(row.dataset.type).toBe('generating-note');
expect(row.dataset.title).toBe('Generating note…');
expect(row.dataset.secondaryMeta).toBe('');
expect(rowText(row)).not.toMatch(/waiting/i);
expect(row.dataset.childBadges).toBe('child-1:running child-2:paused');
// Staged resumes it; there is nothing for the user to resume.
expect(resumeButton('session-waiting')).toBeNull();
// No note to open yet, so the row opens the chat, which says what it waits on.
expect(row.querySelector('[data-testid="row-open"]')).not.toBeNull();
// Like a running row, it opens the chat, which says what it waits on.
clickRow('session-waiting');
expect(noteModal.props).toMatchObject({
noteId: 'note-waiting',
sessionId: 'session-waiting',
chatOpen: true,
});

unmount(section);
});

it('shows a waiting session as running before its repo sessions are read, and with none in flight', async () => {
listNotes(unwritten);
commandMocks.getSessionWakeup.mockImplementation(async () => armedWakeup('child-1'));
const section = mountSection();
await settle();

const row = rowFor('session-waiting');
expect(row.dataset.type).toBe('generating-note');
expect(row.dataset.secondaryMeta).toBe('Generating note');
expect(row.dataset.childBadges).toBe('');
expect(rowText(row)).not.toMatch(/waiting/i);

unmount(section);
});

it('drops a badge when a session being waited on ends, and recounts', async () => {
it('drops a badge when a session being waited on ends, and stays running', async () => {
listNotes(unwritten);
commandMocks.getSessionWakeup.mockImplementation(async () => armedWakeup('child-1', 'child-2'));
listChildren({ 'session-waiting': [childSession('child-1'), childSession('child-2')] });
const section = mountSection();
await settle();
expect(rowFor('session-waiting').dataset.title).toBe('Waiting on 2 repo sessions');
expect(rowFor('session-waiting').dataset.childBadges).toBe('child-1:running child-2:running');

listChildren({ 'session-waiting': [childSession('child-2')] });
await emitSessionStatus({
Expand All @@ -970,20 +1018,20 @@ describe('ProjectSection sessions waiting on repo sessions', () => {
});

const row = rowFor('session-waiting');
expect(row.dataset.type).toBe('waiting-note');
expect(row.dataset.title).toBe('Waiting on 1 repo session');
expect(row.dataset.type).toBe('generating-note');
expect(rowText(row)).not.toMatch(/waiting/i);
expect(row.dataset.childBadges).toBe('child-2:running');

unmount(section);
});

it('stops showing a session as waiting once its wake-up is gone', async () => {
it('stops showing a session as running once its wake-up is gone', async () => {
listNotes(unwritten);
commandMocks.getSessionWakeup.mockImplementation(async () => armedWakeup('child-1'));
listChildren({ 'session-waiting': [childSession('child-1')] });
const section = mountSection();
await settle();
expect(rowFor('session-waiting').dataset.type).toBe('waiting-note');
expect(rowFor('session-waiting').dataset.type).toBe('generating-note');

// Stop waiting, here or in another window: the repo session keeps running.
await emitWakeup('session-waiting', NO_WAKEUP);
Expand All @@ -996,7 +1044,7 @@ describe('ProjectSection sessions waiting on repo sessions', () => {
unmount(section);
});

it('says what a written note’s session waits on under its title', async () => {
it('shows a written note’s session as running while it waits, with its badges and no waiting line', async () => {
listNotes(completedNote);
listChildren({ 'session-1': [childSession('child-1'), childSession('child-2')] });
const section = mountSection();
Expand All @@ -1009,10 +1057,165 @@ describe('ProjectSection sessions waiting on repo sessions', () => {
await emitWakeup('session-1', armedWakeup('child-2'));

const row = rowFor('session-1');
expect(row.dataset.type).toBe('note');
expect(row.dataset.type).toBe('generating-note');
expect(row.dataset.title).toBe('A finished note');
expect(row.dataset.secondaryMeta).toBe('Waiting on 1 repo session');
expect(row.dataset.secondaryMeta).toBe('');
expect(rowText(row)).not.toMatch(/waiting/i);
// Every repo session it has going is badged, named in the wake-up or not.
expect(row.dataset.childBadges).toBe('child-1:running child-2:running');
expect(resumeButton('session-1')).toBeNull();
// A running row opens on its chat, not on the note.
clickRow('session-1');
expect(noteModal.props).toMatchObject({ sessionId: 'session-1', chatOpen: true });

// The wake-up is stopped: it is the finished note again.
await emitWakeup('session-1', NO_WAKEUP);
expect(rowFor('session-1').dataset.type).toBe('note');

unmount(section);
});

it('dates a written note’s waiting row as the finished note’s is when it has no badges', async () => {
listNotes(completedNote);
const section = mountSection();
await settle();
// The finished note's subtitle: when it was written.
const dated = rowFor('session-1').dataset.secondaryMeta;
expect(dated).toBeTruthy();

// It waits on a repo session that is not among its children: the
// children have not been read, or the target is between states.
await emitWakeup('session-1', armedWakeup('child-1'));

const row = rowFor('session-1');
expect(row.dataset.type).toBe('generating-note');
expect(row.dataset.title).toBe('A finished note');
expect(row.dataset.childBadges).toBe('');
// No turn is running and the note is written: nothing is being generated.
expect(row.dataset.secondaryMeta).toBe(dated);
expect(rowText(row)).not.toMatch(/generating|waiting/i);

unmount(section);
});

it('keeps a written note’s menu actions, chat state and reply draft while its session waits', async () => {
// Unread, with a reply in its chat the user has not seen and one of the
// user's own left unsent.
listNotes(noteWith({ readAt: null, lastChatMessageAt: 5, chatReadAt: null }));
listChildren({ 'session-1': [childSession('child-1')] });
replyDraftPresence.preview('session-1', { quotes: [], text: 'An unsent reply' });
commandMocks.setProjectNoteRead.mockImplementation(async (noteId, read) =>
noteWith({ id: noteId, readAt: read ? 9 : null })
);
const section = mountSection();
await settle();
// The finished note: all of it shows, the unread dot included.
expect(rowFor('session-1').dataset).toMatchObject({
type: 'note',
unread: 'true',
chatState: 'unread',
hasDraft: 'true',
});

await emitWakeup('session-1', armedWakeup('child-1'));

// The row reads as running, and like any running row carries no unread
// dot. What belongs to the note is still there.
const row = rowFor('session-1');
expect(row.dataset).toMatchObject({
type: 'generating-note',
unread: 'false',
chatState: 'unread',
hasDraft: 'true',
});
expect(row.dataset.childBadges).toBe('child-1:running');
expect(resumeButton('session-1')).toBeNull();
// The note is still unread, so the menu marks it read,
expect(menuMarkButton('session-1', 'unread')).toBeNull();
menuMarkButton('session-1', 'read')!.click();
await settle();
expect(commandMocks.setProjectNoteRead.mock.calls).toEqual([['note-1', true]]);
// and it can be referred to from a new session.
expect(menuActionFor('session-1').dataset.hashtagRef).toBe(REF);
writer.saveNow.mockClear();
writer.scheduleSave.mockClear();
loadDraftMock.mockImplementation(async () => null);
clickReferring();
await settle();
expect(promptInput()).not.toBeNull();
expect(savedPrompts().some((prompt) => prompt.includes(REF))).toBe(true);

replyDraftPresence.preview('session-1', { quotes: [], text: '' });
unmount(section);
});

it('offers to mark a read note unread while its session waits', async () => {
listNotes(noteWith({ readAt: 5 }));
commandMocks.getSessionWakeup.mockImplementation(async () => armedWakeup('child-1'));
const section = mountSection();
await settle();

const row = rowFor('session-1');
expect(row.dataset).toMatchObject({
type: 'generating-note',
unread: 'false',
chatState: 'none',
hasDraft: 'false',
});
expect(menuMarkButton('session-1', 'read')).toBeNull();
expect(menuMarkButton('session-1', 'unread')).not.toBeNull();

unmount(section);
});

// What the waiting row's unread dot is matched to: a written note whose
// session is in a turn carries none. Unlike a waiting one it is not a
// finished note meanwhile, so its menu offers neither the reference nor
// the read marks, and its row shows no chat state or reply draft.
it('shows no unread dot or note actions on a written note whose session is in a turn', async () => {
listNotes(
noteWith({ sessionStatus: 'running', readAt: null, lastChatMessageAt: 5, chatReadAt: null })
);
replyDraftPresence.preview('session-1', { quotes: [], text: 'An unsent reply' });
const section = mountSection();
await settle();

expect(rowFor('session-1').dataset).toMatchObject({
type: 'generating-note',
unread: 'false',
chatState: 'none',
hasDraft: 'false',
});
expect(menuActionFor('session-1').dataset.hashtagRef).toBe('');
expect(menuMarkButton('session-1', 'read')).toBeNull();
expect(menuMarkButton('session-1', 'unread')).toBeNull();

replyDraftPresence.preview('session-1', { quotes: [], text: '' });
unmount(section);
});

it('lists a waiting session among the running ones, after the finished notes', async () => {
// Written before the finished note, so by date alone it would come first.
listNotes(
{ ...unwritten, createdAt: 1, updatedAt: 1, completedAt: 1 },
{ ...completedNote, createdAt: 2, updatedAt: 2, completedAt: 2 }
);
commandMocks.getSessionWakeup.mockImplementation(async (sessionId) =>
sessionId === 'session-waiting' ? armedWakeup('child-1') : NO_WAKEUP
);
listChildren({ 'session-waiting': [childSession('child-1')] });
const section = mountSection();
await settle();

const order = () =>
[...document.querySelectorAll<HTMLElement>('[data-testid="timeline-row"]')].map(
(row) => `${row.dataset.sessionId}:${row.dataset.type}`
);
expect(order()).toEqual(['session-1:note', 'session-waiting:generating-note']);

// No longer waiting: it is a finished row again, in date order.
await emitWakeup('session-waiting', NO_WAKEUP);
expect(order()).toEqual(['session-waiting:failed-note', 'session-1:note']);

unmount(section);
});
Expand All @@ -1034,7 +1237,7 @@ describe('ProjectSection sessions waiting on repo sessions', () => {
layerHolder.layer = reactiveProps({ active: true, visible: true, activations: 0 });
const section = mountSection();
await settle();
expect(rowFor('session-waiting').dataset.type).toBe('waiting-note');
expect(rowFor('session-waiting').dataset.type).toBe('generating-note');
expect(rowFor('session-1').dataset.type).toBe('note');
commandMocks.getSessionWakeup.mockClear();

Expand All @@ -1053,7 +1256,8 @@ describe('ProjectSection sessions waiting on repo sessions', () => {
'session-waiting',
]);
expect(rowFor('session-waiting').dataset.type).toBe('failed-note');
expect(rowFor('session-1').dataset.secondaryMeta).toBe('Waiting on 1 repo session');
expect(rowFor('session-1').dataset.type).toBe('generating-note');
expect(rowFor('session-1').dataset.childBadges).toBe('child-2:running');

unmount(section);
});
Expand Down
Loading