Approving a specific proposal
Task: a user may approve only the current version of their own proposal. An unauthorised person, an old version and a repeated approval must be rejected. Write down the allowed states and transitions.
Tests: permissions and control over actions (in the original, TypeScript types too).
States: “awaiting approval → approved → executed”, tied to the version of an exactly specified proposal. Write approve(state, command), where state is {status: 'pending', proposal} and command is {version, actor, allowed}. Return a new approved state, or throw an error whose message contains state, forbidden or stale.
This is a local function; it doesn't implement a database, signatures, expiry or a remote write. In production the state change also has to be protected against two simultaneous requests.
Follow-up: what happens after an old Teams card is clicked?
You will practise
- Write down the states and allowed transitions
- Check identity and permissions
- Reject an old version and a repeated approval
The 1 tests
Tests ported from the source material's reference.test.ts (node:test). In the browser they run with the same set of assertions.
approval: identity, version and double-approval checks
const pending = { status: 'pending', proposal: { id: 'p', owner: 'u', version: 2, amount: 100 } };
assert.throws(() => approve(pending, { actor: 'other', allowed: true, version: 2 }), /forbidden/);
assert.throws(() => approve(pending, { actor: 'u', allowed: true, version: 1 }), /stale/);
const approved = approve(pending, { actor: 'u', allowed: true, version: 2 });
assert.equal(approved.status, 'approved');
assert.throws(() => approve(approved, { actor: 'u', allowed: true, version: 2 }), /state/);Related questions
Source: AI & Agents — practice tasks — An exercise written for the study programme, not a confirmed interview task.