pda.test.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {
  2. instructionAccountNode,
  3. instructionNode,
  4. pdaLinkNode,
  5. pdaNode,
  6. pdaValueNode,
  7. programNode,
  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 functions from the linked pda', async () => {
  14. // Given the following node.
  15. const node = programNode({
  16. instructions: [
  17. instructionNode({
  18. accounts: [
  19. instructionAccountNode({
  20. defaultValue: pdaValueNode(pdaLinkNode('counter')),
  21. isSigner: true,
  22. isWritable: true,
  23. name: 'counter',
  24. }),
  25. ],
  26. name: 'createCounter',
  27. }),
  28. ],
  29. name: 'myProgram',
  30. pdas: [pdaNode({ name: 'counter', seeds: [] })],
  31. publicKey: '1111',
  32. });
  33. // When we render it.
  34. const renderMap = visit(node, getRenderMapVisitor());
  35. // Then we expect the following to be exported.
  36. await renderMapContains(renderMap, 'instructions/createCounter.ts', ['await findCounterPda()']);
  37. // And we expect the following imports.
  38. await renderMapContainsImports(renderMap, 'instructions/createCounter.ts', {
  39. '../pdas': ['findCounterPda'],
  40. });
  41. });
  42. test('it can override the import of a linked account', async () => {
  43. // Given the following node.
  44. const node = programNode({
  45. instructions: [
  46. instructionNode({
  47. accounts: [
  48. instructionAccountNode({
  49. defaultValue: pdaValueNode(pdaLinkNode('counter')),
  50. isSigner: true,
  51. isWritable: true,
  52. name: 'counter',
  53. }),
  54. ],
  55. name: 'createCounter',
  56. }),
  57. ],
  58. name: 'myProgram',
  59. pdas: [pdaNode({ name: 'counter', seeds: [] })],
  60. publicKey: '1111',
  61. });
  62. // When we render it using a custom import.
  63. const renderMap = visit(
  64. node,
  65. getRenderMapVisitor({
  66. linkOverrides: {
  67. pdas: { counter: 'hooked' },
  68. },
  69. }),
  70. );
  71. // Then we expect the following to be exported.
  72. await renderMapContains(renderMap, 'instructions/createCounter.ts', ['await findCounterPda()']);
  73. // And we expect the imports to be overridden.
  74. await renderMapContainsImports(renderMap, 'instructions/createCounter.ts', {
  75. '../../hooked': ['findCounterPda'],
  76. });
  77. });