InstructionNode.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import {
  2. fieldDiscriminatorNode,
  3. instructionAccountNode,
  4. instructionArgumentNode,
  5. instructionByteDeltaNode,
  6. instructionNode,
  7. instructionRemainingAccountsNode,
  8. numberTypeNode,
  9. numberValueNode,
  10. publicKeyTypeNode,
  11. resolverValueNode,
  12. } from '@kinobi-so/nodes';
  13. import test from 'ava';
  14. import {
  15. deleteNodesVisitorMacro,
  16. getDebugStringVisitorMacro,
  17. identityVisitorMacro,
  18. mergeVisitorMacro,
  19. } from './_setup.js';
  20. const node = instructionNode({
  21. accounts: [
  22. instructionAccountNode({
  23. isSigner: true,
  24. isWritable: true,
  25. name: 'source',
  26. }),
  27. instructionAccountNode({
  28. isSigner: false,
  29. isWritable: true,
  30. name: 'destination',
  31. }),
  32. ],
  33. arguments: [
  34. instructionArgumentNode({
  35. name: 'discriminator',
  36. type: numberTypeNode('u32'),
  37. }),
  38. instructionArgumentNode({
  39. name: 'amount',
  40. type: numberTypeNode('u64'),
  41. }),
  42. ],
  43. discriminators: [fieldDiscriminatorNode('discriminator')],
  44. name: 'transferSol',
  45. });
  46. test(mergeVisitorMacro, node, 8);
  47. test(identityVisitorMacro, node);
  48. test(deleteNodesVisitorMacro, node, '[instructionNode]', null);
  49. test(deleteNodesVisitorMacro, node, '[instructionAccountNode]', {
  50. ...node,
  51. accounts: [],
  52. });
  53. test(deleteNodesVisitorMacro, node, '[instructionArgumentNode]', {
  54. ...node,
  55. arguments: [],
  56. });
  57. test(deleteNodesVisitorMacro, node, '[fieldDiscriminatorNode]', {
  58. ...node,
  59. discriminators: [],
  60. });
  61. test(
  62. getDebugStringVisitorMacro,
  63. node,
  64. `
  65. instructionNode [transferSol]
  66. | instructionAccountNode [source.writable.signer]
  67. | instructionAccountNode [destination.writable]
  68. | instructionArgumentNode [discriminator]
  69. | | numberTypeNode [u32]
  70. | instructionArgumentNode [amount]
  71. | | numberTypeNode [u64]
  72. | fieldDiscriminatorNode [discriminator]`,
  73. );
  74. // Extra arguments.
  75. const nodeWithExtraArguments = instructionNode({
  76. extraArguments: [
  77. instructionArgumentNode({
  78. name: 'myExtraArgument',
  79. type: publicKeyTypeNode(),
  80. }),
  81. ],
  82. name: 'myInstruction',
  83. });
  84. test('mergeVisitor: extraArguments', mergeVisitorMacro, nodeWithExtraArguments, 3);
  85. test('identityVisitor: extraArguments', identityVisitorMacro, nodeWithExtraArguments);
  86. // Remaining accounts.
  87. const nodeWithRemainingAccounts = instructionNode({
  88. name: 'myInstruction',
  89. remainingAccounts: [instructionRemainingAccountsNode(resolverValueNode('myResolver'))],
  90. });
  91. test('mergeVisitor: remainingAccounts', mergeVisitorMacro, nodeWithRemainingAccounts, 3);
  92. test('identityVisitor: remainingAccounts', identityVisitorMacro, nodeWithRemainingAccounts);
  93. // Byte deltas.
  94. const nodeWithByteDeltas = instructionNode({
  95. byteDeltas: [instructionByteDeltaNode(numberValueNode(42))],
  96. name: 'myInstruction',
  97. });
  98. test('mergeVisitor: byteDeltas', mergeVisitorMacro, nodeWithByteDeltas, 3);
  99. test('identityVisitor: byteDeltas', identityVisitorMacro, nodeWithByteDeltas);
  100. // Sub-instructions.
  101. const nodeWithSubInstructions = instructionNode({
  102. name: 'myInstruction',
  103. subInstructions: [instructionNode({ name: 'mySubInstruction1' }), instructionNode({ name: 'mySubInstruction2' })],
  104. });
  105. test('mergeVisitor: subInstructions', mergeVisitorMacro, nodeWithSubInstructions, 3);
  106. test('identityVisitor: subInstructions', identityVisitorMacro, nodeWithSubInstructions);