|
| 1 | +import { assert, expect } from 'chai'; |
| 2 | +import { promises as fs } from 'fs'; |
| 3 | +import { afterEach, beforeEach, describe, it } from 'mocha'; |
| 4 | +import { dirname, join } from 'path'; |
| 5 | +import { fileURLToPath } from 'url'; |
| 6 | + |
| 7 | +import { StarknetArtifactGenerator } from '../scripts/StarknetArtifactGenerator.js'; |
| 8 | +import { CONTRACT_SUFFIXES } from '../src/const.js'; |
| 9 | +import { ContractClass, ContractType } from '../src/types.js'; |
| 10 | + |
| 11 | +import { createMockContractFiles, createMockSierraArtifact } from './utils.js'; |
| 12 | + |
| 13 | +const __filename = fileURLToPath(import.meta.url); |
| 14 | +const __dirname = dirname(__filename); |
| 15 | +const TMP_DIR = join(__dirname, './tmp'); |
| 16 | +const TEST_RELEASE_DIR = join(TMP_DIR, 'release'); |
| 17 | +const TEST_OUTPUT_DIR = join(TMP_DIR, 'dist/artifacts'); |
| 18 | + |
| 19 | +describe('StarknetArtifactGenerator', () => { |
| 20 | + let generator: StarknetArtifactGenerator; |
| 21 | + |
| 22 | + beforeEach(async () => { |
| 23 | + await Promise.all([ |
| 24 | + fs.mkdir(TMP_DIR, { recursive: true }), |
| 25 | + fs.mkdir(TEST_RELEASE_DIR, { recursive: true }), |
| 26 | + fs.mkdir(TEST_OUTPUT_DIR, { recursive: true }), |
| 27 | + ]); |
| 28 | + |
| 29 | + await createMockContractFiles(TEST_RELEASE_DIR); |
| 30 | + |
| 31 | + generator = new StarknetArtifactGenerator( |
| 32 | + TEST_RELEASE_DIR, |
| 33 | + TEST_OUTPUT_DIR, |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(async () => { |
| 38 | + await fs.rm(TMP_DIR, { recursive: true, force: true }).catch(() => {}); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should correctly identify contract types from filenames', () => { |
| 42 | + expect(generator.getContractTypeFromPath('token_MyToken.json')).to.equal( |
| 43 | + ContractType.TOKEN, |
| 44 | + ); |
| 45 | + expect( |
| 46 | + generator.getContractTypeFromPath('mocks_MockContract.json'), |
| 47 | + ).to.equal(ContractType.MOCK); |
| 48 | + expect( |
| 49 | + generator.getContractTypeFromPath('contracts_IMailbox.json'), |
| 50 | + ).to.equal(ContractType.CONTRACT); |
| 51 | + // Defaults to CONTRACT for unknown prefixes |
| 52 | + expect(generator.getContractTypeFromPath('random_name.json')).to.equal( |
| 53 | + ContractType.CONTRACT, |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should create the output directory if it does not exist', async () => { |
| 58 | + await fs.rm(TEST_OUTPUT_DIR, { recursive: true, force: true }); |
| 59 | + await generator.createOutputDirectory(); |
| 60 | + const stats = await fs.stat(TEST_OUTPUT_DIR); |
| 61 | + expect(stats.isDirectory()).to.be.true; |
| 62 | + }); |
| 63 | + |
| 64 | + it('should read and parse a valid Sierra artifact file', async () => { |
| 65 | + const filePath = join( |
| 66 | + TEST_RELEASE_DIR, |
| 67 | + `contracts_Test${CONTRACT_SUFFIXES.SIERRA_JSON}`, |
| 68 | + ); |
| 69 | + const artifact = await generator.readArtifactFile(filePath); |
| 70 | + expect(artifact).to.deep.include({ |
| 71 | + contract_class_version: '0.1.0', |
| 72 | + sierra_program: [], // Based on createMockSierraArtifact |
| 73 | + }); |
| 74 | + expect(artifact.abi).to.be.an('array'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should generate JS content with expected properties for Sierra contracts', () => { |
| 78 | + const artifact = createMockSierraArtifact(); |
| 79 | + const jsContent = generator.generateJavaScriptContent( |
| 80 | + 'TestContract', |
| 81 | + artifact, |
| 82 | + ContractClass.SIERRA, |
| 83 | + ); |
| 84 | + |
| 85 | + const jsonMatch = jsContent.match(/^export const \w+ = (\{.*\});$/s); |
| 86 | + assert(jsonMatch, 'Should find exported JSON object in JS content'); |
| 87 | + |
| 88 | + const parsedArtifact = JSON.parse(jsonMatch[1]); |
| 89 | + expect(parsedArtifact).to.be.an('object'); |
| 90 | + expect(parsedArtifact) |
| 91 | + .to.have.property('sierra_program') |
| 92 | + .that.is.an('array'); |
| 93 | + expect(parsedArtifact) |
| 94 | + .to.have.property('entry_points_by_type') |
| 95 | + .deep.equal(artifact.entry_points_by_type); |
| 96 | + expect(parsedArtifact).to.have.property('abi').that.is.an('array'); |
| 97 | + // Check if a specific known item from the mock ABI exists |
| 98 | + const hasTestFunction = parsedArtifact.abi.some( |
| 99 | + (item: any) => item.name === 'test_function', |
| 100 | + ); |
| 101 | + expect(hasTestFunction, 'ABI should contain "test_function"').to.be.true; |
| 102 | + }); |
| 103 | + |
| 104 | + it('should generate correct TypeScript declaration for Sierra contracts', () => { |
| 105 | + const dtsContent = generator.generateDeclarationContent( |
| 106 | + 'TestSierra', |
| 107 | + true, // isSierra = true |
| 108 | + ); |
| 109 | + expect(dtsContent).to.include( |
| 110 | + 'export declare const TestSierra: CompiledContract', |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should generate correct TypeScript declaration for CASM contracts', () => { |
| 115 | + const dtsContent = generator.generateDeclarationContent( |
| 116 | + 'TestCasm', |
| 117 | + false, // isSierra = false |
| 118 | + ); |
| 119 | + expect(dtsContent).to.include( |
| 120 | + 'export declare const TestCasm: CairoAssembly', |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should process a Sierra artifact file correctly, generating JS and DTS files', async () => { |
| 125 | + const fileName = `contracts_Test${CONTRACT_SUFFIXES.SIERRA_JSON}`; |
| 126 | + const filePath = join(TEST_RELEASE_DIR, fileName); |
| 127 | + const artifact = await generator.readArtifactFile(filePath); // Read expected artifact data |
| 128 | + |
| 129 | + const processResult = await generator.processArtifact(filePath); |
| 130 | + |
| 131 | + // Verify the returned info object |
| 132 | + expect(processResult).to.deep.equal({ |
| 133 | + name: 'contracts_Test', |
| 134 | + contractType: ContractType.CONTRACT, |
| 135 | + contractClass: ContractClass.SIERRA, |
| 136 | + }); |
| 137 | + |
| 138 | + const jsPath = join( |
| 139 | + TEST_OUTPUT_DIR, |
| 140 | + `contracts_Test.${ContractClass.SIERRA}.js`, |
| 141 | + ); |
| 142 | + const dtsPath = join( |
| 143 | + TEST_OUTPUT_DIR, |
| 144 | + `contracts_Test.${ContractClass.SIERRA}.d.ts`, |
| 145 | + ); |
| 146 | + await fs.access(jsPath); // Throws if file doesn't exist |
| 147 | + await fs.access(dtsPath); // Throws if file doesn't exist |
| 148 | + |
| 149 | + const jsContent = await fs.readFile(jsPath, 'utf-8'); |
| 150 | + const jsonMatch = jsContent.match(/^export const \w+ = (\{.*\});$/s); |
| 151 | + assert(jsonMatch, 'Should find JSON object in generated JS file'); |
| 152 | + const parsedJsArtifact = JSON.parse(jsonMatch[1]); |
| 153 | + |
| 154 | + expect(parsedJsArtifact).to.be.an('object'); |
| 155 | + expect(parsedJsArtifact) |
| 156 | + .to.have.property('sierra_program') |
| 157 | + .that.is.an('array'); |
| 158 | + expect(parsedJsArtifact).to.have.property( |
| 159 | + 'contract_class_version', |
| 160 | + artifact.contract_class_version, |
| 161 | + ); |
| 162 | + expect(parsedJsArtifact) |
| 163 | + .to.have.property('entry_points_by_type') |
| 164 | + .deep.equal(artifact.entry_points_by_type); |
| 165 | + expect(parsedJsArtifact) |
| 166 | + .to.have.property('abi') |
| 167 | + .deep.equal( |
| 168 | + typeof artifact.abi === 'string' |
| 169 | + ? JSON.parse(artifact.abi) |
| 170 | + : artifact.abi, |
| 171 | + ); |
| 172 | + |
| 173 | + const dtsContent = await fs.readFile(dtsPath, 'utf-8'); |
| 174 | + expect(dtsContent).to.include( |
| 175 | + 'export declare const contracts_Test: CompiledContract', |
| 176 | + ); |
| 177 | + }); |
| 178 | + |
| 179 | + it('should process a standard set of artifacts, generate index files, and return correct summary', async () => { |
| 180 | + // Assumes beforeEach creates: contracts_Test, token_HypERC20, mocks_MockContract (all Sierra) |
| 181 | + const processedMap = await generator.generate(); |
| 182 | + |
| 183 | + expect(processedMap.size).to.equal(3); |
| 184 | + expect(processedMap.get('contracts_Test')).to.deep.equal({ |
| 185 | + type: ContractType.CONTRACT, |
| 186 | + sierra: true, |
| 187 | + casm: false, |
| 188 | + }); |
| 189 | + expect(processedMap.get('token_HypERC20')).to.deep.equal({ |
| 190 | + type: ContractType.TOKEN, |
| 191 | + sierra: true, |
| 192 | + casm: false, |
| 193 | + }); |
| 194 | + expect(processedMap.get('mocks_MockContract')).to.deep.equal({ |
| 195 | + type: ContractType.MOCK, |
| 196 | + sierra: true, |
| 197 | + casm: false, |
| 198 | + }); |
| 199 | + |
| 200 | + const indexJsPath = join(TEST_OUTPUT_DIR, 'index.js'); |
| 201 | + const indexDtsPath = join(TEST_OUTPUT_DIR, 'index.d.ts'); |
| 202 | + await fs.access(indexJsPath); |
| 203 | + await fs.access(indexDtsPath); |
| 204 | + |
| 205 | + // Check index file content (verify artifact paths are included) |
| 206 | + const indexJsContent = await fs.readFile(indexJsPath, 'utf-8'); |
| 207 | + |
| 208 | + expect(indexJsContent).to.include( |
| 209 | + `./contracts_Test.${ContractClass.SIERRA}.js`, |
| 210 | + ); |
| 211 | + expect(indexJsContent).to.include( |
| 212 | + `./token_HypERC20.${ContractClass.SIERRA}.js`, |
| 213 | + ); |
| 214 | + expect(indexJsContent).to.include( |
| 215 | + `./mocks_MockContract.${ContractClass.SIERRA}.js`, |
| 216 | + ); |
| 217 | + |
| 218 | + // Check artifact file existence (spot check one) |
| 219 | + const testSierraJsPath = join( |
| 220 | + TEST_OUTPUT_DIR, |
| 221 | + `contracts_Test.${ContractClass.SIERRA}.js`, |
| 222 | + ); |
| 223 | + await fs.access(testSierraJsPath); |
| 224 | + }); |
| 225 | + |
| 226 | + it('should handle malformed artifact files gracefully during generation', async () => { |
| 227 | + const malformedFilePath = join( |
| 228 | + TEST_RELEASE_DIR, |
| 229 | + `malformed${CONTRACT_SUFFIXES.SIERRA_JSON}`, |
| 230 | + ); |
| 231 | + await fs.writeFile(malformedFilePath, '{ this is not valid JSON }'); |
| 232 | + |
| 233 | + let errorThrown = false; |
| 234 | + try { |
| 235 | + await generator.generate(); // This should throw an error |
| 236 | + } catch (error) { |
| 237 | + errorThrown = true; |
| 238 | + expect(error).to.be.instanceOf(Error); |
| 239 | + } |
| 240 | + // Expect generate() to throw when encountering invalid JSON |
| 241 | + expect(errorThrown, 'Generator should throw an error for malformed JSON').to |
| 242 | + .be.true; |
| 243 | + }); |
| 244 | + |
| 245 | + it('should correctly process artifacts with unusual filenames during generation', async () => { |
| 246 | + // Add a file with an unusual name that still fits the expected suffix pattern |
| 247 | + const oddNamedFilePath = join( |
| 248 | + TEST_RELEASE_DIR, |
| 249 | + `unusual_prefix.contract_class.json`, // Uses SIERRA_JSON suffix |
| 250 | + ); |
| 251 | + await fs.writeFile( |
| 252 | + oddNamedFilePath, |
| 253 | + JSON.stringify(createMockSierraArtifact()), // Use valid content |
| 254 | + ); |
| 255 | + |
| 256 | + const processedMap = await generator.generate(); |
| 257 | + |
| 258 | + // Expect 4 total artifacts now (3 standard + 1 unusual) |
| 259 | + expect(processedMap.size).to.equal(4); |
| 260 | + |
| 261 | + expect(processedMap.has('unusual_prefix')).to.be.true; |
| 262 | + const fileInfo = processedMap.get('unusual_prefix'); |
| 263 | + expect(fileInfo).to.deep.equal({ |
| 264 | + type: ContractType.CONTRACT, // Default type for unknown prefix |
| 265 | + sierra: true, |
| 266 | + casm: false, |
| 267 | + }); |
| 268 | + |
| 269 | + const jsPath = join( |
| 270 | + TEST_OUTPUT_DIR, |
| 271 | + `unusual_prefix.${ContractClass.SIERRA}.js`, |
| 272 | + ); |
| 273 | + const dtsPath = join( |
| 274 | + TEST_OUTPUT_DIR, |
| 275 | + `unusual_prefix.${ContractClass.SIERRA}.d.ts`, |
| 276 | + ); |
| 277 | + await fs.access(jsPath); |
| 278 | + await fs.access(dtsPath); |
| 279 | + |
| 280 | + const indexJsPath = join(TEST_OUTPUT_DIR, 'index.js'); |
| 281 | + const indexJsContent = await fs.readFile(indexJsPath, 'utf-8'); |
| 282 | + expect(indexJsContent).to.include( |
| 283 | + `./unusual_prefix.${ContractClass.SIERRA}.js`, // Check path inclusion |
| 284 | + ); |
| 285 | + }); |
| 286 | +}); |
0 commit comments