accountsPage.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import {
  2. accountNode,
  3. bytesTypeNode,
  4. fixedSizeTypeNode,
  5. pdaLinkNode,
  6. pdaNode,
  7. programNode,
  8. variablePdaSeedNode,
  9. } from '@kinobi-so/nodes';
  10. import { visit } from '@kinobi-so/visitors-core';
  11. import test from 'ava';
  12. import { getRenderMapVisitor } from '../src/index.js';
  13. import { codeContains } from './_setup.js';
  14. test('it renders a byte array seed used on an account', t => {
  15. // Given the following program with 1 account and 1 pda with a byte array as seeds.
  16. const node = programNode({
  17. accounts: [
  18. accountNode({
  19. name: 'testAccount',
  20. pda: pdaLinkNode('testPda'),
  21. }),
  22. ],
  23. name: 'splToken',
  24. pdas: [
  25. // Byte array seeds.
  26. pdaNode({
  27. name: 'testPda',
  28. seeds: [variablePdaSeedNode('byteArraySeed', fixedSizeTypeNode(bytesTypeNode(), 32))],
  29. }),
  30. ],
  31. publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
  32. });
  33. // When we render it.
  34. const renderMap = visit(node, getRenderMapVisitor());
  35. // Then we expect the following identifier and reference to the byte array
  36. // as a parameters to be rendered.
  37. codeContains(t, renderMap.get('accounts/test_account.rs'), [`byte_array_seed: [u8; 32],`, `&byte_array_seed,`]);
  38. });
  39. test('it renders an empty array seed used on an account', t => {
  40. // Given the following program with 1 account and 1 pda with empty seeds.
  41. const node = programNode({
  42. accounts: [
  43. accountNode({
  44. discriminators: [],
  45. name: 'testAccount',
  46. pda: pdaLinkNode('testPda'),
  47. }),
  48. ],
  49. name: 'splToken',
  50. pdas: [
  51. // Empty array seeds.
  52. pdaNode({ name: 'testPda', seeds: [] }),
  53. ],
  54. publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
  55. });
  56. // When we render it.
  57. const renderMap = visit(node, getRenderMapVisitor());
  58. // Then we expect the following identifier and reference to the byte array
  59. // as a parameters to be rendered.
  60. codeContains(t, renderMap.get('accounts/test_account.rs'), [/pub fn find_pda\(/, /&\[\s*\]/]);
  61. });