Skip to content

Commit 45203c2

Browse files
committed
refactor: hide internal members from consumers of testing package
1 parent e9abc14 commit 45203c2

File tree

6 files changed

+31
-23
lines changed

6 files changed

+31
-23
lines changed

examples/voting/contract.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Bytes, Uint64 } from '@algorandfoundation/algorand-typescript'
2-
import { bytesToUint8Array, TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing'
2+
import { encodingUtil, TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing'
33
import { DynamicArray, UintN8 } from '@algorandfoundation/algorand-typescript/arc4'
44
import nacl from 'tweetnacl'
55
import { afterEach, describe, expect, it } from 'vitest'
@@ -50,7 +50,7 @@ describe('VotingRoundApp', () => {
5050
contract.bootstrap(ctx.any.txn.payment({ receiver: app.address, amount: boostrapMinBalanceReq }))
5151

5252
const account = ctx.any.account()
53-
const signature = nacl.sign.detached(bytesToUint8Array(account.bytes), keyPair.secretKey)
53+
const signature = nacl.sign.detached(encodingUtil.toExternalValue(account.bytes), keyPair.secretKey)
5454
ctx.txn.createScope([ctx.any.txn.applicationCall({ sender: account })]).execute(() => {
5555
const preconditions = contract.getPreconditions(Bytes(signature))
5656

@@ -67,7 +67,7 @@ describe('VotingRoundApp', () => {
6767
contract.bootstrap(ctx.any.txn.payment({ receiver: app.address, amount: boostrapMinBalanceReq }))
6868

6969
const account = ctx.any.account()
70-
const signature = nacl.sign.detached(bytesToUint8Array(account.bytes), keyPair.secretKey)
70+
const signature = nacl.sign.detached(encodingUtil.toExternalValue(account.bytes), keyPair.secretKey)
7171
const answerIds = new DynamicArray<UintN8>(
7272
...Array(13)
7373
.fill(0)

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
import { internal } from '@algorandfoundation/algorand-typescript'
2+
13
export { TestExecutionContext } from './test-execution-context'
2-
export { asUint8Array as bytesToUint8Array } from './util'
4+
export const encodingUtil = {
5+
...internal.encodingUtil,
6+
toExternalValue: internal.primitives.toExternalValue,
7+
}

src/subcontexts/ledger-context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class LedgerContext {
2323
blocks = new Uint64Map<BlockData>()
2424
globalData = new GlobalData()
2525

26+
/* @internal */
2627
addAppIdContractMap(appId: internal.primitives.StubUint64Compat, contract: BaseContract): void {
2728
this.appIdContractMap.set(appId, contract)
2829
}

src/subcontexts/transaction-context.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { bytes, Contract, internal, TransactionType, uint64 } from '@algorandfoundation/algorand-typescript'
22
import { AbiMetadata, getContractMethodAbiMetadata } from '../abi-metadata'
33
import { TRANSACTION_GROUP_MAX_SIZE } from '../constants'
4+
import { checkRoutingConditions } from '../context-helpers/context-util'
45
import { lazyContext } from '../context-helpers/internal-context'
56
import { DecodedLogs, decodeLogs, LogDecoding } from '../decode-logs'
67
import { testInvariant } from '../errors'
@@ -49,23 +50,6 @@ interface ExecutionScope {
4950
execute: <TReturn>(body: () => TReturn) => TReturn
5051
}
5152

52-
export const checkRoutingConditions = (appId: uint64, metadata: AbiMetadata) => {
53-
const appData = lazyContext.getApplicationData(appId)
54-
const isCreating = appData.isCreating
55-
if (isCreating && metadata.onCreate === 'disallow') {
56-
throw new internal.errors.CodeError('method can not be called while creating')
57-
}
58-
if (!isCreating && metadata.onCreate === 'require') {
59-
throw new internal.errors.CodeError('method can only be called while creating')
60-
}
61-
const txn = lazyContext.activeGroup.activeTransaction
62-
if (txn instanceof ApplicationTransaction && metadata.allowActions && !metadata.allowActions.includes(txn.onCompletion)) {
63-
throw new internal.errors.CodeError(
64-
`method can only be called with one of the following on_completion values: ${metadata.allowActions.join(', ')}`,
65-
)
66-
}
67-
}
68-
6953
export class DeferredAppCall<TParams extends unknown[], TReturn> {
7054
constructor(
7155
private readonly appId: uint64,
@@ -106,6 +90,7 @@ export class TransactionContext {
10690
}
10791
}
10892

93+
/* internal */
10994
ensureScope(group: Transaction[], activeTransactionIndex?: number): ExecutionScope {
11095
if (!this.#activeGroup || !this.#activeGroup.transactions.length) {
11196
return this.createScope(group, activeTransactionIndex)
@@ -200,6 +185,7 @@ export class TransactionGroup {
200185
return this.activeTransaction.appId.id
201186
}
202187

188+
/* internal */
203189
get constructingItxn() {
204190
if (!this.constructingItxnGroup.length) {
205191
internal.errors.internalError('itxn field without itxn begin')
@@ -221,9 +207,12 @@ export class TransactionGroup {
221207
Object.assign(activeTransaction, filteredFields)
222208
}
223209

210+
/* internal */
224211
addInnerTransactionGroup(...itxns: InnerTxn[]) {
225212
this.itxnGroups.push(new ItxnGroup(itxns))
226213
}
214+
215+
/* internal */
227216
beginInnerTransactionGroup() {
228217
if (this.constructingItxnGroup.length) {
229218
internal.errors.internalError('itxn begin without itxn submit')
@@ -235,13 +224,15 @@ export class TransactionGroup {
235224
this.constructingItxnGroup.push({} as InnerTxnFields)
236225
}
237226

227+
/* internal */
238228
appendInnerTransactionGroup() {
239229
if (!this.constructingItxnGroup.length) {
240230
internal.errors.internalError('itxn next without itxn begin')
241231
}
242232
this.constructingItxnGroup.push({ type: TransactionType.Payment } as InnerTxnFields)
243233
}
244234

235+
/* internal */
245236
submitInnerTransactionGroup() {
246237
if (!this.constructingItxnGroup.length) {
247238
internal.errors.internalError('itxn submit without itxn begin')

src/test-execution-context.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,32 @@ export class TestExecutionContext implements internal.ExecutionContext {
4444
this.#activeLogicSigArgs = []
4545
}
4646

47+
/* @internal */
4748
account(address?: bytes): Account {
4849
return new AccountCls(address)
4950
}
5051

52+
/* @internal */
5153
application(id?: uint64): Application {
5254
return new ApplicationCls(id)
5355
}
5456

57+
/* @internal */
5558
asset(id?: uint64): Asset {
5659
return new AssetCls(id)
5760
}
5861

62+
/* @internal */
5963
log(value: bytes): void {
6064
this.txn.appendLog(value)
6165
}
6266

67+
/* @internal */
6368
exportLogs<const T extends [...LogDecoding[]]>(appId: uint64, ...decoding: T): DecodedLogs<T> {
6469
return this.txn.exportLogs(appId, ...decoding)
6570
}
6671

72+
/* @internal */
6773
get op() {
6874
return ops
6975
}
@@ -87,12 +93,14 @@ export class TestExecutionContext implements internal.ExecutionContext {
8793
return this.#defaultSender
8894
}
8995

96+
/* @internal */
9097
get abiMetadata() {
9198
return {
9299
captureMethodConfig,
93100
}
94101
}
95102

103+
/* @internal */
96104
get gtxn() {
97105
return {
98106
Transaction: (index: uint64) => this.txn.activeGroup.getTransaction(index),
@@ -105,6 +113,7 @@ export class TestExecutionContext implements internal.ExecutionContext {
105113
}
106114
}
107115

116+
/* @internal */
108117
get itxn() {
109118
return {
110119
submitGroup: itxnSubmitGroup,
@@ -117,6 +126,7 @@ export class TestExecutionContext implements internal.ExecutionContext {
117126
}
118127
}
119128

129+
/* @internal */
120130
get state() {
121131
return {
122132
GlobalState,

tsconfig.build.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"compilerOptions": {
44
"outDir": "./dist/",
55
"noEmit": false,
6-
"declaration": true
6+
"declaration": true,
7+
"stripInternal": true
78
},
89
"include": ["src/**/*.ts"],
9-
"exclude": ["src/**/*.spec.ts", "src/**/*.test.ts", "src/**/tests/**", "examples/**/*.ts",]
10+
"exclude": ["src/**/*.spec.ts", "src/**/*.test.ts", "src/**/tests/**", "examples/**/*.ts"]
1011
}

0 commit comments

Comments
 (0)