Skip to content

Commit 7d70b8c

Browse files
committed
test(test): make edit test less likely to fail in RPC by hardcoding its resolving
1 parent e85a3b8 commit 7d70b8c

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ test-karma-webpack
1010
.plebbit
1111
.plebbit/
1212

13+
# test-server generated files
14+
test/test-server/ens-test-signer.json
15+
1316
# build folder
1417
dist/
1518
/dist/

test/browser-e2e/accounts.test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@ import subplebbitsStore from '../../dist/stores/subplebbits'
88
import testUtils from '../../dist/lib/test-utils'
99
import {offlineIpfs, pubsubIpfs, plebbitRpc} from '../test-server/config'
1010
import signers from '../fixtures/signers'
11+
1112
const subplebbitAddress = signers[0].address
1213
const adminRoleSigner = signers[1]
1314

15+
// Load ENS test signer info from test-server via HTTP
16+
// This allows tests to create subplebbits with a signer that matches 'my-sub.eth'
17+
// The test-server has an endpoint at http://localhost:59281/ens-test-signer
18+
let ensTestSignerPromise
19+
const loadEnsTestSigner = async () => {
20+
const response = await fetch('http://localhost:59281/ens-test-signer')
21+
return await response.json()
22+
}
23+
ensTestSignerPromise = loadEnsTestSigner()
24+
1425
const isBase64 = (testString) => /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}))?$/gm.test(testString)
1526

1627
// large value for manual debugging
@@ -133,7 +144,17 @@ for (const plebbitOptionsType in plebbitOptionsTypes) {
133144
const createdSubplebbitTitle = 'my title'
134145
let subplebbit
135146
await act(async () => {
136-
subplebbit = await rendered.result.current.createSubplebbit({title: createdSubplebbitTitle})
147+
// Use the ensTestSigner so that 'my-sub.eth' resolves to this subplebbit's address
148+
// This is required for the ENS address validation to pass
149+
const ensTestSigner = await ensTestSignerPromise
150+
const createOptions = {title: createdSubplebbitTitle}
151+
if (ensTestSigner) {
152+
createOptions.signer = {
153+
type: ensTestSigner.type,
154+
privateKey: ensTestSigner.privateKey,
155+
}
156+
}
157+
subplebbit = await rendered.result.current.createSubplebbit(createOptions)
137158
})
138159
console.log('after create subplebbit', subplebbit.address)
139160
const createdSubplebbitAddress = subplebbit?.address

test/test-server/index.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import startPlebbitRpc from './start-plebbit-rpc.js'
66
import signers from '../fixtures/signers.js'
77
import {directory as getTmpFolderPath} from 'tempy'
88
import http from 'http'
9+
import fs from 'fs'
10+
import path from 'path'
11+
import {fileURLToPath} from 'url'
12+
13+
const __filename = fileURLToPath(import.meta.url)
14+
const __dirname = path.dirname(__filename)
915
const plebbitDataPath = getTmpFolderPath()
1016

1117
// set up a subplebbit for testing
@@ -41,6 +47,32 @@ const plebbitDataPath = getTmpFolderPath()
4147
})
4248
const signer = await plebbit.createSigner({privateKey, type: 'ed25519'})
4349

50+
// Create a signer for testing ENS domain address editing
51+
// The 'my-sub.eth' domain should resolve to this signer's address
52+
const ensTestSigner = await plebbit.createSigner()
53+
const ensTestDomain = 'my-sub.eth'
54+
55+
// Mock the ENS text record for 'my-sub.eth' to point to ensTestSigner.address
56+
// This allows the test to successfully edit a subplebbit's address to 'my-sub.eth'
57+
const cacheKey = plebbit._clientsManager._getKeyOfCachedDomainTextRecord(ensTestDomain, 'subplebbit-address')
58+
const timestamp = () => Math.floor(Date.now() / 1000)
59+
await plebbit._storage.setItem(cacheKey, {
60+
timestampSeconds: timestamp(),
61+
valueOfTextRecord: ensTestSigner.address,
62+
})
63+
console.log(`Mocked ENS text record '${ensTestDomain}' -> '${ensTestSigner.address}'`)
64+
65+
// Export the ENS test signer info to a file so tests can use it
66+
const ensTestSignerInfo = {
67+
privateKey: ensTestSigner.privateKey,
68+
address: ensTestSigner.address,
69+
type: ensTestSigner.type,
70+
domain: ensTestDomain,
71+
}
72+
const signerInfoPath = path.join(__dirname, 'ens-test-signer.json')
73+
fs.writeFileSync(signerInfoPath, JSON.stringify(ensTestSignerInfo, null, 2))
74+
console.log(`Exported ENS test signer info to '${signerInfoPath}'`)
75+
4476
console.log(`creating subplebbit with address '${signer.address}'...`)
4577
const subplebbit = await plebbit.createSubplebbit({
4678
signer: signer,
@@ -82,7 +114,13 @@ const plebbitDataPath = getTmpFolderPath()
82114
http
83115
.createServer((req, res) => {
84116
res.setHeader('Access-Control-Allow-Origin', '*')
85-
res.end('test server ready')
117+
if (req.url === '/ens-test-signer') {
118+
// Return the ENS test signer info for browser tests
119+
res.setHeader('Content-Type', 'application/json')
120+
res.end(JSON.stringify(ensTestSignerInfo))
121+
} else {
122+
res.end('test server ready')
123+
}
86124
})
87125
.listen(59281)
88126
})

0 commit comments

Comments
 (0)