UNPKG

@metamask/snaps-simulation

Version:

A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment

1 lines 4.54 kB
{"version":3,"file":"validation.mjs","sourceRoot":"","sources":["../src/validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,4BAA4B;AAEjD,OAAO,EAAE,cAAc,EAAE,8BAA8B;AACvD,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,wBAAwB;AAEtD,OAAO,EAAE,gBAAgB,EAAE,wBAAoB;AAa/C;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CACjC,EAAiB;IAEjB,MAAM,CAAC,WAAW,CAAC,EAAE,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,IAAI,KAAK,UAAU,CAAC,KAAK,CAAC,CAAC;AAClE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,0BAA0B,CACxC,EAAiB;IAEjB,MAAM,CAAC,WAAW,CAAC,EAAE,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,IAAI,KAAK,UAAU,CAAC,YAAY,CAAC,CAAC;AACzE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAClC,EAAiB;IAEjB,MAAM,CAAC,WAAW,CAAC,EAAE,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,IAAI,KAAK,UAAU,CAAC,MAAM,CAAC,CAAC;AACnE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAClC,EAAiB;IAEjB,MAAM,CAAC,CAAC,WAAW,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC;AACnC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,2BAA2B,CACzC,EAA+C;IAE/C,MAAM,MAAM,GAAG,gBAAgB,CAAgB,EAAE,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IAErE,MAAM,CAAC,MAAM,IAAI,cAAc,CAAC,MAAM,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC;AACxD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kCAAkC,CAChD,EAA+C;IAE/C,MAAM,MAAM,GAAG,gBAAgB,CAAgB,EAAE,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IAErE,MAAM,CAAC,MAAM,IAAI,cAAc,CAAC,MAAM,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC;AACxD,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,6BAA6B,CAC3C,EAA+C;IAE/C,MAAM,MAAM,GAAG,gBAAgB,CAAgB,EAAE,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IAErE,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;AAClB,CAAC","sourcesContent":["import { DialogType } from '@metamask/snaps-sdk';\nimport type { FooterElement } from '@metamask/snaps-sdk/jsx';\nimport { getJsxChildren } from '@metamask/snaps-utils';\nimport { assert, hasProperty } from '@metamask/utils';\n\nimport { getElementByType } from './interface';\nimport type {\n DefaultSnapInterface,\n DefaultSnapInterfaceWithFooter,\n DefaultSnapInterfaceWithoutFooter,\n DefaultSnapInterfaceWithPartialFooter,\n SnapAlertInterface,\n SnapConfirmationInterface,\n SnapInterface,\n SnapInterfaceActions,\n SnapPromptInterface,\n} from './types';\n\n/**\n * Ensure that the actual interface is an alert dialog.\n *\n * @param ui - The interface to verify.\n */\nexport function assertIsAlertDialog(\n ui: SnapInterface,\n): asserts ui is SnapAlertInterface & SnapInterfaceActions {\n assert(hasProperty(ui, 'type') && ui.type === DialogType.Alert);\n}\n\n/**\n * Ensure that the actual interface is a confirmation dialog.\n *\n * @param ui - The interface to verify.\n */\nexport function assertIsConfirmationDialog(\n ui: SnapInterface,\n): asserts ui is SnapConfirmationInterface & SnapInterfaceActions {\n assert(hasProperty(ui, 'type') && ui.type === DialogType.Confirmation);\n}\n\n/**\n * Ensure that the actual interface is a Prompt dialog.\n *\n * @param ui - The interface to verify.\n */\nexport function assertIsPromptDialog(\n ui: SnapInterface,\n): asserts ui is SnapPromptInterface & SnapInterfaceActions {\n assert(hasProperty(ui, 'type') && ui.type === DialogType.Prompt);\n}\n\n/**\n * Ensure that the actual interface is a custom dialog.\n *\n * @param ui - The interface to verify.\n */\nexport function assertIsCustomDialog(\n ui: SnapInterface,\n): asserts ui is DefaultSnapInterface & SnapInterfaceActions {\n assert(!hasProperty(ui, 'type'));\n}\n\n/**\n * Ensure that the actual interface is a custom dialog with a complete footer.\n *\n * @param ui - The interface to verify.\n */\nexport function assertCustomDialogHasFooter(\n ui: DefaultSnapInterface & SnapInterfaceActions,\n): asserts ui is DefaultSnapInterfaceWithFooter & SnapInterfaceActions {\n const footer = getElementByType<FooterElement>(ui.content, 'Footer');\n\n assert(footer && getJsxChildren(footer).length === 2);\n}\n\n/**\n * Ensure that the actual interface is a custom dialog with a partial footer.\n *\n * @param ui - The interface to verify.\n */\nexport function assertCustomDialogHasPartialFooter(\n ui: DefaultSnapInterface & SnapInterfaceActions,\n): asserts ui is DefaultSnapInterfaceWithPartialFooter & SnapInterfaceActions {\n const footer = getElementByType<FooterElement>(ui.content, 'Footer');\n\n assert(footer && getJsxChildren(footer).length === 1);\n}\n\n/**\n * Ensure that the actual interface is a custom dialog without a footer.\n *\n * @param ui - The interface to verify.\n */\nexport function assertCustomDialogHasNoFooter(\n ui: DefaultSnapInterface & SnapInterfaceActions,\n): asserts ui is DefaultSnapInterfaceWithoutFooter & SnapInterfaceActions {\n const footer = getElementByType<FooterElement>(ui.content, 'Footer');\n\n assert(!footer);\n}\n"]}