|
| 1 | +import { readFileSync } from 'node:fs' |
| 2 | +import { describe, expect, it } from 'vitest' |
| 3 | + |
| 4 | +const read = (path: string) => readFileSync(path, 'utf8') |
| 5 | + |
| 6 | +const snippetPath = 'src/snippets/network-identities.txt' |
| 7 | +const networkUpgradesPath = 'src/pages/docs/guide/node/network-upgrades.mdx' |
| 8 | +const troubleshootingPath = 'src/pages/docs/guide/node/validator-troubleshooting.mdx' |
| 9 | + |
| 10 | +const networks = [ |
| 11 | + { name: 'mainnet', tab: 'Mainnet' }, |
| 12 | + { name: 'testnet', tab: 'Testnet' }, |
| 13 | +] as const |
| 14 | + |
| 15 | +function region(source: string, name: string) { |
| 16 | + const match = source.match( |
| 17 | + new RegExp(`// \\[!region ${name}\\]\\n([\\s\\S]*?)\\n// \\[!endregion ${name}\\]`), |
| 18 | + ) |
| 19 | + expect(match, `missing region ${name}`).toBeTruthy() |
| 20 | + return match?.[1] ?? '' |
| 21 | +} |
| 22 | + |
| 23 | +describe('network identities', () => { |
| 24 | + const snippet = read(snippetPath) |
| 25 | + const networkUpgrades = read(networkUpgradesPath) |
| 26 | + const troubleshooting = read(troubleshootingPath) |
| 27 | + |
| 28 | + for (const { name, tab } of networks) { |
| 29 | + it(`keeps the ${name} identity, override command, and page epoch in sync`, () => { |
| 30 | + const identity = region(snippet, `${name}-identity`) |
| 31 | + const override = region(snippet, `${name}-override`) |
| 32 | + |
| 33 | + expect(identity).toMatch(/^0x[0-9a-f]{192}$/) |
| 34 | + expect(override).toContain(`--consensus.network-identity ${identity} \\`) |
| 35 | + |
| 36 | + const epoch = override.match(/--consensus\.network-identity-from-epoch (\d+)$/)?.[1] |
| 37 | + expect(epoch, `missing ${name} from-epoch`).toBeTruthy() |
| 38 | + |
| 39 | + const tabSource = networkUpgrades.match( |
| 40 | + new RegExp(`<Tab title="${tab}">([\\s\\S]*?)</Tab>`), |
| 41 | + )?.[1] |
| 42 | + expect(tabSource, `missing ${tab} tab`).toBeTruthy() |
| 43 | + expect(tabSource).toContain(`**Network identity, from epoch ${epoch}:**`) |
| 44 | + expect(tabSource).toContain( |
| 45 | + `// [!include ~/snippets/network-identities.txt:${name}-identity]`, |
| 46 | + ) |
| 47 | + expect(troubleshooting).toContain( |
| 48 | + `// [!include ~/snippets/network-identities.txt:${name}-override]`, |
| 49 | + ) |
| 50 | + }) |
| 51 | + } |
| 52 | + |
| 53 | + it('does not hard-code identities outside the shared snippet', () => { |
| 54 | + for (const source of [networkUpgrades, troubleshooting]) { |
| 55 | + expect(source).not.toMatch(/0x[0-9a-f]{192}/) |
| 56 | + } |
| 57 | + }) |
| 58 | +}) |
0 commit comments