Repository navigation
Runtime policies: an Unchanged rows control that folds what didn't change #218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
agorischek
wants to merge
4
commits into
main
Choose a base branch
from
agorischek-runtime-policy-fold
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+348
−28
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f5e672b
Runtime policies: an Unchanged rows control that folds what didn't ch…
agorischek c249cc7
Reference: the runtime-policy renderer's Unchanged rows control
agorischek 3164314
Runtime policies: a fold is a plain row of its table
agorischek 25791f6
Runtime policies: a pane folds by the comparison's changes, not its o…
agorischek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import {expect, test} from '@playwright/test' | ||
| import {openStage} from '@githubnext/projections-renderer-testing/playwright' | ||
| import {harnessInspector} from '@githubnext/projections-renderer-harness/playwright' | ||
|
|
||
| /** | ||
| * The Unchanged rows control: Show (the default) lists every row; Fold, as a definition presets it, folds each run of | ||
| * unchanged rows into one row and each unchanged hook's lane into one line, which opens in place and closes again, | ||
| * while every change stays drawn. | ||
| */ | ||
| test('the Unchanged rows control folds unchanged rows and hooks, and a fold opens in place', async ({page}) => { | ||
| const inspector = harnessInspector() | ||
| const shown = await openStage(page, inspector, {scenario: 'runtime-policy/deployment-review', comparisonMode: 'diff', width: 1000}) | ||
| await shown.measure('settle') | ||
| const listed = await shown.frame.locator('[data-change]').count() | ||
| await expect(shown.frame.locator('.rp-fold')).toHaveCount(0) | ||
| const tall = await shown.frame.locator('.rp-view').evaluate(view => view.getBoundingClientRect().height) | ||
|
|
||
| const folded = await openStage(page, inspector, { | ||
| scenario: 'runtime-policy/deployment-review-folded', | ||
| comparisonMode: 'diff', | ||
| width: 1000, | ||
| }) | ||
| await folded.measure('settle') | ||
| const frame = folded.frame | ||
| await expect(frame.locator('.rp-fold')).toHaveText(['2 unchanged kernels', '1 unchanged hook', '3 unchanged hooks']) | ||
| // Every change is still drawn; only unchanged rows fold. | ||
| expect(await frame.locator('[data-change]').count()).toBe(listed) | ||
| await expect(frame.locator('.rp-fold [data-change]')).toHaveCount(0) | ||
| expect(await frame.locator('.rp-view').evaluate(view => view.getBoundingClientRect().height)).toBeLessThan(tall - 400) | ||
| // A fold is a row of its table, as wide as the table, its label on one line. | ||
| for (const fold of await frame.locator('.rp-fold').all()) { | ||
| const [row, table] = await fold.evaluate(element => [ | ||
| element.getBoundingClientRect().width, | ||
| (element.parentElement ?? element).getBoundingClientRect().width, | ||
| ]) | ||
| expect(row).toBeCloseTo(table, 0) | ||
| expect(await fold.locator('.pk-fold-label').evaluate(label => label.getClientRects().length)).toBe(1) | ||
| } | ||
|
|
||
| const hooks = frame.getByRole('button', {name: '3 unchanged hooks'}) | ||
| const lanes = await frame.locator('.rp-lane').count() | ||
| await hooks.click() | ||
| await expect(frame.getByRole('button', {name: 'Hide 3 unchanged hooks'})).toHaveAttribute('aria-expanded', 'true') | ||
| await expect(frame.locator('.rp-lane')).toHaveCount(lanes + 3) | ||
| await frame.getByRole('button', {name: 'Hide 3 unchanged hooks'}).click() | ||
| await expect(frame.locator('.rp-lane')).toHaveCount(lanes) | ||
| }) | ||
|
|
||
| test('the Changes scope never folds: it already leaves unchanged rows out', async ({page}) => { | ||
| const stage = await openStage(page, harnessInspector(), { | ||
| scenario: 'runtime-policy/deployment-review-folded', | ||
| comparisonMode: 'diff', | ||
| width: 1000, | ||
| }) | ||
| await stage.measure('settle') | ||
| await expect(stage.frame.locator('.rp-fold')).not.toHaveCount(0) | ||
| const subject = stage.subject | ||
| if (subject.mode !== 'comparison') throw new Error('the scenario is a comparison') | ||
| const scope = subject.comparison.changes.flatMap(change => (change.status === 'unchanged' ? [] : [...change.before, ...change.after])) | ||
| await stage.filter({query: '', changesOnly: true, ids: null, scope}) | ||
| await expect(stage.frame.locator('.rp-fold')).toHaveCount(0) | ||
| }) | ||
|
|
||
| test('a side-by-side pane folds what the comparison leaves unchanged, not what its own revision draws alike', async ({page}) => { | ||
| const inspector = harnessInspector() | ||
| for (const pane of ['before', 'after'] as const) { | ||
| const stage = await openStage(page, inspector, { | ||
| scenario: 'runtime-policy/deployment-review-folded', | ||
| comparisonMode: 'side-by-side', | ||
| pane, | ||
| width: 1000, | ||
| }) | ||
| await stage.measure('settle') | ||
| // Every workload's coverage changes, which a pane draws with no chip arriving or leaving: none of them folds. | ||
| await expect(stage.frame.locator('.rp-fold', {hasText: 'workload'}), pane).toHaveCount(0) | ||
| for (const workload of ['checkout-web', 'payments-api', 'settlement']) { | ||
| await expect(stage.frame.locator('.rp-name', {hasText: workload}), `${pane}: ${workload}`).toBeVisible() | ||
| } | ||
| await expect(stage.frame.locator('.rp-fold', {hasText: 'unchanged kernels'}), pane).toHaveCount(1) | ||
| } | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import {isChanged} from '@githubnext/projections-renderer-kit' | ||
| import type {RendererControl} from '@githubnext/projections-renderer-sdk/protocol' | ||
| import type {Chip, HookItem, Item, PolicyItem, RuntimePolicyModel, SelectorItem, WorkloadItem} from './model.js' | ||
|
|
||
| /** | ||
| * The host draws this in a comparison: whether the All scope lists unchanged rows or folds them. A definition presets | ||
| * it (`presentation.rendererOptions: {"unchanged": "fold"}`) where a change is small beside its unchanged surroundings, | ||
| * as on a stage too short to list both. The Changes scope already leaves unchanged rows out. | ||
| */ | ||
| export const unchangedControl: RendererControl = { | ||
| id: 'unchanged', | ||
| label: 'Unchanged rows', | ||
| description: 'List unchanged agents, policies, workloads and hooks, or fold each run of them into one row', | ||
| type: 'choice', | ||
| group: 'rows', | ||
| groupLabel: 'Rows', | ||
| default: 'show', | ||
| choices: [ | ||
| {value: 'show', label: 'Show'}, | ||
| {value: 'fold', label: 'Fold'}, | ||
| ], | ||
| } | ||
|
|
||
| const chipsChanged = (chips: readonly Chip[]) => chips.some(chip => chip.change !== 'unchanged') | ||
|
|
||
| /** Whether a row draws a change: its own, or a chip arriving or leaving inside it. */ | ||
| export function rowChanged(item: Item | PolicyItem | WorkloadItem | SelectorItem): boolean { | ||
| if (isChanged(item.row)) return true | ||
| if ('scope' in item && chipsChanged(item.scope)) return true | ||
| if ('coveredBy' in item && chipsChanged(item.coveredBy)) return true | ||
| return 'chips' in item && chipsChanged(item.chips) | ||
| } | ||
|
|
||
| /** Whether a hook's lane draws a change: the hook's own, or any of its probes' or rules'. */ | ||
| export const laneChanged = (hook: HookItem) => | ||
| rowChanged(hook) || hook.probes.some(probe => rowChanged(probe) || probe.selectors.some(rowChanged)) | ||
|
|
||
| /** | ||
| * The IDs of every row and hook that changes, read from the diff's model (`buildModel` with side `diff`): a | ||
| * side-by-side pane draws one revision, with no chip arriving or leaving, so folding by its own model would fold | ||
| * a workload whose coverage changed in both panes. | ||
| */ | ||
| export function changedRows(diff: RuntimePolicyModel): ReadonlySet<string> { | ||
| const rows = [...diff.deployment, ...diff.policies, ...diff.workloads].filter(rowChanged) | ||
| const hooks = diff.categories.flatMap(category => category.hooks).filter(laneChanged) | ||
|
|
||
| return new Set([...rows, ...hooks].map(item => item.row.id)) | ||
| } | ||
|
|
||
| export type Entry<T> = {type: 'item'; item: T} | {type: 'fold'; key: string; count: number; open: boolean} | ||
|
|
||
| /** | ||
| * A list with each run of unchanged items folded into one entry, keyed by its first item, when `fold` is on; a run | ||
| * `expanded` names is listed after its fold, open. A run shorter than `minimum` stays listed: a row's fold takes as | ||
| * much room as the row, but a hook lane's takes much less. | ||
| */ | ||
| export function foldUnchanged<T extends Item>( | ||
| items: readonly T[], | ||
| changed: (item: T) => boolean, | ||
| fold: boolean, | ||
| expanded: ReadonlySet<string>, | ||
| minimum: number, | ||
| ): Entry<T>[] { | ||
| if (!fold) return items.map(item => ({type: 'item', item})) | ||
| const entries: Entry<T>[] = [] | ||
| let run: T[] = [] | ||
| const flush = () => { | ||
| const [first] = run | ||
| if (first && run.length >= minimum) { | ||
| const key = first.row.id | ||
| const open = expanded.has(key) | ||
| entries.push({type: 'fold', key, count: run.length, open}) | ||
| if (open) for (const item of run) entries.push({type: 'item', item}) | ||
| } else for (const item of run) entries.push({type: 'item', item}) | ||
| run = [] | ||
| } | ||
| for (const item of items) { | ||
| if (changed(item)) { | ||
| flush() | ||
| entries.push({type: 'item', item}) | ||
| } else run.push(item) | ||
| } | ||
| flush() | ||
| return entries | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.