Skip to content

Commit

Permalink
test: Add test fixture for running Algorand TypeScript code on chain
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanmenzel committed Oct 24, 2024
1 parent 0b45c3b commit 32ac667
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 6 deletions.
1 change: 0 additions & 1 deletion examples/voting/contract.algo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const BOX_BYTE_MIN_BALANCE: uint64 = 400
// The min balance increase for each asset opted into
const ASSET_MIN_BALANCE: uint64 = 100000

// TODO: ObjectPType should hopefully respect this ordering of properties
type VotingPreconditions = {
is_voting_open: uint64
is_allowed_to_vote: uint64
Expand Down
187 changes: 187 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dev:calculator": "tsx src/cli.ts build examples/calculator/contract.algo.ts",
"dev:approvals": "tsx src/cli.ts build tests/approvals --output-awst --output-awst-json --no-output-teal --output-ssa-ir --out-dir out/[name]",
"dev:expected-output": "tsx src/cli.ts build tests/expected-output --dry-run",
"dev:testing": "tsx src/cli.ts build tests/approvals/destructuring-iterators.algo.ts --output-awst --output-awst-json --log-level=info --log-level debug",
"dev:testing": "tsx src/cli.ts build tests/expected-output/cant-create.algo.ts --output-awst --output-awst-json --log-level=info --log-level debug",
"audit": "better-npm-audit audit",
"format": "prettier --write .",
"lint": "eslint \"src/**/*.ts\"",
Expand Down Expand Up @@ -45,6 +45,7 @@
"author": "Algorand foundation",
"license": "MIT",
"devDependencies": {
"@algorandfoundation/algokit-utils": "^7.0.0-beta.7",
"@algorandfoundation/algorand-typescript": "file:packages/algo-ts/dist",
"@commitlint/cli": "^19.5.0",
"@commitlint/config-conventional": "^19.5.0",
Expand Down
35 changes: 31 additions & 4 deletions src/util/generate-temp-file.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { randomUUID } from 'crypto'
import fs from 'fs'
import { globIterateSync } from 'glob'
import type { WriteFileOptions } from 'node:fs'
import path from 'node:path'
import os from 'os'
import upath from 'upath'
import { mkDirIfNotExists } from './index'

export type TempFile = {
writeFileSync(data: NodeJS.ArrayBufferView, options?: WriteFileOptions): void
Expand All @@ -11,14 +13,14 @@ export type TempFile = {
} & Disposable

function ensureTempDir(): string {
const tempDir = path.join(os.tmpdir(), 'puya-ts')
if (!fs.existsSync(tempDir)) fs.mkdirSync(tempDir)
const tempDir = upath.join(os.tmpdir(), 'puya-ts')
mkDirIfNotExists(tempDir)
return tempDir
}

export function generateTempFile(options?: { ext?: string }): TempFile {
const { ext = 'tmp' } = options ?? {}
const filePath = path.join(ensureTempDir(), `${randomUUID()}.${ext}`)
const filePath = upath.join(ensureTempDir(), `${randomUUID()}.${ext}`)

return {
get filePath() {
Expand All @@ -32,3 +34,28 @@ export function generateTempFile(options?: { ext?: string }): TempFile {
},
}
}
export type TempDir = {
readonly dirPath: string
files(): IterableIterator<string>
} & Disposable

export function generateTempDir(): TempDir {
const dirPath = upath.join(ensureTempDir(), `${randomUUID()}`)
mkDirIfNotExists(dirPath)

return {
get dirPath() {
return dirPath
},
*files(): IterableIterator<string> {
for (const p of globIterateSync(upath.join(dirPath, '**'), {
nodir: true,
})) {
yield p
}
},
[Symbol.dispose]() {
fs.rmSync(dirPath, { recursive: true, force: true })
},
}
}
15 changes: 15 additions & 0 deletions tests/onchain/abi-decorators.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { describe } from 'vitest'
import { createTestFixture } from './util/test-fixture'

describe('abi-decorators', () => {
const test = createTestFixture('tests/approvals/abi-decorators.algo.ts', { createParams: { method: 'createMethod' } })
test('can be created', async ({ appFactory }) => {
await appFactory.send.create({ method: 'createMethod' })
})
test('methods can be called', async ({ appClient, expect }) => {
await appClient.send.call({ method: 'justNoop' })
await appClient.send.call({ method: 'allActions', onComplete: 1 })
const { return: returnValue } = await appClient.send.call({ method: 'overrideReadonlyName' })
expect(returnValue).toBe(5n)
})
})
Loading

0 comments on commit 32ac667

Please sign in to comment.