|
| 1 | +import { afterEach, describe, expect, test } from 'vitest' |
| 2 | +import { decodeLogs } from '../src/decode-logs' |
| 3 | +import { TestExecutionContext } from '../src/test-execution-context' |
| 4 | +import { LoggedErrorsContract } from './artifacts/logged-errors/contract.algo' |
| 5 | + |
| 6 | +describe('logged errors', async () => { |
| 7 | + const ctx = new TestExecutionContext() |
| 8 | + |
| 9 | + afterEach(() => { |
| 10 | + ctx.reset() |
| 11 | + }) |
| 12 | + |
| 13 | + test.for([ |
| 14 | + { arg: 1, expectedError: 'ERR:01' }, |
| 15 | + { arg: 2, expectedError: 'ERR:02' }, |
| 16 | + { arg: 3, expectedError: 'ERR:03:arg is 3' }, |
| 17 | + { arg: 4, expectedError: 'AER:04' }, |
| 18 | + { arg: 5, expectedError: 'AER:05:arg is 5' }, |
| 19 | + { arg: 6, expectedError: 'ERR:06:arg is 6' }, |
| 20 | + { arg: 7, expectedError: 'ERR:07' }, |
| 21 | + { arg: 8, expectedError: 'ERR:08' }, |
| 22 | + { arg: 9, expectedError: 'ERR:09:arg is 9' }, |
| 23 | + { arg: 10, expectedError: 'AER:10' }, |
| 24 | + { arg: 11, expectedError: 'AER:11:arg is 11' }, |
| 25 | + { arg: 12, expectedError: 'ERR:12:arg is 12' }, |
| 26 | + ])('should log correct error for arg $arg', ({ arg, expectedError }) => { |
| 27 | + const contract = ctx.contract.create(LoggedErrorsContract) |
| 28 | + expect(() => contract.testValid(arg)).toThrow(expectedError) |
| 29 | + assertLog(expectedError) |
| 30 | + }) |
| 31 | + |
| 32 | + test.for([ |
| 33 | + { arg: 1, expectedError: 'ERR:not-alnum!' }, |
| 34 | + { arg: 2, expectedError: 'ERR:not-alnum!' }, |
| 35 | + ])('should log error with non alphanumeric code', ({ arg, expectedError }) => { |
| 36 | + const contract = ctx.contract.create(LoggedErrorsContract) |
| 37 | + expect(() => contract.testInvalidCode(arg)).toThrow(expectedError) |
| 38 | + assertLog(expectedError) |
| 39 | + }) |
| 40 | + |
| 41 | + test.for([ |
| 42 | + { arg: 1, expectedError: 'ERR:MyCode' }, |
| 43 | + { arg: 2, expectedError: 'ERR:MyCode' }, |
| 44 | + ])('should log error with camel case code', ({ arg, expectedError }) => { |
| 45 | + const contract = ctx.contract.create(LoggedErrorsContract) |
| 46 | + expect(() => contract.testCamelCaseCode(arg)).toThrow(expectedError) |
| 47 | + assertLog(expectedError) |
| 48 | + }) |
| 49 | + |
| 50 | + test.for([ |
| 51 | + { arg: 1, expectedError: 'AER:01' }, |
| 52 | + { arg: 2, expectedError: 'AER:01' }, |
| 53 | + ])('should log error with AER prefix', ({ arg, expectedError }) => { |
| 54 | + const contract = ctx.contract.create(LoggedErrorsContract) |
| 55 | + expect(() => contract.testAERPrefix(arg)).toThrow(expectedError) |
| 56 | + assertLog(expectedError) |
| 57 | + }) |
| 58 | + |
| 59 | + test.for([ |
| 60 | + { |
| 61 | + arg: 1, |
| 62 | + expectedError: 'ERR:01:I will now provide a succint description of the error. I guess it all started when I was 5...', |
| 63 | + }, |
| 64 | + { |
| 65 | + arg: 2, |
| 66 | + expectedError: 'ERR:01:I will now provide a succint description of the error. I guess it all started when I was 5...', |
| 67 | + }, |
| 68 | + ])('should log error with long message', ({ arg, expectedError }) => { |
| 69 | + const contract = ctx.contract.create(LoggedErrorsContract) |
| 70 | + expect(() => contract.testLongMessage(arg)).toThrow(expectedError) |
| 71 | + assertLog(expectedError) |
| 72 | + }) |
| 73 | + |
| 74 | + test.for([ |
| 75 | + { arg: 1, expectedError: 'ERR:abcd' }, |
| 76 | + { arg: 2, expectedError: 'ERR:abcd' }, |
| 77 | + ])('should log error with 8 byte message', ({ arg, expectedError }) => { |
| 78 | + const contract = ctx.contract.create(LoggedErrorsContract) |
| 79 | + expect(() => contract.test8ByteMessage(arg)).toThrow(expectedError) |
| 80 | + assertLog(expectedError) |
| 81 | + }) |
| 82 | + |
| 83 | + test.for([ |
| 84 | + { |
| 85 | + arg: 1, |
| 86 | + expectedError: 'ERR:01:aaaaaaaaaaaaaaaaaaaaaaaaa', |
| 87 | + }, |
| 88 | + { |
| 89 | + arg: 2, |
| 90 | + expectedError: 'ERR:01:aaaaaaaaaaaaaaaaaaaaaaaaa', |
| 91 | + }, |
| 92 | + ])('should log error with 32 byte message', ({ arg, expectedError }) => { |
| 93 | + const contract = ctx.contract.create(LoggedErrorsContract) |
| 94 | + expect(() => contract.test32ByteMessage(arg)).toThrow(expectedError) |
| 95 | + assertLog(expectedError) |
| 96 | + }) |
| 97 | + |
| 98 | + test('should throw error when code contains colon', () => { |
| 99 | + const expectedError = "error code must not contain domain separator ':'" |
| 100 | + const contract = ctx.contract.create(LoggedErrorsContract) |
| 101 | + expect(() => contract.testColonInCode(1)).toThrow(expectedError) |
| 102 | + }) |
| 103 | + |
| 104 | + test('should throw error when message contains colon', () => { |
| 105 | + const expectedError = "error message must not contain domain separator ':'" |
| 106 | + const contract = ctx.contract.create(LoggedErrorsContract) |
| 107 | + expect(() => contract.testColonInMessage(1)).toThrow(expectedError) |
| 108 | + }) |
| 109 | + |
| 110 | + test('should throw error when prefix is invalid', () => { |
| 111 | + const expectedError = 'error prefix must be one of AER, ERR' |
| 112 | + const contract = ctx.contract.create(LoggedErrorsContract) |
| 113 | + expect(() => contract.testInvalidPrefix(1)).toThrow(expectedError) |
| 114 | + }) |
| 115 | + |
| 116 | + function assertLog(expectedError: string) { |
| 117 | + const appLogs = ctx.txn.activeGroup.getApplicationCallTransaction().appLogs |
| 118 | + const [log] = decodeLogs(appLogs, ['s']) |
| 119 | + expect(log).toEqual(expectedError) |
| 120 | + } |
| 121 | +}) |
0 commit comments