program.test.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import {
  2. instructionAccountNode,
  3. instructionNode,
  4. pdaNode,
  5. programLinkNode,
  6. programNode,
  7. rootNode,
  8. } from '@kinobi-so/nodes';
  9. import { visit } from '@kinobi-so/visitors-core';
  10. import { test } from 'vitest';
  11. import { getRenderMapVisitor } from '../../src';
  12. import { renderMapContains, renderMapContainsImports } from '../_setup';
  13. test('it imports constants from the linked program', async () => {
  14. // Given the following node.
  15. const node = rootNode(
  16. programNode({
  17. instructions: [
  18. instructionNode({
  19. accounts: [
  20. instructionAccountNode({
  21. defaultValue: programLinkNode('someOtherProgram'),
  22. isSigner: false,
  23. isWritable: false,
  24. name: 'otherProgram',
  25. }),
  26. ],
  27. name: 'myInstruction',
  28. }),
  29. ],
  30. name: 'myProgram',
  31. pdas: [pdaNode({ name: 'counter', seeds: [] })],
  32. publicKey: '1111',
  33. }),
  34. [programNode({ name: 'someOtherProgram', publicKey: '2222' })],
  35. );
  36. // When we render it.
  37. const renderMap = visit(node, getRenderMapVisitor());
  38. // Then we expect the following to be exported.
  39. await renderMapContains(renderMap, 'instructions/myInstruction.ts', ['SOME_OTHER_PROGRAM_PROGRAM_ADDRESS']);
  40. // And we expect the following imports.
  41. await renderMapContainsImports(renderMap, 'instructions/myInstruction.ts', {
  42. '../programs': ['SOME_OTHER_PROGRAM_PROGRAM_ADDRESS'],
  43. });
  44. });
  45. test('it can override the import of a linked account', async () => {
  46. // Given the following node.
  47. const node = rootNode(
  48. programNode({
  49. instructions: [
  50. instructionNode({
  51. accounts: [
  52. instructionAccountNode({
  53. defaultValue: programLinkNode('someOtherProgram'),
  54. isSigner: false,
  55. isWritable: false,
  56. name: 'otherProgram',
  57. }),
  58. ],
  59. name: 'myInstruction',
  60. }),
  61. ],
  62. name: 'myProgram',
  63. pdas: [pdaNode({ name: 'counter', seeds: [] })],
  64. publicKey: '1111',
  65. }),
  66. [programNode({ name: 'someOtherProgram', publicKey: '2222' })],
  67. );
  68. // When we render it using a custom import.
  69. const renderMap = visit(
  70. node,
  71. getRenderMapVisitor({
  72. linkOverrides: {
  73. programs: { someOtherProgram: 'hooked' },
  74. },
  75. }),
  76. );
  77. // Then we expect the following to be exported.
  78. await renderMapContains(renderMap, 'instructions/myInstruction.ts', ['SOME_OTHER_PROGRAM_PROGRAM_ADDRESS']);
  79. // And we expect the imports to be overridden.
  80. await renderMapContainsImports(renderMap, 'instructions/myInstruction.ts', {
  81. '../../hooked': ['SOME_OTHER_PROGRAM_PROGRAM_ADDRESS'],
  82. });
  83. });