import { describe, expect, test } from 'bun:test' import { existsSync, readdirSync, readFileSync } from 'node:fs' import { join, resolve } from 'node:path' function dashboardTemplates(directory: string): string[] { const files: string[] = [] for (const entry of readdirSync(directory, { withFileTypes: true })) { const path = join(directory, entry.name) if (entry.isDirectory()) files.push(...dashboardTemplates(path)) else if (path.endsWith('.stx')) files.push(path) } return files } describe('dashboard button contract', () => { test('documents the canonical dashboard action contract', () => { const skill = readFileSync( resolve('storage/framework/defaults/ai/skills/stacks-dashboard/SKILL.md'), 'utf8', ) expect(skill).toContain('Use `Dashboard/UI/Button.stx` for every dashboard action.') expect(skill).toContain('`bg-gradient-to-b from-blue-500 to-blue-600`') expect(skill).toContain('Use `tag="a"` whenever `href` is reactive') expect(skill).toContain('Prefer component events and named slots') }) test('uses one canonical reusable button component', () => { const button = readFileSync( resolve('storage/framework/defaults/resources/components/Dashboard/UI/Button.stx'), 'utf8', ) expect(existsSync(resolve('storage/framework/defaults/resources/components/Button.stx'))).toBe(false) expect(existsSync(resolve('storage/framework/defaults/resources/components/Buttons/BaseButton.stx'))).toBe(false) // The variant colours live in functions/dashboard-button.ts, shared by // the server render and the live `:variant` toggles (stacksjs/stacks#3180). const variants = readFileSync(resolve('storage/framework/defaults/functions/dashboard-button.ts'), 'utf8') expect(variants).toContain('bg-gradient-to-b from-blue-500 to-blue-600') expect(button).toContain("import { buttonVariantClasses } from '../../../../functions/dashboard-button'") expect(button).toContain("variant?: 'primary' | 'secondary' | 'outline' | 'ghost' | 'danger' | 'success' | 'warning'") expect(button).toContain("tag?: 'button' | 'a'") expect(button).toContain('class?: string') expect(button).toContain("class: customClass = ''") expect(button).toContain('customClass,') expect(button).toContain("const liveDownload = useReactiveProp('download', '')") expect(button).toContain("const liveType = useReactiveProp('type', 'button')") expect(button).toContain("const liveForm = useReactiveProp('form', '')") expect(button.match(/:form="liveForm\(\) \|\| undefined"/g)).toHaveLength(2) expect(button).not.toContain('dataAction') expect(button).not.toContain('dataCloseModal') expect(button).not.toContain('dataEmailId') expect(button).not.toContain('dataErrorMessage') expect(button).not.toContain('dataErrorType') expect(button).not.toContain('dataMethodId') expect(button).toContain("const livePressed = useReactiveProp('pressed', false)") expect(button).toContain("interaction?: 'action' | 'toggle'") expect(button).toContain("@if(interaction === 'toggle')") expect(button).toContain("interaction === 'toggle'") expect(button).toContain('].filter(Boolean).join') expect(button).toContain(':aria-pressed="String(livePressed())"') const pressedStateFiles = [ 'storage/framework/defaults/resources/components/Dashboard/UI/Button.stx', 'storage/framework/defaults/resources/components/Dashboard/Queries/QueryDashboard.stx', 'storage/framework/defaults/resources/components/Dashboard/Email/InboxDashboard.stx', 'storage/framework/defaults/resources/components/Dashboard/Email/EmailActivityDashboard.stx', 'storage/framework/defaults/resources/components/Dashboard/Settings/AppearanceSettingsDashboard.stx', 'storage/framework/defaults/resources/components/Dashboard/Ci/CiDashboard.stx', 'storage/framework/defaults/resources/components/Dashboard/Kanban/KanbanBoardsDashboard.stx', 'storage/framework/defaults/resources/components/Dashboard/Kanban/KanbanCardDialog.stx', ] for (const file of pressedStateFiles) { const source = readFileSync(resolve(file), 'utf8') const pressedExpressions = [...source.matchAll(/:aria-pressed="([^"]+)"/g)].map(match => match[1]) expect(pressedExpressions.length).toBeGreaterThan(0) expect(pressedExpressions.every(expression => expression.startsWith('String('))).toBe(true) } }) test('keeps the Deployment primary style exclusive to the canonical component', () => { const buttonPath = resolve('storage/framework/defaults/resources/components/Dashboard/UI/Button.stx') const files = [ ...dashboardTemplates(resolve('storage/framework/defaults/resources/components/Dashboard')), ...dashboardTemplates(resolve('storage/framework/defaults/views/dashboard')), ] for (const file of files) { if (file === buttonPath) continue const source = readFileSync(file, 'utf8') expect(source).not.toContain('bg-gradient-to-b from-blue-500 to-blue-600') } }) test('keeps native dashboard buttons limited to semantic state controls', () => { const files = [ ...dashboardTemplates(resolve('storage/framework/defaults/resources/components/Dashboard')), ...dashboardTemplates(resolve('storage/framework/defaults/views/dashboard')), ] const allowedStyledControls = [ 'aria-label="Close window"', '@click="requestSelectFile(file.name)"', ] for (const file of files) { if (file.endsWith('/UI/Button.stx')) continue const source = readFileSync(file, 'utf8') const nativeButtons = [...source.matchAll(//g)].map(match => match[0]) const locallyStyledActions = nativeButtons.filter(button => /(?:bg-gradient|(?:bg|from|to)-(?:blue|red|green|orange)-(?:4|5|6|7)00)/.test(button), ) expect(locallyStyledActions.every(button => allowedStyledControls.some(marker => button.includes(marker)), )).toBe(true) } }) test('keeps fixed dialogs inside the shared dashboard modal layer', () => { const files = [ ...dashboardTemplates(resolve('storage/framework/defaults/resources/components/Dashboard')), ...dashboardTemplates(resolve('storage/framework/defaults/views/dashboard')), ] const missing: string[] = [] for (const file of files) { const source = readFileSync(file, 'utf8') const fixedLayers = [...source.matchAll(//g)] .map(match => match[0]) .filter(element => /\bfixed inset-(?:0|y-0)\b/.test(element) && /(?::if=|x-show=|role="(?:alert)?dialog")/.test(element) && !element.includes('dashboard-mobile-sidebar-open'), ) if (fixedLayers.some(element => !element.includes('dashboard-modal-layer'))) missing.push(file) } expect(missing).toEqual([]) }) test('routes primary commerce actions through the canonical component', () => { const files = [ 'CommerceProductsDashboard.stx', 'CommerceProductDialog.stx', 'CommerceProductDeleteDialog.stx', 'CommerceProductDetailDashboard.stx', 'CommerceCustomersDashboard.stx', 'CommerceCustomerDialog.stx', 'CommerceCustomerDeleteDialog.stx', 'CommerceCustomerDetailsDialog.stx', 'CommerceOrdersDashboard.stx', 'CommerceOrderDialog.stx', 'CommerceOrderDeleteDialog.stx', 'CommerceOrderDetailsDialog.stx', 'CommerceCouponsDashboard.stx', 'CommerceCouponDialog.stx', 'CommerceCouponDeleteDialog.stx', 'CommerceCouponDetailsDialog.stx', 'CommerceGiftCardsDashboard.stx', 'CommerceGiftCardDialog.stx', 'CommerceGiftCardDeleteDialog.stx', 'CommerceGiftCardDetailsDialog.stx', 'CommerceCategoriesTable.stx', 'CommerceManufacturersTable.stx', 'CommerceUnitsTable.stx', 'CommerceTaxesTable.stx', 'CommerceVariantsTable.stx', 'CommerceReviewsTable.stx', 'CommerceProductsTable.stx', 'CommerceCustomersTable.stx', 'CommerceOrdersTable.stx', 'CommerceCouponsTable.stx', 'CommerceGiftCardsTable.stx', 'CommercePaymentsTable.stx', 'PrintDevicesTable.stx', 'PrintLogsTable.stx', 'ProductWaitlistTable.stx', 'CommerceOverviewDashboard.stx', 'CommercePosDashboard.stx', 'CommercePosCatalog.stx', 'CommercePosCart.stx', 'CommercePosCheckoutDialog.stx', 'CommercePosReceiptDialog.stx', 'PaymentDetailsDialog.stx', 'PaymentRefundDialog.stx', 'Delivery/DeliveryOverviewDashboard.stx', 'Delivery/ShippingMethodsDashboard.stx', 'Delivery/CouriersDashboard.stx', 'Delivery/DeliveryRoutesDashboard.stx', 'Delivery/LicenseKeysDashboard.stx', 'Delivery/ShippingZonesDashboard.stx', 'Delivery/DigitalDeliveriesDashboard.stx', 'Delivery/ShippingRatesDashboard.stx', 'Delivery/DigitalDeliveryTable.stx', 'Delivery/DeliveryRoutesTable.stx', 'Delivery/LicenseKeysTable.stx', 'Delivery/ShippingMethodsTable.stx', 'Delivery/CouriersTable.stx', 'Delivery/ShippingZonesTable.stx', 'Delivery/ShippingRatesTable.stx', 'Delivery/DeliveryPagination.stx', ] for (const file of files) { const source = readFileSync( resolve('storage/framework/defaults/resources/components/Dashboard/Commerce', file), 'utf8', ) const nativeButtons = [...source.matchAll(/]*>/g)].map(match => match[0]) // ``. Table is itself held to this // contract, in the shared-controls list below - its sort headers, column // menu and pagination were four raw `