|
| 1 | +import { expect, test } from 'vitest'; |
| 2 | + |
| 3 | +import { joinLines } from '@agentscript-ai/utils'; |
| 4 | + |
| 5 | +import { createAgent } from '../../agent/createAgent.js'; |
| 6 | +import { parseScript } from '../../parser/parseScript.js'; |
| 7 | +import { executeAgent } from '../executeAgent.js'; |
| 8 | +import { completedFrame, rootFrame } from './testUtils.js'; |
| 9 | + |
| 10 | +test('ternary operator with literals, true', async () => { |
| 11 | + const code = joinLines([ |
| 12 | + // |
| 13 | + 'true ? 1 : 2', |
| 14 | + ]); |
| 15 | + |
| 16 | + const script = parseScript(code); |
| 17 | + |
| 18 | + const agent = createAgent({ |
| 19 | + tools: {}, |
| 20 | + script, |
| 21 | + }); |
| 22 | + |
| 23 | + await executeAgent({ agent }); |
| 24 | + |
| 25 | + const expectedStack = rootFrame({ |
| 26 | + status: 'finished', |
| 27 | + children: [ |
| 28 | + // ternary operator |
| 29 | + completedFrame({ |
| 30 | + trace: '0:0', |
| 31 | + value: 1, |
| 32 | + children: [ |
| 33 | + // condition |
| 34 | + completedFrame({ |
| 35 | + trace: '0:0:0', |
| 36 | + value: true, |
| 37 | + }), |
| 38 | + // then |
| 39 | + completedFrame({ |
| 40 | + trace: '0:0:1', |
| 41 | + value: 1, |
| 42 | + }), |
| 43 | + ], |
| 44 | + }), |
| 45 | + ], |
| 46 | + }); |
| 47 | + |
| 48 | + expect(agent.root).toEqual(expectedStack); |
| 49 | + expect(agent.status).toBe('finished'); |
| 50 | +}); |
| 51 | + |
| 52 | +test('ternary operator with literals, false', async () => { |
| 53 | + const code = joinLines([ |
| 54 | + // |
| 55 | + 'false ? 1 : 2', |
| 56 | + ]); |
| 57 | + |
| 58 | + const script = parseScript(code); |
| 59 | + |
| 60 | + const agent = createAgent({ |
| 61 | + tools: {}, |
| 62 | + script, |
| 63 | + }); |
| 64 | + |
| 65 | + await executeAgent({ agent }); |
| 66 | + |
| 67 | + const expectedStack = rootFrame({ |
| 68 | + status: 'finished', |
| 69 | + children: [ |
| 70 | + // ternary operator |
| 71 | + completedFrame({ |
| 72 | + trace: '0:0', |
| 73 | + value: 2, |
| 74 | + children: [ |
| 75 | + // condition |
| 76 | + completedFrame({ |
| 77 | + trace: '0:0:0', |
| 78 | + value: false, |
| 79 | + }), |
| 80 | + // then |
| 81 | + completedFrame({ |
| 82 | + trace: '0:0:1', |
| 83 | + value: 2, |
| 84 | + }), |
| 85 | + ], |
| 86 | + }), |
| 87 | + ], |
| 88 | + }); |
| 89 | + |
| 90 | + expect(agent.root).toEqual(expectedStack); |
| 91 | + expect(agent.status).toBe('finished'); |
| 92 | +}); |
| 93 | + |
| 94 | +test('ternary operator with literals, undefined', async () => { |
| 95 | + const code = joinLines([ |
| 96 | + // |
| 97 | + 'undefined ? 1 : 2', |
| 98 | + ]); |
| 99 | + |
| 100 | + const script = parseScript(code); |
| 101 | + |
| 102 | + const agent = createAgent({ |
| 103 | + tools: {}, |
| 104 | + script, |
| 105 | + }); |
| 106 | + |
| 107 | + await executeAgent({ agent }); |
| 108 | + |
| 109 | + const expectedStack = rootFrame({ |
| 110 | + status: 'finished', |
| 111 | + children: [ |
| 112 | + // ternary operator |
| 113 | + completedFrame({ |
| 114 | + trace: '0:0', |
| 115 | + value: 2, |
| 116 | + children: [ |
| 117 | + // condition |
| 118 | + completedFrame({ |
| 119 | + trace: '0:0:0', |
| 120 | + value: undefined, |
| 121 | + }), |
| 122 | + // then |
| 123 | + completedFrame({ |
| 124 | + trace: '0:0:1', |
| 125 | + value: 2, |
| 126 | + }), |
| 127 | + ], |
| 128 | + }), |
| 129 | + ], |
| 130 | + }); |
| 131 | + |
| 132 | + expect(agent.root).toEqual(expectedStack); |
| 133 | + expect(agent.status).toBe('finished'); |
| 134 | +}); |
| 135 | + |
| 136 | +test('ternary operator with expressions', async () => { |
| 137 | + const code = joinLines([ |
| 138 | + // |
| 139 | + 'let a = 3', |
| 140 | + 'a < 1 ? 1 : 2', |
| 141 | + ]); |
| 142 | + |
| 143 | + const script = parseScript(code); |
| 144 | + |
| 145 | + const agent = createAgent({ |
| 146 | + tools: {}, |
| 147 | + script, |
| 148 | + }); |
| 149 | + |
| 150 | + await executeAgent({ agent }); |
| 151 | + |
| 152 | + const expectedStack = rootFrame({ |
| 153 | + status: 'finished', |
| 154 | + variables: { |
| 155 | + a: 3, |
| 156 | + }, |
| 157 | + children: [ |
| 158 | + // var a declaration |
| 159 | + completedFrame({ |
| 160 | + trace: '0:0', |
| 161 | + children: [ |
| 162 | + // literal |
| 163 | + completedFrame({ |
| 164 | + trace: '0:0:0', |
| 165 | + value: 3, |
| 166 | + }), |
| 167 | + ], |
| 168 | + }), |
| 169 | + // ternary operator |
| 170 | + completedFrame({ |
| 171 | + trace: '0:1', |
| 172 | + value: 2, |
| 173 | + children: [ |
| 174 | + // condition |
| 175 | + completedFrame({ |
| 176 | + trace: '0:1:0', |
| 177 | + value: false, |
| 178 | + children: [ |
| 179 | + // left operand |
| 180 | + completedFrame({ |
| 181 | + trace: '0:1:0:0', |
| 182 | + value: 3, |
| 183 | + }), |
| 184 | + // right operand |
| 185 | + completedFrame({ |
| 186 | + trace: '0:1:0:1', |
| 187 | + value: 1, |
| 188 | + }), |
| 189 | + ], |
| 190 | + }), |
| 191 | + // else |
| 192 | + completedFrame({ |
| 193 | + trace: '0:1:1', |
| 194 | + value: 2, |
| 195 | + }), |
| 196 | + ], |
| 197 | + }), |
| 198 | + ], |
| 199 | + }); |
| 200 | + |
| 201 | + expect(agent.root).toEqual(expectedStack); |
| 202 | + expect(agent.status).toBe('finished'); |
| 203 | +}); |
0 commit comments