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
49 changes: 49 additions & 0 deletions src/webview/page-antipatterns-editor-wiring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,52 @@ describe('Anti-Patterns page rendered again by a save', () => {
expect(calls('saveRule')).toHaveLength(2);
});
});

describe('What the page shows after a save', () => {
it.each([1, 3])('one section per practice group after %i saves', async saves => {
const page = await renderPage();
showDetail(page, A);
for (let save = 0; save < saves; save++) await saveShownRule(page, A);
expect([...page.querySelectorAll('#apDetails .ap-group-name')].map(name => name.textContent))
.toEqual(['Prompt Quality', 'Session Hygiene', 'Code Review', 'Tool Mastery']);
});

it('the saved rule, in the detail that was open', async () => {
const page = await renderPage();
showDetail(page, A);
await saveShownRule(page, A, [{ ...listed(A), description: 'The saved description.' }, listed(B)]);
expect(el(page, '#rule-detail-panel').style.display).toBe('');
expect(el(page, '.rule-detail-desc').textContent).toBe('The saved description.');
});

it('the rule list, when the rule whose detail was open is gone', async () => {
const page = await renderPage();
showDetail(page, A);
await saveShownRule(page, A, [listed(B)]);
expect(el(page, '#rule-detail-panel').style.display).toBe('none');
expect(el(page, '#rule-list').style.display).toBe('');
});

it('the rule list, when the detail was closed with Back before a save', async () => {
const page = await renderPage();
showDetail(page, A);
click(page, '#rule-back-btn');
click(page, '#rule-new-btn');
click(page, '.rule-editor-tab[data-editor-tab="source"]');
el<HTMLTextAreaElement>(page, '#rule-editor-raw').value = SOURCES[C];
click(page, '#rule-editor-save');
await answer('saveRule', { ok: true });
expect(el(page, '#rule-detail-panel').style.display).toBe('none');
expect(el(page, '#rule-list').style.display).toBe('');
});
});

async function saveShownRule(page: HTMLElement, id: string, rules?: Record<string, unknown>[]): Promise<void> {
await editShownRule(page, id);
const listing = mocks.rpc.getMockImplementation()!;
if (rules) mocks.rpc.mockImplementation((method: string, params?: Record<string, unknown>) => method === 'getRuleEditor'
? Promise.resolve({ ...PAGE_DATA.getRuleEditor as object, rules })
: listing(method, params) as Promise<unknown>);
click(page, '#rule-editor-save');
await answer('saveRule', { ok: true });
}
29 changes: 21 additions & 8 deletions src/webview/page-antipatterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,9 @@ export async function renderAntiPatterns(container: HTMLElement, currentFilter:

// Wire up rules section
wireRulesSection(container, rules, previewMap, currentFilter);

const openRuleId = container.querySelector<HTMLElement>('#rule-detail-panel')!.dataset.ruleId;
if (openRuleId) showRuleDetail(container, openRuleId, rules, previewMap, currentFilter);
}

/* ── Tab switching ── */
Expand Down Expand Up @@ -610,6 +613,7 @@ function renderFindings(
): void {
const detailsContainer = container.querySelector('#apDetails')!;
const allGroupKeys: PracticeGroup[] = ['prompt-quality', 'session-hygiene', 'code-review', 'tool-mastery'];
const sections: HTMLElement[] = [];

for (const groupKey of allGroupKeys) {
const gs = scores.find(s => s.group === groupKey);
Expand Down Expand Up @@ -674,8 +678,9 @@ function renderFindings(
section.appendChild(card);
}
}
detailsContainer.appendChild(section);
sections.push(section);
}
detailsContainer.replaceChildren(...sections);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional, as you mentioned in the description: a group someone had expanded comes back collapsed after each save. If you'd like to keep it open, one idea is to remember the open groups before rebuilding:

const sections: HTMLElement[] = [];
const openGroups = new Set([...detailsContainer.querySelectorAll<HTMLDetailsElement>('details.ap-group-details[open]')]
  .map(d => d.dataset.group));
// ...
const section = el('details', 'ap-group-details');
section.dataset.group = groupKey;
(section as HTMLDetailsElement).open = openGroups.has(groupKey);

We tried this locally with your tests plus one for an expanded group, and typecheck is clean. Happy for this to be a separate PR or skipped.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for trying this out locally, the openGroups idea is really neat! I'd like to keep this PR focused on #352, so I'll open a small follow up PR with your change and a test for an expanded group as soon as this one lands.


const hint = consumeNavHint();
if (hint) {
Expand Down Expand Up @@ -1037,12 +1042,7 @@ function wireRulesSection(
}, { signal });

// Back button
container.querySelector('#rule-back-btn')?.addEventListener('click', () => {
container.querySelector<HTMLElement>('#rule-list')!.style.display = '';
container.querySelector<HTMLElement>('#rule-detail-panel')!.style.display = 'none';
container.querySelector<HTMLElement>('#rule-search-bar')!.style.display = '';
container.querySelector<HTMLElement>('#ap-rules-header')!.style.display = '';
}, { signal });
container.querySelector('#rule-back-btn')?.addEventListener('click', () => hideRuleDetail(container), { signal });

// Unified Rule Editor
wireRuleEditorModal(container, currentFilter, () => renderAntiPatterns(container, currentFilter), signal);
Expand Down Expand Up @@ -1213,7 +1213,10 @@ function showRuleDetail(
currentFilter: DateFilter,
): void {
const rule = rules.find(r => r.id === ruleId);
if (!rule) return;
if (!rule) {
hideRuleDetail(container);
return;
}

const preview = previewMap.get(ruleId);
container.querySelector<HTMLElement>('#rule-list')!.style.display = 'none';
Expand All @@ -1222,6 +1225,7 @@ function showRuleDetail(

const panel = container.querySelector<HTMLElement>('#rule-detail-panel')!;
panel.style.display = '';
panel.dataset.ruleId = ruleId;

const content = container.querySelector<HTMLElement>('#rule-detail-content')!;
render(renderRuleDetailContent(rule, preview, ruleId), content);
Expand All @@ -1237,6 +1241,15 @@ function showRuleDetail(
wireRuleDetailExplainButtons(content, currentFilter);
}

function hideRuleDetail(container: HTMLElement): void {
const panel = container.querySelector<HTMLElement>('#rule-detail-panel')!;
panel.style.display = 'none';
delete panel.dataset.ruleId;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is important: without it, opening a rule, pressing Back, then saving a new rule would bring the old detail back. Right now all the tests still pass if it's removed, though. Could we add a case next to your new ones? This passes with your change and fails without this line:

it('the rule list, when the detail was closed with Back before a save', async () => {
  const page = await renderPage();
  showDetail(page, A);
  click(page, '#rule-back-btn');
  click(page, '#rule-new-btn');
  click(page, '.rule-editor-tab[data-editor-tab="source"]');
  el<HTMLTextAreaElement>(page, '#rule-editor-raw').value = SOURCES[C];
  click(page, '#rule-editor-save');
  await answer('saveRule', { ok: true });
  expect(el(page, '#rule-detail-panel').style.display).toBe('none');
  expect(el(page, '#rule-list').style.display).toBe('');
});

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Sanjay Singh (@san360), great catch! Added your case as is, right after the other new ones in 744474c. I checked it the same way: it passes with the change and fails once delete panel.dataset.ruleId is removed.

container.querySelector<HTMLElement>('#rule-list')!.style.display = '';
container.querySelector<HTMLElement>('#rule-search-bar')!.style.display = '';
container.querySelector<HTMLElement>('#ap-rules-header')!.style.display = '';
}

function renderLayerStatus(layers: RuleLayerInfo[], layerName: string): ComponentChildren {
const info = layers.find(l => l.layer === layerName);
if (!info) return html`<span class="rule-help-status rule-help-na">Not applicable</span>`;
Expand Down