{"path":"scripts/devnet-updte-test.js","size":4319,"locked":false,"binary":false,"truncated":false,"content":"/**\n * devnet-update-test.js — verifies the UPDATE fallback in lib/sns.js's\n * writeIpfsRecord, which has never been independently tested. The main\n * devnet-test.js always registers a fresh, uniquely-named domain, so it\n * only ever exercises the CREATE path. This script uses one FIXED domain\n * name and calls the real production writeIpfsRecord() TWICE — the\n * second call is the one that actually hits the untested branch, since\n * the record will already exist from the first call.\n *\n * Reuses the same persisted wallet as devnet-test.js (.devnet-test-wallet.json)\n * — run devnet-test.js at least once first so that wallet exists and is\n * funded/wrapped.\n */\n\nimport { readFileSync, existsSync } from 'node:fs';\nimport { Connection, Keypair, Transaction, SystemProgram } from '@solana/web3.js';\nimport { getAssociatedTokenAddressSync, NATIVE_MINT } from '@solana/spl-token';\nimport { devnet } from '@bonfida/spl-name-service';\nimport { sendAndConfirm } from '../lib/solana-confirm.js';\nimport { writeIpfsRecord } from '../lib/sns.js';\n\nconst DEVNET_RPC = process.env.ANCHR_DEVNET_RPC_URL || 'https://api.devnet.solana.com';\nconst WALLET_FILE = '.devnet-test-wallet.json';\nconst FIXED_DOMAIN = 'anchr-update-fallback-test'; // NOT timestamped — reused every run, on purpose\n\nasync function main() {\n  const connection = new Connection(DEVNET_RPC, 'confirmed');\n  console.log('Using RPC:', DEVNET_RPC);\n\n  if (!existsSync(WALLET_FILE)) {\n    throw new Error(`No ${WALLET_FILE} found — run devnet-test.js at least once first to create a funded wallet.`);\n  }\n  const secretKey = Uint8Array.from(JSON.parse(readFileSync(WALLET_FILE, 'utf-8')));\n  const wallet = Keypair.fromSecretKey(secretKey);\n  console.log('Wallet:', wallet.publicKey.toBase58());\n\n  // Bridge this wallet into the REAL production code path — lib/sns.js's\n  // loadWallet() reads this exact env var.\n  process.env.ANCHR_WALLET_SECRET_KEY = JSON.stringify(Array.from(secretKey));\n\n  // Register the fixed domain if it doesn't already exist (skip if it does\n  // — same reused-across-runs pattern as devnet-test.js).\n  const { pubkey: domainKey } = devnet.utils.getDomainKeySync(FIXED_DOMAIN);\n  const domainInfo = await connection.getAccountInfo(domainKey);\n\n  if (domainInfo) {\n    console.log(`Domain \"${FIXED_DOMAIN}\" already registered — skipping registration.`);\n  } else {\n    console.log(`Registering \"${FIXED_DOMAIN}\" for the first time...`);\n    const ata = getAssociatedTokenAddressSync(NATIVE_MINT, wallet.publicKey);\n    const registerIx = await devnet.bindings.registerDomainNameV2(\n      connection,\n      FIXED_DOMAIN,\n      1_000,\n      wallet.publicKey,\n      ata,\n      NATIVE_MINT\n    );\n    const registerTx = new Transaction();\n    if (Array.isArray(registerIx)) {\n      registerTx.add(...registerIx);\n    } else {\n      registerTx.add(registerIx);\n    }\n    const registerSig = await sendAndConfirm(connection, registerTx, [wallet]);\n    console.log('Registered. Tx:', `https://explorer.solana.com/tx/${registerSig}?cluster=devnet`);\n  }\n\n  // First call — likely CREATE (if no record exists yet) or already the\n  // fallback if a previous run of THIS script got partway through.\n  console.log('\\n--- First writeIpfsRecord call ---');\n  const first = await writeIpfsRecord(FIXED_DOMAIN, 'bafyfirstcall000000000000000000000000000000000000000000000', {\n    rpcUrl: DEVNET_RPC\n  });\n  console.log('Success. Tx:', `${first.explorerUrl}?cluster=devnet`);\n\n  // Second call — the record now DEFINITELY exists from the first call,\n  // so this MUST hit the update fallback. This is the actual test.\n  console.log('\\n--- Second writeIpfsRecord call (should trigger the update fallback) ---');\n  const second = await writeIpfsRecord(FIXED_DOMAIN, 'bafysecondcall00000000000000000000000000000000000000000000', {\n    rpcUrl: DEVNET_RPC\n  });\n  console.log('Success. Tx:', `${second.explorerUrl}?cluster=devnet`);\n\n  console.log('\\n--- RESULT ---');\n  console.log(\n    '✅ Both calls succeeded. If you saw the \"falling back to update\" log ' +\n    'line above the second call, the update path is confirmed working — ' +\n    'not just theoretically reachable, but actually exercised successfully.'\n  );\n}\n\nmain().catch((err) => {\n  console.error('\\n❌ Update-fallback test failed:', err);\n  process.exit(1);\n});\n"}