getValidationItemsVisitor.test.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { programNode, publicKeyTypeNode, structFieldTypeNode, structTypeNode, tupleTypeNode } from '@codama/nodes';
  2. import { visit } from '@codama/visitors-core';
  3. import { expect, test } from 'vitest';
  4. import { getValidationItemsVisitor, validationItem } from '../src';
  5. test('it validates program nodes', () => {
  6. // Given the following program node with empty strings.
  7. const node = programNode({
  8. accounts: [],
  9. definedTypes: [],
  10. errors: [],
  11. instructions: [],
  12. name: '',
  13. origin: undefined,
  14. publicKey: '',
  15. // @ts-expect-error Empty string does not match ProgramVersion.
  16. version: '',
  17. });
  18. // When we get the validation items using a visitor.
  19. const items = visit(node, getValidationItemsVisitor());
  20. // Then we expect the following validation errors.
  21. expect(items).toEqual([
  22. validationItem('error', 'Program has no name.', node, []),
  23. validationItem('error', 'Program has no public key.', node, []),
  24. validationItem('warn', 'Program has no version.', node, []),
  25. validationItem('info', 'Program has no origin.', node, []),
  26. ]);
  27. });
  28. test('it validates nested nodes', () => {
  29. // Given the following tuple with nested issues.
  30. const node = tupleTypeNode([
  31. tupleTypeNode([]),
  32. structTypeNode([
  33. structFieldTypeNode({ name: 'owner', type: publicKeyTypeNode() }),
  34. structFieldTypeNode({ name: 'owner', type: publicKeyTypeNode() }),
  35. ]),
  36. ]);
  37. // When we get the validation items using a visitor.
  38. const items = visit(node, getValidationItemsVisitor());
  39. // Then we expect the following validation errors.
  40. const tupleNode = node.items[0];
  41. const structNode = node.items[1];
  42. expect(items).toEqual([
  43. validationItem('warn', 'Tuple has no items.', tupleNode, [node]),
  44. validationItem('error', 'Struct field name "owner" is not unique.', structNode.fields[0], [node]),
  45. ]);
  46. });