|
| 1 | +import PKC from "@pkcprotocol/pkc-js"; |
| 2 | + |
| 3 | +const RPC_URL = "ws://localhost:9138"; |
| 4 | + |
| 5 | +const pkc = await PKC({ pkcRpcClientsOptions: [RPC_URL] }); |
| 6 | + |
| 7 | +await new Promise((resolve, reject) => { |
| 8 | + const timeout = setTimeout(() => reject(new Error("Timed out")), 20000); |
| 9 | + pkc.once("communitieschange", () => { clearTimeout(timeout); resolve(); }); |
| 10 | +}); |
| 11 | + |
| 12 | +const address = pkc.communities[0]; |
| 13 | +if (!address) { console.log("No communities"); await pkc.destroy(); process.exit(1); } |
| 14 | + |
| 15 | +const community = await pkc.createCommunity({ address }); |
| 16 | +console.log("Community:", address); |
| 17 | +console.log("Current roles:", JSON.stringify(community.roles, null, 2)); |
| 18 | + |
| 19 | +// Find any existing role to test null removal |
| 20 | +const existingRole = Object.entries(community.roles || {})[0]; |
| 21 | +if (!existingRole) { console.log("No roles to test with"); await pkc.destroy(); process.exit(0); } |
| 22 | + |
| 23 | +const [roleAddr, roleValue] = existingRole; |
| 24 | +console.log(`\nTesting null removal on: ${roleAddr} (${roleValue?.role})`); |
| 25 | + |
| 26 | +// Test 1: Try removing with null |
| 27 | +console.log("\n--- Test: Removing role with null ---"); |
| 28 | +try { |
| 29 | + await community.edit({ roles: { [roleAddr]: null } }); |
| 30 | + console.log("SUCCESS: null works"); |
| 31 | +} catch (e) { |
| 32 | + console.error("FAILED with null:", e.message || e); |
| 33 | +} |
| 34 | + |
| 35 | +// Test 2: Try removing with undefined |
| 36 | +console.log("\n--- Test: Removing role with undefined ---"); |
| 37 | +try { |
| 38 | + const community2 = await pkc.createCommunity({ address }); |
| 39 | + await community2.edit({ roles: { [roleAddr]: undefined } }); |
| 40 | + console.log("SUCCESS: undefined works"); |
| 41 | +} catch (e) { |
| 42 | + console.error("FAILED with undefined:", e.message || e); |
| 43 | +} |
| 44 | + |
| 45 | +// Check final state |
| 46 | +const community3 = await pkc.createCommunity({ address }); |
| 47 | +console.log("\nFinal roles:", JSON.stringify(community3.roles, null, 2)); |
| 48 | + |
| 49 | +// Re-add the role back |
| 50 | +console.log("\n--- Restoring original role ---"); |
| 51 | +try { |
| 52 | + await community3.edit({ roles: { [roleAddr]: roleValue } }); |
| 53 | + console.log("Restored"); |
| 54 | +} catch (e) { |
| 55 | + console.error("Failed to restore:", e.message || e); |
| 56 | +} |
| 57 | + |
| 58 | +await pkc.destroy(); |
0 commit comments