matthewwong525/WongStack / .agents/plugins/access
access
VerifiedSee who can sign in and what each person may use; add, change or remove a person.
screen/roles.test.mjs
View on GitHub// @vitest-environment jsdom
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
import { cleanup, fireEvent, moment, render, screen, waitFor, within } from './test-dom.mjs'
// A shop with three apps: Hello changes things with Stripe, Payroll with Bank, and the tip calculator uses no key.
const key = (id, title, changes = {}) => ({ id, title, levels: ['read', 'write'], saved: true, setup: false, alone: false, ...changes })
const built = (id, title) => ({ id, title, description: `${title} for the shop` })
const person = (email, changes = {}) => ({ email, status: 'active', settled: true, role: null, apps: [], keys: {}, ...changes })
const sales = () => ({ apps: ['hello', 'tips'], keys: { stripe: 'read' } })
// Support has Hello, which changes things with Stripe, and holds Stripe at Read: the two are not tied to each other.
const support = () => ({ apps: ['hello'], keys: { stripe: 'read' } })
const status = () => ({ ownerEmail: 'owner@shop.com', viewer: { email: 'owner@shop.com', owner: true }, environment: 'live', key: 'ready', started: true, imported: 0,
keysStarted: true, unticked: { people: [], roles: [] },
apps: [built('hello', 'Hello'), built('payroll', 'Payroll'), built('tips', 'Tip calculator')],
keys: [key('stripe', 'Stripe'), key('bank', 'Bank'),
key('cloudflare', 'Cloudflare', { levels: ['read'], setup: true, alone: true }), key('code', 'Project code', { levels: ['read', 'write'], alone: true })],
roles: [{ id: 'office', name: 'Office', apps: [], keys: {} }, { id: 'sales', name: 'Sales', ...sales() }, { id: 'support', name: 'Support', ...support() }],
people: [person('gone@shop.com', { status: 'removed' }), person('kim@shop.com', { apps: ['hello'], keys: { stripe: 'read', cloudflare: 'read' } }),
person('lee@shop.com', { role: 'sales', ...sales() }), person('sam@shop.com', { role: 'sales', ...sales() }), person('pat@shop.com', { role: 'support', ...support() })],
work: [], project: 'ready' })
let roster
let failed
let fetchMock
// The apps a person can open.
const screens = () => roster.apps.map(item => item.id)
beforeEach(() => {
roster = status(); failed = new Set()
fetchMock = vi.fn(async (url) => {
const path = url.replace('/api/access/', '')
if (failed.has(path)) return Response.json({ code: 'unavailable' }, { status: 503 })
if (path === 'apps') return Response.json({ state: 'current', role: 'owner', apps: ['access', ...screens()], revision: 1 })
if (path === 'setup') return Response.json({ role: 'owner', api: 'authenticated', identity: { email: 'owner@shop.com', subject: 'owner' }, apps: screens(),
repository: 'manual_provider_setup', memory: 'independent_operator_setup', prompt: { state: 'unavailable', message: 'Not on this preview' } })
return Response.json(roster)
})
vi.stubGlobal('fetch', fetchMock)
})
afterEach(() => { cleanup(); vi.unstubAllGlobals() })
const open = (path = '') => render(`/apps/access/${path}`)
const where = () => decodeURIComponent(window.location.pathname)
const click = (name) => fireEvent.click(screen.getByRole('button', { name }))
const posts = (path) => fetchMock.mock.calls.filter(([url, init]) => url.endsWith(path) && init?.method === 'POST')
const sent = (path) => posts(path).map(([, init]) => JSON.parse(init.body))
const reads = (path) => fetchMock.mock.calls.filter(([url, init]) => url.endsWith(path) && init?.method !== 'POST')
const checked = (inputs) => inputs.filter(input => (input).checked).map(input => input.parentElement .textContent)
// One role's row in the list and what its cells say.
const row = async (name) => within((await screen.findByRole('link', { name })).closest('tr'))
const cells = async (name) => Array.from((await screen.findByRole('link', { name })).closest('tr') .querySelectorAll('td')).map(cell => cell.textContent)
// The panel a role is opened in, by its name, and that none is open any more.
const panel = (name) => screen.findByRole('dialog', { name })
const closed = () => waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
const named = () => screen.getByLabelText('Name')
// A named group (Apps, Keys or Project), or one key's level choice by the start of its legend, with its quiet lines.
const part = (name) => screen.getByRole('group', { name: new RegExp(`^${name}( · |$)`) })
const level = (name) => within(part(name))
const pick = (name, to) => fireEvent.click(level(name).getByRole('radio', { name: to }))
const note = (name) => Array.from(part(name).querySelectorAll('p')).map(line => line.textContent)
// Apps: a press on one app's tick, and the apps that are ticked.
const give = (name) => fireEvent.click(level('Apps').getByRole('checkbox', { name }))
const given = () => checked(level('Apps').getAllByRole('checkbox'))
// Project code is one tick in a group of its own after the keys, with the step left under it.
const INSTALL = 'Can install the project'
const install = () => screen.getByRole('checkbox', { name: INSTALL })
const project = () => level('Project')
// Keys: every line with the level it shows, and the lines above None.
const lines = () => level('Keys').getAllByRole('group')
const title = (line) => line.querySelector('legend') .firstChild .textContent
// The same line's whole legend: the key, then whether the app holds it.
const state = (line) => line.querySelector('legend') .textContent
const levels = () => Object.fromEntries(lines().map(line => [title(line), checked(within(line).getAllByRole('radio'))[0]]))
const held = () => Object.fromEntries(Object.entries(levels()).filter(([, at]) => at !== 'None'))
const NO_APPS = { hello: false, payroll: false, tips: false }
const NO_KEYS = { stripe: null, bank: null, cloudflare: null, code: null }
// Admin, the role made for you, as the status sends it: every app, and each key at its highest level.
const ADMIN = { id: 'admin', name: 'Admin', builtIn: true, apps: ['hello', 'payroll', 'tips'], keys: { stripe: 'write', bank: 'write', cloudflare: 'read', code: 'write' } }
it('lists Admin first as Everything, and opens it to what it gives and who holds it, with nothing to name, save or remove', async () => {
roster.roles = [ADMIN]; roster.people = [person('kim@shop.com')]
await open('roles'); const add = await screen.findByRole('link', { name: 'Add role' })
expect(add.getAttribute('href')).toBe('/apps/access/roles/new')
// The list is never empty: with no role of the owner's own it still holds Admin,
// and the add button ends the views' line.
expect(await cells('Admin')).toEqual(['Admin', 'Everything', 'Nobody yet'])
expect(screen.getByRole('navigation', { name: 'Access views' }).parentElement .lastElementChild).toBe(add)
cleanup(); roster = status(); roster.roles = [ADMIN, ...roster.roles]; roster.people[1] = person('kim@shop.com', { role: 'admin', apps: ADMIN.apps, keys: ADMIN.keys })
await open('roles'); const table = await screen.findByRole('table', { name: 'Roles' })
expect(Array.from(table.querySelectorAll('tbody td:first-child a')).map(link => link.textContent)).toEqual(['Admin', 'Office', 'Sales', 'Support'])
expect(await cells('Admin')).toEqual(['Admin', 'Everything', '1 person'])
await fireEvent.click((await row('Admin')).getByText('Everything'))
const opened = await panel('Admin'); expect(where()).toBe('/apps/access/roles/admin')
expect(table.querySelector('tr[aria-current="true"] a') .textContent).toBe('Admin')
expect(Array.from(opened.querySelectorAll('p')).map(line => line.textContent)).toEqual(['Every app and every key, including new ones.', 'Can manage Access.', 'Only the owner gives it.', 'People: kim@shop.com'])
// Nothing here changes: no field, tick or level, and each button only closes the panel.
expect(opened.querySelectorAll('input, select, textarea')).toHaveLength(0)
expect(within(opened).getAllByRole('button').map(button => button.textContent)).toEqual(['Close', 'Close'])
// Enter saves nothing, and closing asks nothing.
fireEvent.submit(opened.querySelector('form')); await fireEvent.click(within(opened).getAllByRole('button', { name: 'Close' })[0])
await closed(); expect([where(), screen.queryByRole('alertdialog'), posts('roles')]).toEqual(['/apps/access/roles', null, []])
// Another role still opens to its fields.
await fireEvent.click((await row('Sales')).getByRole('link', { name: 'Sales' })); await panel('Sales')
expect([named().value, screen.getAllByRole('button', { name: /Save role|Remove role|Cancel/ }).map(button => button.textContent)]).toEqual(['Sales', ['Save role', 'Cancel', 'Remove role']])
})
it('lists each role on one line with how many apps, keys and people it has, with nothing marked as missing, and changes one in the panel beside the list', async () => {
await open(); await fireEvent.click(await screen.findByRole('link', { name: /^Roles/ }))
const table = await screen.findByRole('table', { name: 'Roles' })
expect(within(table).getAllByRole('columnheader').map(cell => cell.textContent)).toEqual(['Role', 'Apps and keys', 'People'])
// Counts, not labels: the names show where the role is opened. Sales has Hello with Stripe at Read, unmarked.
expect([await cells('Office'), await cells('Sales'), await cells('Support')]).toEqual([['Office', 'No apps', 'Nobody yet'], ['Sales', '2 apps, 1 key', '2 people'], ['Support', '1 app, 1 key', '1 person']])
expect(table.textContent).not.toMatch(/!|gap/); expect(document.querySelector('[data-slot="badge"]')).toBeNull()
const sales = await row('Sales')
expect(screen.queryByRole('link', { name: 'Edit' })).toBeNull()
expect(sales.getByRole('link', { name: 'Sales' }).getAttribute('href')).toBe('/apps/access/roles/sales')
// A click anywhere on the row opens the role beside the list, with its row marked.
await fireEvent.click(sales.getByText('2 people'))
const opened = await panel('Sales'); expect(where()).toBe('/apps/access/roles/sales')
expect(screen.getByRole('table', { name: 'Roles' }).querySelector('tr[aria-current="true"] a') .textContent).toBe('Sales'); expect(screen.getByRole('navigation', { name: 'Access views' })).toBeTruthy()
expect(named().value).toBe('Sales'); expect(screen.queryByLabelText('Copy from a person')).toBeNull()
// The same two groups as for a person: a tick per app, then a level per key. The project's tick too; none to manage.
expect([given(), levels()]).toEqual([['Hello', 'Tip calculator'], { Stripe: 'Read', Bank: 'None', Cloudflare: 'None' }])
expect(screen.getAllByRole('checkbox').map(box => box.parentElement .textContent)).toEqual(['Hello', 'Payroll', 'Tip calculator', INSTALL, 'Can publish']); expect(install().checked).toBe(false)
expect([note('Stripe'), note('Bank'), note('Cloudflare')]).toEqual([[], [], []])
// Each key says whether the app holds it, as where a person is opened, and no Key settings is offered.
expect(lines().map(state)).toEqual(['Stripe · Saved', 'Bank · Saved', 'Cloudflare · Saved']); expect(screen.queryByRole('link', { name: 'Key settings' })).toBeNull()
// No Start from, Can reach or Can't yet, and nothing said to be missing.
for (const name of ['Start from', 'Can reach', "Can't yet"]) expect(screen.queryByRole('group', { name }), name).toBeNull()
expect(opened.textContent).not.toMatch(/Start from|Can reach|Can't yet|can't use|not change|what it needs|Look up|gap|!/)
expect(screen.getByText('People: lee@shop.com, sam@shop.com')).toBeTruthy(); expect(screen.queryByText('Not saved yet')).toBeNull()
await fireEvent.change(named(), { target: { value: 'Field sales' } }); expect(screen.getByText('Not saved yet')).toBeTruthy()
// A tick gives the app and moves no key level: Payroll changes things with Bank, and Bank stays at None until picked.
await give('Payroll'); expect([given(), held()]).toEqual([['Hello', 'Payroll', 'Tip calculator'], { Stripe: 'Read' }])
await pick('Bank', 'Read & write'); expect(given()).toEqual(['Hello', 'Payroll', 'Tip calculator'])
await click('Save role'); await screen.findByText('Saved.')
expect(sent('roles')).toEqual([{ id: 'sales', name: 'Field sales', apps: { hello: true, payroll: true, tips: true }, keys: { ...NO_KEYS, stripe: 'read', bank: 'write' } }])
expect(where()).toBe('/apps/access/roles'); expect(screen.queryByRole('dialog')).toBeNull()
})
it("a tick on a role saves the app and no key level, and the role's people then have what the role has", async () => {
await open('roles'); await fireEvent.click((await row('Support')).getByText('1 person')); await panel('Support')
expect([given(), held()]).toEqual([['Hello'], { Stripe: 'Read' }])
await give('Payroll'); expect([given(), held()]).toEqual([['Hello', 'Payroll'], { Stripe: 'Read' }])
expect(screen.getByText('Not saved yet')).toBeTruthy(); expect(posts('roles')).toHaveLength(0)
// Unticked again, the role is as it was, with nothing to save.
await give('Payroll'); expect(screen.queryByText('Not saved yet')).toBeNull(); await give('Payroll')
// The server answers with the role's new set, which is its people's too.
const now = { apps: ['hello', 'payroll'], keys: { stripe: 'read' } }
roster.roles[2] = { ...roster.roles[2], ...now }; roster.people[4] = { ...roster.people[4], ...now }
await click('Save role'); await screen.findByText('Saved.')
expect(sent('roles')).toEqual([{ id: 'support', name: 'Support', apps: { ...NO_APPS, hello: true, payroll: true }, keys: { ...NO_KEYS, stripe: 'read' } }])
expect(await cells('Support')).toEqual(['Support', '2 apps, 1 key', '1 person'])
// Its person, opened, names what the role gives: each app by its title, and the key with its level.
await fireEvent.click(screen.getByRole('link', { name: /^People/ })); await fireEvent.click(await screen.findByRole('link', { name: 'pat@shop.com' }))
expect(within(await panel('pat@shop.com')).getAllByRole('listitem').map(item => item.textContent)).toEqual(['Hello', 'Payroll', 'Stripe Read'])
})
it("gives a whole role the project with the same tick as a person's, and names the step left under it", async () => {
roster.keys[3].saved = false; roster.project = 'key'
await open('roles/office'); await panel('Office')
expect(install().checked).toBe(false); expect(project().queryByText(/One step first/)).toBeNull()
// Project code is the tick, never a level choice among the keys.
expect(lines().map(title)).toEqual(['Stripe', 'Bank', 'Cloudflare']); expect(project().queryByRole('radio')).toBeNull()
await fireEvent.click(install())
expect(project().getByText('One step first.')).toBeTruthy(); expect(project().getByRole('button', { name: 'Copy that request' })).toBeTruthy()
// It saves before the key arrives, and ticks no app.
await click('Save role'); await screen.findByText('Saved.')
expect(sent('roles')).toEqual([{ id: 'office', name: 'Office', apps: NO_APPS, keys: { ...NO_KEYS, code: 'read' } }])
// Once the app can hand the project out, the tick shows with nothing under it, and unticking saves none.
cleanup(); roster.keys[3].saved = true; roster.project = 'ready'; roster.roles[0].keys = { code: 'read' }
await open('roles/office'); await panel('Office')
expect(install().checked).toBe(true); expect(project().queryByText(/One step first/)).toBeNull(); expect(project().queryByRole('button')).toBeNull()
await fireEvent.click(install()); await click('Save role'); await screen.findByText('Saved.')
expect(sent('roles')[1]).toEqual({ id: 'office', name: 'Office', apps: NO_APPS, keys: NO_KEYS })
// The role's tick says it also saves, and names the one step left while the app hands the project out
// and can not save yet.
cleanup(); roster.projectSave = 'key'
await open('roles/office'); await panel('Office')
expect(project().getByText('They can save changes too.')).toBeTruthy()
expect(project().getByText('Saving needs one more step.')).toBeTruthy(); expect(project().getByRole('button', { name: 'Copy that request' })).toBeTruthy()
})
it('adds a role, which can copy what one person has now', async () => {
await open('roles'); await fireEvent.click(await screen.findByRole('link', { name: 'Add role' }))
await panel('Add role'); expect(where()).toBe('/apps/access/roles/new')
expect(document.activeElement).toBe(named())
expect(screen.getByText('People: nobody yet')).toBeTruthy(); expect(screen.queryByRole('button', { name: 'Remove role' })).toBeNull()
// The list stays beside the panel, and no row is marked: the new role has none yet.
expect(screen.getByRole('table', { name: 'Roles' }).querySelector('tr[aria-current]')).toBeNull()
// Only people who can sign in are offered, and Nobody is picked.
const from = screen.getByLabelText('Copy from a person')
expect(within(from).getAllByRole('option').map(option => option.textContent)).toEqual(['Nobody', 'kim@shop.com', 'lee@shop.com', 'sam@shop.com', 'pat@shop.com'])
expect([from.value, given(), held()]).toEqual(['', [], {}])
await give('Payroll'); expect(given()).toEqual(['Payroll'])
// A person who holds a role gives the new role what that role gives them, in place of what was ticked.
await fireEvent.change(from, { target: { value: 'lee@shop.com' } })
expect([from.value, given(), held()]).toEqual(['lee@shop.com', ['Hello', 'Tip calculator'], { Stripe: 'Read' }])
await fireEvent.change(from, { target: { value: '' } })
expect([from.value, given(), held()]).toEqual(['', [], {}])
await fireEvent.change(from, { target: { value: 'kim@shop.com' } })
expect([from.value, given(), held()]).toEqual(['kim@shop.com', ['Hello'], { Stripe: 'Read', Cloudflare: 'Read' }])
await fireEvent.change(named(), { target: { value: 'Helpers' } }); await click('Save role'); await screen.findByText('Saved.')
expect(sent('roles')).toEqual([{ name: 'Helpers', apps: { ...NO_APPS, hello: true }, keys: { ...NO_KEYS, stripe: 'read', cloudflare: 'read' } }])
})
it('asks in a popup before removing a role, says its people keep their access, and keeps the panel as it was on Cancel', async () => {
await open('roles/sales'); await panel('Sales')
await fireEvent.change(named(), { target: { value: 'Field sales' } }); await click('Remove role')
const ask = within(screen.getByRole('alertdialog', { name: 'Remove Sales?' }))
expect(ask.getByText('Its people keep the access they have now, as their own set.')).toBeTruthy()
// The way out is focused first; the action is the question's one solid button.
const [out, yes] = [ask.getByRole('button', { name: 'Cancel' }), ask.getByRole('button', { name: 'Remove role' })]
await moment(); expect([document.activeElement, out.getAttribute('data-variant'), yes.getAttribute('data-variant')]).toEqual([out, 'outline', 'default'])
// The panel stays under the question, with its change.
await click('Cancel'); expect(screen.queryByRole('alertdialog')).toBeNull(); expect(named().value).toBe('Field sales'); expect(posts('roles')).toHaveLength(0)
expect((await panel('Sales')).contains(screen.getByRole('button', { name: 'Save role' }))).toBe(true); await moment()
await click('Remove role'); await click('Remove role'); await screen.findByText('Saved.')
expect(sent('roles')).toEqual([{ id: 'sales', removed: true }]); expect(where()).toBe('/apps/access/roles'); await closed()
})
it('a role save that did not finish says so and reads the list again', async () => {
await open('roles/office'); await screen.findByRole('button', { name: 'Save role' }); failed.add('roles'); await click('Save role')
await screen.findByText('That did not finish. Check the list below before trying again.')
await row('Office'); expect(where()).toBe('/apps/access/roles')
expect(posts('roles')).toHaveLength(1); expect(reads('status').length).toBeGreaterThan(1)
})
it('every row opens its item from the link in its first cell, and no list has an Edit button', async () => {
for (const [path, name, links] of [['', 'People', ['gone@shop.com', 'kim@shop.com', 'lee@shop.com', 'sam@shop.com', 'pat@shop.com']], ['roles', 'Roles', ['Office', 'Sales', 'Support']]]) {
await open(path); const table = await screen.findByRole('table', { name })
expect(Array.from(table.querySelectorAll('tbody td:first-child a')).map(link => link.textContent), path).toEqual(links)
expect(screen.queryByRole('link', { name: 'Edit' }), path).toBeNull()
cleanup()
}
})
it('names each person and role an update unticked, with the app they lost, in the one notice spot on both views until the next save', async () => {
const [kim, lost] = [{ email: 'kim@example.com', apps: ['Orders'] }, { name: 'Sales', apps: ['Payroll', 'Orders'] }]
const after = (first, second) => !!(first.compareDocumentPosition(second) & Node.DOCUMENT_POSITION_FOLLOWING)
roster.unticked = { people: [kim], roles: [lost] }
await open(); const heading = await screen.findByText('Unticked in this update'); const box = heading.closest ('[data-slot="alert"]')
// Marked by a "!" in the text and by bold words, never by colour alone.
expect([heading.tagName, heading.textContent]).toEqual(['STRONG', '! Unticked in this update'])
// One line per person and per role, each with what they lost, then what to do about it.
expect(Array.from(box.querySelectorAll('p, li')).map(item => item.textContent)).toEqual(['! Unticked in this update', 'They could only look, and a tick gives everything:',
'kim@example.com: Orders', 'Sales role: Payroll, Orders', 'Tick an app to give it back.'])
expect([after(screen.getByRole('navigation', { name: 'Access views' }), box), after(box, screen.getByRole('table', { name: 'People' }))]).toEqual([true, true])
// It shows on Roles too, and beside an opened role.
await fireEvent.click(screen.getByRole('link', { name: 'Roles 3' })); await screen.findByRole('region', { name: 'Roles' }); expect(screen.getAllByText('Unticked in this update')).toHaveLength(1)
await fireEvent.click(screen.getByRole('link', { name: 'Add role' })); await panel('Add role'); expect(screen.getAllByText('Unticked in this update')).toHaveLength(1); cleanup()
// A role alone, or a person alone, is named the same way.
roster.unticked = { people: [], roles: [lost] }; await open(); await screen.findByText('Sales role: Payroll, Orders'); expect(screen.queryByText(/kim@example\.com/)).toBeNull(); cleanup()
roster.unticked = { people: [kim], roles: [] }; await open(); await screen.findByText('kim@example.com: Orders'); expect(screen.queryByText(/ role: /)).toBeNull(); cleanup()
// The next save clears the names, and the notice with them: nobody unticked, no notice.
roster.unticked = { people: [], roles: [] }; await open(); await screen.findByRole('link', { name: 'Add person' })
expect([screen.queryByText('Unticked in this update'), document.querySelector('[data-slot="alert"]')]).toEqual([null, null])
})
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |