import { Given, When, Then } from '@cucumber/cucumber' import { strict as assert } from 'assert' import type { PerausetWorld } from '../support/world.js' import { ensureUser } from '../support/db.js' Given('a user {string} exists', function (this: PerausetWorld, email: string) { this.createdUsers[email] = ensureUser(email, []) }) Given('a user {string} exists with roles {string}', function (this: PerausetWorld, email: string, roles: string) { const rolesArray = roles.split(',').map((r) => r.trim()) this.createdUsers[email] = ensureUser(email, rolesArray) }) When('I get my own user profile', async function (this: PerausetWorld) { const userId = await this.getSessionUserId() await this.get(`/users/${userId}`) }) When('I update my profile with displayName {string}', async function (this: PerausetWorld, displayName: string) { const userId = await this.getSessionUserId() await this.put(`/users/${userId}/profile`, { displayName }) }) Then('the response body should be a list with field {string}', function (this: PerausetWorld, field: string) { assert.ok(this.lastBody, 'No response body') assert.ok(Array.isArray(this.lastBody[field]), `Expected body.${field} to be an array, got: ${JSON.stringify(this.lastBody[field])}`) }) Then('the response body field {string} should be {string}', function (this: PerausetWorld, field: string, value: string) { assert.ok(this.lastBody, 'No response body') assert.equal( this.lastBody[field], value, `Expected body.${field} to be "${value}", got: "${this.lastBody[field]}"` ) })