|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { execFileSync } from 'node:child_process'; |
| 3 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { dirname, join } from 'node:path'; |
| 6 | +import { test } from 'node:test'; |
| 7 | +import { fileURLToPath } from 'node:url'; |
| 8 | + |
| 9 | +import { |
| 10 | + type ParsedContent, |
| 11 | + evaluateExpressions, |
| 12 | + generateCppConstants, |
| 13 | + generatePilConstants, |
| 14 | + generateSolidityConstants, |
| 15 | + generateTypescriptConstants, |
| 16 | + parseNoirFile, |
| 17 | +} from './generator.ts'; |
| 18 | + |
| 19 | +const noirFixture = ` |
| 20 | +pub global MAX_FIELD_VALUE: Field = |
| 21 | + 21888242871839275222246405745257275088548364400416034343698204186575808495616; |
| 22 | +pub global MAX_ETH_ADDRESS_VALUE: Field = 0xffffffffffffffffffffffffffffffffffffffff; |
| 23 | +pub global ARCHIVE_HEIGHT: u32 = 30; |
| 24 | +pub global DOM_SEP__MERKLE_HASH: u32 = 2982624097; |
| 25 | +`; |
| 26 | + |
| 27 | +function generateToString(generate: (content: ParsedContent, targetPath: string) => void): string { |
| 28 | + const tempDir = mkdtempSync(join(tmpdir(), 'constants-codegen-')); |
| 29 | + const targetPath = join(tempDir, 'output'); |
| 30 | + try { |
| 31 | + const { constantsExpressions, domainSeparatorEnum } = parseNoirFile(noirFixture); |
| 32 | + generate({ constants: evaluateExpressions(constantsExpressions), domainSeparatorEnum }, targetPath); |
| 33 | + return readFileSync(targetPath, 'utf8'); |
| 34 | + } finally { |
| 35 | + rmSync(tempDir, { recursive: true, force: true }); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +test('generates TypeScript constants and domain separators', () => { |
| 40 | + assert.equal( |
| 41 | + generateToString(generateTypescriptConstants), |
| 42 | + `// GENERATED FILE - DO NOT EDIT, RUN yarn remake-constants |
| 43 | +export const MAX_FIELD_VALUE = 21888242871839275222246405745257275088548364400416034343698204186575808495616n; |
| 44 | +export const MAX_ETH_ADDRESS_VALUE = 1461501637330902918203684832716283019655932542975n; |
| 45 | +export const ARCHIVE_HEIGHT = 30; |
| 46 | +export enum DomainSeparator { |
| 47 | + MERKLE_HASH = 2982624097, |
| 48 | +}`, |
| 49 | + ); |
| 50 | +}); |
| 51 | + |
| 52 | +test('generates the existing C++ subset', () => { |
| 53 | + const output = generateToString(generateCppConstants); |
| 54 | + |
| 55 | + assert.match(output, /#define MAX_ETH_ADDRESS_VALUE "0x0{24}f{40}"/); |
| 56 | + assert.match(output, /#define ARCHIVE_HEIGHT 30/); |
| 57 | + assert.match(output, /#define DOM_SEP__MERKLE_HASH 2982624097UL/); |
| 58 | + assert.doesNotMatch(output, /MAX_FIELD_VALUE/); |
| 59 | +}); |
| 60 | + |
| 61 | +test('generates the existing PIL subset', () => { |
| 62 | + const output = generateToString(generatePilConstants); |
| 63 | + |
| 64 | + assert.match(output, /pol MAX_ETH_ADDRESS_VALUE = 1461501637330902918203684832716283019655932542975;/); |
| 65 | + assert.match(output, /pol DOM_SEP__MERKLE_HASH = 2982624097;/); |
| 66 | + assert.doesNotMatch(output, /ARCHIVE_HEIGHT/); |
| 67 | +}); |
| 68 | + |
| 69 | +test('generates the existing Solidity subset', () => { |
| 70 | + const output = generateToString(generateSolidityConstants); |
| 71 | + |
| 72 | + assert.match( |
| 73 | + output, |
| 74 | + /uint256 internal constant MAX_FIELD_VALUE = 21888242871839275222246405745257275088548364400416034343698204186575808495616;/, |
| 75 | + ); |
| 76 | + assert.doesNotMatch(output, /ARCHIVE_HEIGHT/); |
| 77 | +}); |
| 78 | + |
| 79 | +test('the CLI generates multiple requested outputs', () => { |
| 80 | + const tempDir = mkdtempSync(join(tmpdir(), 'constants-codegen-cli-')); |
| 81 | + const inputPath = join(tempDir, 'constants.nr'); |
| 82 | + const includedInputPath = join(tempDir, 'additional.nr'); |
| 83 | + const typescriptPath = join(tempDir, 'typescript', 'constants.ts'); |
| 84 | + const cppPath = join(tempDir, 'cpp', 'constants.hpp'); |
| 85 | + const cliPath = join(dirname(fileURLToPath(import.meta.url)), 'cli.ts'); |
| 86 | + |
| 87 | + try { |
| 88 | + writeFileSync(inputPath, noirFixture); |
| 89 | + writeFileSync( |
| 90 | + includedInputPath, |
| 91 | + `pub global INCLUDED_CONSTANT: u32 = ARCHIVE_HEIGHT + 1; // selected for export |
| 92 | +pub global EXCLUDED_CONSTANT: u32 = 100; |
| 93 | +`, |
| 94 | + ); |
| 95 | + execFileSync( |
| 96 | + process.execPath, |
| 97 | + [ |
| 98 | + cliPath, |
| 99 | + '--input', |
| 100 | + inputPath, |
| 101 | + '--include', |
| 102 | + `${includedInputPath}:INCLUDED_CONSTANT`, |
| 103 | + '--typescript', |
| 104 | + typescriptPath, |
| 105 | + '--cpp', |
| 106 | + cppPath, |
| 107 | + ], |
| 108 | + { stdio: 'pipe' }, |
| 109 | + ); |
| 110 | + |
| 111 | + assert.match(readFileSync(typescriptPath, 'utf8'), /export const ARCHIVE_HEIGHT = 30;/); |
| 112 | + assert.match(readFileSync(typescriptPath, 'utf8'), /export const INCLUDED_CONSTANT = 31;/); |
| 113 | + assert.doesNotMatch(readFileSync(typescriptPath, 'utf8'), /EXCLUDED_CONSTANT/); |
| 114 | + assert.match(readFileSync(cppPath, 'utf8'), /#define ARCHIVE_HEIGHT 30/); |
| 115 | + } finally { |
| 116 | + rmSync(tempDir, { recursive: true, force: true }); |
| 117 | + } |
| 118 | +}); |
0 commit comments