addPdasVisitor.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { CODAMA_ERROR__VISITORS__CANNOT_ADD_DUPLICATED_PDA_NAMES, CodamaError } from '@codama/errors';
  2. import {
  3. constantPdaSeedNodeFromProgramId,
  4. constantPdaSeedNodeFromString,
  5. pdaNode,
  6. programNode,
  7. publicKeyTypeNode,
  8. variablePdaSeedNode,
  9. } from '@codama/nodes';
  10. import { visit } from '@codama/visitors-core';
  11. import { expect, test } from 'vitest';
  12. import { addPdasVisitor } from '../src';
  13. test('it adds PDA nodes to a program', () => {
  14. // Given a program with a single PDA.
  15. const node = programNode({
  16. name: 'myProgram',
  17. pdas: [
  18. pdaNode({
  19. name: 'associatedToken',
  20. seeds: [
  21. variablePdaSeedNode('owner', publicKeyTypeNode()),
  22. constantPdaSeedNodeFromProgramId(),
  23. variablePdaSeedNode('mint', publicKeyTypeNode()),
  24. ],
  25. }),
  26. ],
  27. publicKey: 'Epo9rxh99jpeeWabRZi4tpgUVxZQeVn9vbbDjUztJtu4',
  28. });
  29. // When we add two more PDAs.
  30. const newPdas = [
  31. pdaNode({
  32. name: 'metadata',
  33. seeds: [
  34. constantPdaSeedNodeFromString('utf8', 'metadata'),
  35. constantPdaSeedNodeFromProgramId(),
  36. variablePdaSeedNode('mint', publicKeyTypeNode()),
  37. ],
  38. }),
  39. pdaNode({
  40. name: 'masterEdition',
  41. seeds: [
  42. constantPdaSeedNodeFromString('utf8', 'metadata'),
  43. constantPdaSeedNodeFromProgramId(),
  44. variablePdaSeedNode('mint', publicKeyTypeNode()),
  45. constantPdaSeedNodeFromString('utf8', 'edition'),
  46. ],
  47. }),
  48. ];
  49. const result = visit(node, addPdasVisitor({ myProgram: newPdas }));
  50. // Then we expect the following program to be returned.
  51. expect(result).toEqual({ ...node, pdas: [...node.pdas, ...newPdas] });
  52. });
  53. test('it fails to add a PDA if its name conflicts with an existing PDA on the program', () => {
  54. // Given a program with a PDA named "myPda".
  55. const node = programNode({
  56. name: 'myProgram',
  57. pdas: [
  58. pdaNode({
  59. name: 'myPda',
  60. seeds: [
  61. variablePdaSeedNode('owner', publicKeyTypeNode()),
  62. constantPdaSeedNodeFromProgramId(),
  63. variablePdaSeedNode('mint', publicKeyTypeNode()),
  64. ],
  65. }),
  66. ],
  67. publicKey: 'Epo9rxh99jpeeWabRZi4tpgUVxZQeVn9vbbDjUztJtu4',
  68. });
  69. // When we try to add another PDA with the same name.
  70. const fn = () =>
  71. visit(
  72. node,
  73. addPdasVisitor({
  74. myProgram: [
  75. pdaNode({
  76. name: 'myPda',
  77. seeds: [
  78. constantPdaSeedNodeFromString('utf8', 'metadata'),
  79. constantPdaSeedNodeFromProgramId(),
  80. variablePdaSeedNode('mint', publicKeyTypeNode()),
  81. ],
  82. }),
  83. ],
  84. }),
  85. );
  86. // Then we expect the following error to be thrown.
  87. expect(fn).toThrow(
  88. new CodamaError(CODAMA_ERROR__VISITORS__CANNOT_ADD_DUPLICATED_PDA_NAMES, {
  89. duplicatedPdaNames: ['myPda'],
  90. program: node,
  91. programName: 'myProgram',
  92. }),
  93. );
  94. });