SPB Git

Repository Public License

ExpressionTests.swift 30.3 KB 435 lines swift

1// ORIX OS Automation2// File: Packages/ORIXWorkflowEngine/Tests/ORIXWorkflowEngineTests/ExpressionTests.swift3// Description: Unit tests of the expression language: operators, every function, scoping, templates, single-pass interpolation, rejected syntax and bounds.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9import ORIXCore10@testable import ORIXWorkflowEngine1112private let hostile = "${{ secret(\"x\") }} then ${{ env.HOME }} and ${{ 1 + 1 }}"1314private func context() -> ExpressionContext {15    ExpressionContext(16        params: ["name": "Ada", "n": 3, "list": [1, 2, 3], "folder": "~/x"],17        vars: ["count": 2, "obj": ["a": 1, "b": [10, 20]], "text": "Hello World", "empty": []],18        steps: [19            "fetch": ["status": "succeeded", "error": nil,20                      "output": ["items": [["name": "a", "size": 1], ["name": "b", "size": 5]], "text": .string(hostile)]],21            "step-one": ["status": "failed", "output": ["n": 7], "error": "boom"],22        ],23        locals: ["item": ["name": "it"], "index": 4],24        environment: ["HOME": "/Users/t", "USER": "t", "TMPDIR": "/tmp/", "DATE": "2027-01-15", "TIME": "08:00:00",25                      "DATETIME": "2027-01-15T08:00:00Z", "TIMESTAMP": "1800000000", "PATH": "/usr/bin", "SECRET_TOKEN": "nope"],26        now: Date(timeIntervalSince1970: 1_800_000_000), timeZone: TimeZone(identifier: "UTC")!)27}2829private func ev(_ source: String) throws -> JSONValue { try WorkflowExpression.evaluate(source, context: context()) }3031private func failure(_ source: String, template: Bool = false) -> WorkflowExpressionError? {32    do {33        _ = template ? try WorkflowExpression.interpolate(source, context: context()) : try WorkflowExpression.evaluate(source, context: context())34        return nil35    } catch let e as WorkflowExpressionError { return e } catch { return WorkflowExpressionError(message: "\(error)") }36}3738private func expectFailure(_ source: String, _ fragment: String, sourceLocation: SourceLocation = #_sourceLocation) {39    let e = failure(source)40    #expect(e != nil, "\(source) should fail", sourceLocation: sourceLocation)41    #expect(e?.message.contains(fragment) == true, "\(source): got \"\(e?.message ?? "no error")\", expected to contain \"\(fragment)\"", sourceLocation: sourceLocation)42}4344@Suite("Expression language: literals and operators (unit)") struct ExpressionOperatorTests {45    @Test func literals() throws {46        #expect(try ev("42") == .int(42) && ev("1.5") == .number(1.5) && ev("2e3") == .number(2000) && ev("1.5e-1") == .number(0.15))47        #expect(try ev("true") == true && ev("false") == false && ev("null") == .null)48        #expect(try ev(#"'it\'s'"#) == "it's" && ev(#""a\nb\t\"q\" \\ é""#) == "a\nb\t\"q\" \\ é")49        #expect(try ev("[1, 'a', [true], null]") == [1, "a", [true], nil] && ev("[]") == [])50        #expect(try ev("  1  ") == 1)51        expectFailure("", "empty expression")52        expectFailure("'open", "unterminated string")53        expectFailure(#"'bad \q'"#, "unknown escape")54    }5556    @Test func arithmeticKeepsIntegersWhenExact() throws {57        #expect(try ev("1 + 2") == .int(3) && ev("5 - 7") == .int(-2) && ev("4 * 3") == .int(12))58        #expect(try ev("6 / 2") == .int(3) && ev("7 / 2") == .number(3.5) && ev("7 % 3") == .int(1) && ev("-7 % 3") == .int(-1))59        #expect(try ev("1.5 + 1") == .number(2.5) && ev("3 * 0.5") == .number(1.5) && ev("5.5 % 2") == .number(1.5) && ev("1 / 4.0") == .number(0.25))60        #expect(try ev("-3") == .int(-3) && ev("-(1 + 2)") == .int(-3) && ev("-1.5") == .number(-1.5) && ev("1 - -1") == .int(2) && ev("--2") == .int(2))61    }6263    @Test func integerOverflowFallsBackToFloatingPoint() throws {64        #expect(try ev("9223372036854775807 + 1") == .number(9.223372036854775807e18))65        #expect(try ev("9223372036854775807 * 2") == .number(1.8446744073709552e19))66        #expect(try ev("-9223372036854775807 - 2") == .number(-9.223372036854775807e18))67        #expect(try ev("99999999999999999999") == .number(1e20))68        #expect(try ev("(-9223372036854775807 - 1) % -1") == .int(0))69        #expect(try ev("-(-9223372036854775807 - 1)") == .number(9.223372036854775808e18))70    }7172    @Test func divisionByZeroIsAnError() {73        expectFailure("1 / 0", "division by zero")74        expectFailure("1 % 0", "division by zero")75        expectFailure("1.5 / 0", "division by zero")76        expectFailure("1 % 0.0", "division by zero")77    }7879    @Test func precedenceAndAssociativity() throws {80        #expect(try ev("1 + 2 * 3") == 7 && ev("(1 + 2) * 3") == 9 && ev("10 - 2 - 3") == 5 && ev("2 * 3 % 4") == 2 && ev("-2 * 3") == -6)81        #expect(try ev("1 + 1 == 2") == true && ev("1 < 2 == true") == true && ev("!false == true") == true)82        #expect(try ev("true || false && false") == true && ev("(true || false) && false") == false)83        #expect(try ev("1 + 2 > 2 && 2 * 2 <= 4") == true)84    }8586    @Test func comparisons() throws {87        #expect(try ev("1 < 2") == true && ev("2 <= 2") == true && ev("3 > 4") == false && ev("4 >= 5") == false && ev("1 < 1.5") == true)88        #expect(try ev("'a' < 'b'") == true && ev("'b' >= 'b'") == true && ev("'abc' > 'abd'") == false)89        #expect(try ev("1 == 1.0") == true && ev("1 != 2") == true && ev("'1' == 1") == false && ev("null == null") == true)90        #expect(try ev("[1, 2] == [1, 2]") == true && ev("vars.obj == vars.obj") == true && ev("true != false") == true && ev("null != 0") == true)91        expectFailure("1 < 'a'", "cannot compare")92        expectFailure("null > 1", "cannot compare")93        expectFailure("[1] < [2]", "cannot compare")94    }9596    @Test func logicalOperatorsShortCircuitAndYieldBooleans() throws {97        #expect(try ev("false && (1 / 0)") == false && ev("true || (1 / 0)") == true)98        #expect(try ev("0 && nope()") == false && ev("'x' || nope()") == true)99        #expect(try ev("1 && 'x'") == true && ev("0 || ''") == false && ev("null || [0]") == true)100        #expect(try ev("!0") == true && ev("!!'x'") == true && ev("![]") == true && ev("!vars.obj") == false)101        expectFailure("true && (1 / 0)", "division by zero")102        expectFailure("-'x'", "cannot negate")103    }104105    @Test func plusConcatenatesStringsAndLists() throws {106        #expect(try ev("'a' + 'b'") == "ab" && ev("'n=' + 1") == "n=1" && ev("1 + 'a'") == "1a" && ev("'v' + 1.5 + true + null") == "v1.5truenull")107        #expect(try ev("'l=' + [1, 2]") == "l=[1,2]" && ev("[1] + [2, 3]") == [1, 2, 3] && ev("[] + []") == [])108        expectFailure("true + 1", "needs numbers")109        expectFailure("[1] + 1", "needs numbers")110        expectFailure("'a' * 2", "needs numbers")111        expectFailure("null - 1", "needs numbers")112    }113}114115@Suite("Expression language: access and scoping (unit)") struct ExpressionAccessTests {116    @Test func indexing() throws {117        #expect(try ev("params.list[0]") == 1 && ev("params.list[-1]") == 3 && ev("params.list[5]") == .null && ev("params.list[-9]") == .null)118        #expect(try ev("'abc'[1]") == "b" && ev("'abc'[-1]") == "c" && ev("'abc'[3]") == .null)119        #expect(try ev("vars.obj['a']") == 1 && ev("vars.obj[\"b\"][1]") == 20 && ev("vars.obj.b.1") == 20 && ev("vars.obj.b.length") == 2)120        #expect(try ev("params.list[1 + 1]") == 3 && ev("[10, 20][1]") == 20 && ev("vars.obj['zz']") == .null)121        #expect(try ev("vars.missing") == .null && ev("vars.missing.deep.er") == .null && ev("vars.missing[0]") == .null && ev("params.list.name") == .null)122        #expect(try ev("params.name.anything") == .null)123        expectFailure("1[0]", "cannot index")124        expectFailure("params.list[1.5]", "must be an integer")125        expectFailure("params.list['x']", "must be an integer")126        expectFailure("'abc'['x']", "must be an integer")127    }128129    @Test func stepsParamsAndVars() throws {130        #expect(try ev("steps.fetch.status") == "succeeded" && ev("steps.fetch.output.items[1].name") == "b" && ev("steps.fetch.output.items.0.size") == 1)131        #expect(try ev("steps.fetch.error") == .null && ev("steps.fetch.output.nothing.here") == .null)132        #expect(try ev("steps.step-one.status") == "failed" && ev("steps.step-one.output.n - 1") == 6 && ev("steps['step-one'].error") == "boom")133        #expect(try ev("len(steps.fetch.output.items) > 1 && steps.fetch.status == 'succeeded'") == true)134        #expect(try ev("params.name") == "Ada" && ev("params['n'] * 2") == 6 && ev("vars.count + params.n") == 5 && ev("keys(params)") == ["folder", "list", "n", "name"])135        expectFailure("steps.ghost.output", "unknown step 'ghost'")136        expectFailure("steps['ghost']", "unknown step 'ghost'")137        expectFailure("params.ghost", "unknown parameter 'ghost'")138        expectFailure("stranger", "unknown name 'stranger'")139        expectFailure("stranger.field", "unknown name 'stranger'")140    }141142    @Test func localsAndLambdaScoping() throws {143        #expect(try ev("item.name") == "it" && ev("index + 1") == 5)144        #expect(try ev("map(params.list, x => x * params.n)") == [3, 6, 9])145        #expect(try ev("map(params.list, item => item)") == [1, 2, 3], "a lambda parameter shadows a loop variable")146        #expect(try ev("map([1], item => item) + [item.name]") == [1, "it"], "and the loop variable is back afterwards")147        #expect(try ev("map([10, 20], (x, i) => x + i)") == [10, 21])148        #expect(try ev("map([1, 2], x => map([10, 20], y => x + y))") == [[11, 21], [12, 22]])149        #expect(try ev("map([[1, 2], [3]], x => map(x, x => x * 2))") == [[2, 4], [6]])150        #expect(try ev("map([1], params => params)") == [1] && ev("map([1], steps => steps + 1)") == [2])151        expectFailure("map([1], x => x) + x", "unknown name 'x'")152        expectFailure("map([1], x => y)", "unknown name 'y'")153    }154155    @Test func environmentIsAnAllowlist() throws {156        #expect(try ev("env.HOME") == "/Users/t" && ev("env.USER") == "t" && ev("env.TMPDIR") == "/tmp/" && ev("env['DATE']") == "2027-01-15")157        #expect(try ev("env.TIME + ' ' + env.DATETIME + ' ' + env.TIMESTAMP") == "08:00:00 2027-01-15T08:00:00Z 1800000000")158        #expect(try ev("keys(env)") == ["DATE", "DATETIME", "HOME", "TIME", "TIMESTAMP", "TMPDIR", "USER"])159        expectFailure("env.PATH", "'env.PATH' is not available")160        expectFailure("env['PATH']", "'env.PATH' is not available")161        expectFailure("env.SECRET_TOKEN", "not available")162        #expect(try ev("toJSON(env)").stringValue?.contains("nope") == false)163        let real = WorkflowExpression.defaultEnvironment()164        #expect(Set(real.keys) == WorkflowExpression.environmentAllowlist && real["HOME"] == NSHomeDirectory())165    }166}167168@Suite("Expression language: functions (unit)") struct ExpressionFunctionTests {169    /// name, expression, expected value, an expression that must fail.170    static let table: [(String, String, JSONValue, String)] = [171        ("len", "[len('héllo'), len(params.list), len(vars.obj), len(null)]", [5, 3, 2, 0], "len(5)"),172        ("contains", "[contains('hello', 'ell'), contains(params.list, 2.0), contains(vars.obj, 'a'), contains(null, 1), contains([1], 9)]", [true, true, true, false, false], "contains(5, 1)"),173        ("startsWith", "[startsWith('hello', 'he'), startsWith('hello', 'lo')]", [true, false], "startsWith(1, 'a')"),174        ("endsWith", "[endsWith('a.pdf', '.pdf'), endsWith('a.pdf', '.txt')]", [true, false], "endsWith('a')"),175        ("lower", "lower('HeLLo')", "hello", "lower(1)"),176        ("upper", "upper('héllo')", "HÉLLO", "upper([])"),177        ("trim", "trim('  a b \\n')", "a b", "trim(null)"),178        ("split", "split('a,b,,c', ',')", ["a", "b", "", "c"], "split('a', '')"),179        ("join", "[join(['a', 1, true], '-'), join([1, 2]), join(null, '-')]", ["a-1-true", "1,2", ""], "join('abc', '-')"),180        ("replace", "replace('a-b-c', '-', '+')", "a+b+c", "replace('a', '', 'b')"),181        ("basename", "basename('/a/b/c.txt')", "c.txt", "basename(1)"),182        ("dirname", "dirname('/a/b/c.txt')", "/a/b", "dirname()"),183        ("extname", "[extname('/a/b.TXT'), extname('/a/b'), extname('archive.tar.gz')]", [".TXT", "", ".gz"], "extname(null)"),184        ("joinPath", "[joinPath('~/a/', '/b', 'c.txt'), joinPath('/root'), joinPath('', 'x')]", ["~/a/b/c.txt", "/root", "x"], "joinPath()"),185        ("toJSON", "toJSON(vars.obj)", "{\"a\":1,\"b\":[10,20]}", "toJSON()"),186        ("fromJSON", "fromJSON('{\"a\": [1, 2.5, null]}').a", [1, 2.5, nil], "fromJSON('nope')"),187        ("number", "[number('42'), number(' 1.5 '), number(true), number(7), number('-3')]", [42, 1.5, 1, 7, -3], "number('x')"),188        ("string", "[string(12), string([1, 'a']), string(null), string('s'), string(1.5)]", ["12", "[1,\"a\"]", "null", "s", "1.5"], "string()"),189        ("default", "[default(null, vars.ghost, 3), default(null), default(0, 5), default('', 'x')]", [3, nil, 0, ""], "default()"),190        ("keys", "keys(vars.obj)", ["a", "b"], "keys([1])"),191        ("values", "values(vars.obj)", [1, [10, 20]], "values('x')"),192        ("map", "[map(params.list, x => x * 2), map(steps.fetch.output.items, 'name'), map(null, x => x)]", [[2, 4, 6], ["a", "b"], []], "map(1, x => x)"),193        ("filter", "[filter(params.list, x => x > 1), filter(steps.fetch.output.items, i => i.size > 2), filter([0, 1, '', 'a'], x => x)]",194         [[2, 3], [["name": "b", "size": 5]], [1, "a"]], "filter('abc', x => x)"),195        ("reduce", "[reduce(params.list, (acc, x) => acc + x, 0), reduce(['a', 'b'], (acc, x) => acc + x, ''), reduce([], (a, b) => a, 9)]", [6, "ab", 9],196         "reduce(params.list, x => x, 0)"),197        ("count", "[count(params.list), count(params.list, x => x > 1), count(null)]", [3, 2, 0], "count(5)"),198        ("sum", "[sum(params.list), sum(steps.fetch.output.items, i => i.size), sum([1, 0.5]), sum([])]", [6, 6, 1.5, 0], "sum(['a'])"),199        ("first", "[first(params.list), first([]), first(null)]", [1, nil, nil], "first('abc')"),200        ("last", "[last(params.list), last([])]", [3, nil], "last(1)"),201        ("slice", "[slice('hello', 1, 3), slice('hello', -3), slice(params.list, 1), slice(params.list, -2, -1), slice(params.list, 2, 1), slice(params.list, 0, 99)]",202         ["el", "llo", [2, 3], [2], [], [1, 2, 3]], "slice(params.list, 'a')"),203        ("now", "now()", "2027-01-15T08:00:00Z", "now(1)"),204        ("formatDate", "[formatDate('2026-11-21T10:30:00Z', 'yyyy/MM/dd HH:mm'), formatDate(1800000000, 'yyyy-MM-dd'), formatDate('2026-11-21T10:30:00Z', 'HH:mm', 'America/Toronto'), formatDate('2026-07-04', 'EEE d MMM')]",205         ["2026/11/21 10:30", "2027-01-15", "05:30", "Sat 4 Jul"], "formatDate('not a date', 'yyyy')"),206        ("secret", "secret('smtp-password')", ["$secret": "smtp-password"], "secret(vars.text)"),207    ]208209    @Test func theTableCoversEveryFunction() {210        #expect(Self.table.map(\.0).sorted() == WorkflowExpression.functionNames.sorted())211        #expect(Set(WorkflowExpression.functionNames).count == WorkflowExpression.functionNames.count)212    }213214    @Test(arguments: table.indices)215    func everyFunctionWorksAndReportsMisuse(index: Int) throws {216        let (name, expression, expected, bad) = Self.table[index]217        #expect(try ev(expression) == expected, "\(name): \(expression)")218        let e = failure(bad)219        #expect(e != nil, "\(name): \(bad) should fail")220        #expect(e?.message.isEmpty == false)221    }222223    @Test func misuseMessagesAreSpecific() {224        expectFailure("nope(1)", "unknown function 'nope'")225        expectFailure("len()", "len() takes 1 argument, got 0")226        expectFailure("slice([1])", "slice() takes 2 to 3 arguments")227        expectFailure("fromJSON('{')", "could not parse")228        expectFailure("formatDate(1800000000, 'yyyy', 'Mars/Olympus')", "unknown time zone")229        expectFailure("formatDate(true, 'yyyy')", "ISO 8601 string or a Unix timestamp")230        expectFailure("number([])", "cannot convert")231        expectFailure("map(params.list, 5)", "lambda")232        expectFailure("map(params.list, (a, b, c) => a)", "takes 3 arguments")233        expectFailure("vars.text(1)", "only named functions can be called")234        expectFailure("x => x", "unexpected")235        expectFailure("(x => x)(1)", "expected")236        expectFailure("1 + (x => x)", "expected")237    }238239    @Test func secretYieldsAnOpaquePlaceholderOnly() throws {240        #expect(try ev("secret(\"api-token\")") == ["$secret": "api-token"])241        #expect(WorkflowExpression.secretPlaceholderKey == "$secret")242        expectFailure("secret(vars.text)", "literal reference name")243        expectFailure("secret('a' + 'b')", "literal reference name")244        expectFailure("secret('')", "literal reference name")245        expectFailure("secret('a', 'b')", "takes 1 argument")246        let header = try WorkflowExpression.interpolate("Bearer ${{ secret('api-token') }}", context: context())247        #expect(header == "Bearer {\"$secret\":\"api-token\"}", "inside text the placeholder is still only a reference")248    }249}250251@Suite("Expression language: templates and injection (unit)") struct ExpressionTemplateTests {252    private func tpl(_ s: String) throws -> JSONValue { try WorkflowExpression.interpolate(s, context: context()) }253254    @Test func aSingleInterpolationYieldsTheTypedValue() throws {255        #expect(try tpl("${{ params.list }}") == [1, 2, 3] && tpl("${{ 1 + 1 }}") == .int(2) && tpl("${{ vars.obj }}") == ["a": 1, "b": [10, 20]])256        #expect(try tpl("${{ null }}") == .null && tpl("${{true}}") == true && tpl("${{ vars.count > 1 }}") == true)257        #expect(try tpl(" ${{ 1 }}") == " 1" && tpl("${{ 1 }} ") == "1 " && tpl("${{ 1 }}${{ 2 }}") == "12")258        #expect(try tpl("no interpolation") == "no interpolation" && tpl("") == "" && tpl("$ {{ 1 }} ${ {1} }") == "$ {{ 1 }} ${ {1} }")259    }260261    @Test func mixedTextUsesDisplayStrings() throws {262        #expect(try tpl("n=${{ 1 + 1 }} l=${{ [1, 2] }} o=${{ vars.obj }} z=${{ null }} b=${{ true }} s=${{ params.name }} f=${{ 0.5 }}")263                == "n=2 l=[1,2] o={\"a\":1,\"b\":[10,20]} z=null b=true s=Ada f=0.5")264        #expect(try tpl("${{ params.folder }}/out-${{ env.DATE }}.pdf") == "~/x/out-2027-01-15.pdf")265        #expect(try tpl("line1\n${{ join(map(params.list, x => '- ' + x), '\\n') }}\nend") == "line1\n- 1\n- 2\n- 3\nend")266    }267268    @Test func escapesAndDelimiters() throws {269        #expect(try tpl("$${{ not evaluated }}") == "${{ not evaluated }}")270        #expect(try tpl("cost: $${{ x }} and ${{ 1 }}") == "cost: ${{ x }} and 1")271        #expect(try tpl("${{ \"a}}b\" }}") == "a}}b" && tpl("${{ 'x}}' + '{{y' }}!") == "x}}{{y!")272        #expect(try tpl("${{ 'it\\'s }}' }}") == "it's }}")273        #expect(try tpl("a }} b") == "a }} b")274    }275276    @Test func templateErrorsCarryOffsets() {277        let unterminated = failure("abc ${{ 1 + ", template: true)278        #expect(unterminated?.message.contains("unterminated") == true && unterminated?.offset == 4)279        let inner = failure("ab ${{ 1 + }} cd", template: true)280        #expect(inner != nil && inner?.offset == 11, "offset \(inner?.offset ?? -1) points inside the template")281        let second = failure("${{ 1 }} and ${{ nope }}", template: true)282        #expect(second?.message.contains("unknown name 'nope'") == true && second?.offset == 17)283        #expect(failure("${{ }}", template: true)?.message.contains("empty expression") == true)284        let bare = failure("1 + ")285        #expect(bare?.offset == 4)286        #expect(WorkflowExpression.syntaxErrors(in: "1 +", isExpressionField: true).count == 1)287        #expect(WorkflowExpression.syntaxErrors(in: "plain text (not an expression", isExpressionField: false).isEmpty)288        #expect(WorkflowExpression.syntaxErrors(in: "x ${{ ( }}", isExpressionField: false).count == 1)289        #expect(WorkflowExpression.syntaxErrors(in: "len(vars.x) > 0", isExpressionField: true).isEmpty)290    }291292    @Test func interpolationIsSinglePassAndResultsAreData() throws {293        // A step output that looks like a template is returned verbatim: never parsed, never evaluated.294        #expect(try tpl("${{ steps.fetch.output.text }}") == .string(hostile))295        #expect(try tpl("pre ${{ steps.fetch.output.text }} post") == .string("pre \(hostile) post"))296        #expect(try tpl("${{ steps.fetch.output.text + steps.fetch.output.text }}") == .string(hostile + hostile))297        #expect(try WorkflowExpression.evaluateField("steps.fetch.output.text", context: context()) == .string(hostile))298        let resolved = try WorkflowExpression.resolve(["body": "${{ steps.fetch.output.text }}", "list": ["x ${{ steps.fetch.output.text }}"]], context: context())299        #expect(resolved == ["body": .string(hostile), "list": [.string("x \(hostile)")]])300        // Resolving the already-resolved value again is what must never happen implicitly; it is shown here to be a distinct, explicit call.301        #expect(try WorkflowExpression.resolve(resolved, context: context()) != resolved)302        // Data cannot become code through functions either.303        #expect(try ev("fromJSON('\"${{ env.HOME }}\"')") == "${{ env.HOME }}")304        #expect(try ev("replace(steps.fetch.output.text, 'x', 'y')").stringValue?.contains("${{ secret(\"y\") }}") == true)305        #expect(try ev("'${{ 1 + 1 }}'") == "${{ 1 + 1 }}", "a string literal inside an expression is just a string")306        #expect(try tpl("${{ '$' + '{{ env.HOME }}' }}") == "${{ env.HOME }}")307    }308309    @Test func evaluateFieldAndResolve() throws {310        let c = context()311        #expect(try WorkflowExpression.evaluateField("1 + 1", context: c) == 2)312        #expect(try WorkflowExpression.evaluateField("${{ 1 + 1 }}", context: c) == 2)313        #expect(try WorkflowExpression.evaluateField("x ${{ 1 }}", context: c) == "x 1")314        let value: JSONValue = ["path": "${{ params.folder }}/a", "n": "${{ params.n }}", "keep": 5, "flag": true, "nested": ["list": ["${{ vars.count }}", "lit", nil]],315                                "${{ key }}": "untouched key"]316        #expect(try WorkflowExpression.resolve(value, context: c)317                == ["path": "~/x/a", "n": 3, "keep": 5, "flag": true, "nested": ["list": [2, "lit", nil]], "${{ key }}": "untouched key"])318        #expect(throws: WorkflowExpressionError.self) { try WorkflowExpression.resolve(["a": ["${{ nope }}"]], context: c) }319    }320}321322@Suite("Expression language: rejected syntax, bounds and analysis (unit)") struct ExpressionSafetyTests {323    @Test func noAssignmentNoStatementsNoArbitraryCode() {324        expectFailure("vars.count = 1", "assignment is not part of the expression language")325        expectFailure("a = 1", "assignment")326        expectFailure("1; 2", "unexpected character ';'")327        expectFailure("`ls`", "unexpected character '`'")328        expectFailure("$(whoami)", "unexpected character '$'")329        expectFailure("{a: 1}", "unexpected character '{'")330        expectFailure("1 ? 2 : 3", "unexpected character '?'")331        expectFailure("1 | 2", "unexpected character '|'")332        expectFailure("1 & 2", "unexpected character '&'")333        expectFailure("vars.text.toUpperCase()", "only named functions can be called")334        expectFailure("len('a')('b')", "only named functions can be called")335        expectFailure("eval('1 + 1')", "unknown function 'eval'")336        expectFailure("import('os')", "unknown function 'import'")337        expectFailure("system('rm -rf /')", "unknown function 'system'")338        expectFailure("__proto__", "unknown name")339        expectFailure("1 2", "unexpected number")340        expectFailure("(1", "expected ')'")341        expectFailure("[1, 2", "expected ']'")342        expectFailure("vars.", "expected a property name")343        expectFailure("a b", "unexpected 'b'")344        expectFailure("café", "unexpected character")345    }346347    @Test func nestingDepthIsBounded() throws {348        let ok = String(repeating: "(", count: 20) + "1" + String(repeating: ")", count: 20)349        #expect(try ev(ok) == 1)350        let deep = String(repeating: "(", count: 70) + "1" + String(repeating: ")", count: 70)351        expectFailure(deep, "nested too deeply")352        expectFailure(String(repeating: "!", count: 70) + "true", "nested too deeply")353        expectFailure(String(repeating: "[", count: 70) + String(repeating: "]", count: 70), "nested too deeply")354        expectFailure(String(repeating: "len(", count: 70) + "1" + String(repeating: ")", count: 70), "nested too deeply")355    }356357    @Test func sourceLengthAndOperatorChainsAreBounded() throws {358        let long = (0..<6000).map { _ in "1" }.joined(separator: " + ")359        #expect(long.count > ExpressionLimits.maxSourceCharacters)360        expectFailure(long, "longer than")361        // Chains nest without brackets: they are bounded so that a long one is an error, never a stack overflow.362        let limit = ExpressionLimits.maxChainedOperators363        #expect(try ev((0...limit).map { _ in "1" }.joined(separator: " + ")) == .int(limit + 1))364        expectFailure((0...(limit + 1)).map { _ in "1" }.joined(separator: " + "), "too many chained operators")365        expectFailure("vars" + String(repeating: ".a", count: limit + 1), "too many chained operators")366        expectFailure("vars.x" + String(repeating: "[0]", count: limit + 1), "too many chained operators")367        #expect(try ev("vars" + String(repeating: ".a", count: limit)) == .null)368        #expect(try ev("true" + String(repeating: " && true", count: limit - 1) + " && false") == false)369        // Static analysis and renaming walk the same trees.370        let longest = (0..<limit).map { "steps.s\($0).output" }.joined(separator: " + ")371        #expect(longest.count < ExpressionLimits.maxSourceCharacters)372        #expect(WorkflowExpression.syntaxErrors(in: longest, isExpressionField: true).count == 1, "member accesses count as chained operators too")373        let chain = (0..<(limit / 4)).map { "steps.s\($0).output" }.joined(separator: " + ")374        #expect(try WorkflowExpression.references(in: chain, isExpressionField: true).steps.count == limit / 4)375        #expect(try WorkflowExpression.references(in: (0...limit).map { _ in "x" }.joined(separator: " + "), isExpressionField: true).freeNames == ["x"])376        #expect(WorkflowExpression.renamingStep(in: chain, from: "s1", to: "renamed", isExpressionField: true).contains("steps.renamed.output + steps.s2.output"))377        #expect(WorkflowExpression.syntaxErrors(in: "${{ " + (0...(limit + 1)).map { _ in "1" }.joined(separator: "-") + " }}", isExpressionField: false).count == 1)378    }379380    @Test func evaluationWorkIsBounded() {381        var c = context()382        c.vars["big"] = .array((0..<1000).map { .int($0) })383        let nested = "map(vars.big, x => map(vars.big, y => x + y))"384        #expect(throws: WorkflowExpressionError.self) { try WorkflowExpression.evaluate(nested, context: c) }385        do { _ = try WorkflowExpression.evaluate(nested, context: c) }386        catch { #expect("\(error)".contains("evaluation budget")) }387        #expect((try? WorkflowExpression.evaluate("sum(map(vars.big, x => x * 2))", context: c)) == .int(999_000))388    }389390    @Test func outputSizeIsBounded() throws {391        var c = context()392        c.vars["s"] = .string(String(repeating: "a", count: 600_000))393        c.vars["many"] = .array(Array(repeating: .int(1), count: 60_000))394        func message(_ source: String, template: Bool = false) -> String {395            do {396                _ = template ? try WorkflowExpression.interpolate(source, context: c) : try WorkflowExpression.evaluate(source, context: c)397                return "no error"398            } catch { return "\(error)" }399        }400        #expect(message("vars.s + vars.s").contains("larger than"))401        #expect(message("join([vars.s, vars.s], '')").contains("larger than"))402        #expect(message("replace(vars.s, 'a', 'bb')").contains("larger than"))403        #expect(message("${{ vars.s }}${{ vars.s }}", template: true).contains("larger than"))404        #expect(message("vars.many + vars.many").contains("more than"))405        #expect(message("split(vars.s, 'a')").contains("more than"))406        #expect(try WorkflowExpression.evaluate("len(vars.s + 'b')", context: c) == .int(600_001))407    }408409    @Test func referencesAreFoundStatically() throws {410        let refs = try WorkflowExpression.references(411            in: "x ${{ steps.a.output + params.p + vars.v + env.HOME + secret('s') }} y ${{ len(item) + map(xs, y => y + z + steps[\"b-2\"].status) + steps.c-3.output }}",412            isExpressionField: false)413        #expect(refs.steps == ["a", "b-2", "c-3"] && refs.parameters == ["p"] && refs.variables == ["v"] && refs.environment == ["HOME"])414        #expect(refs.secrets == ["s"] && refs.functions == ["secret", "len", "map"] && refs.freeNames == ["item", "xs", "z"])415        let bare = try WorkflowExpression.references(in: "params['q'] + vars[\"w\"] + env['USER']", isExpressionField: true)416        #expect(bare.parameters == ["q"] && bare.variables == ["w"] && bare.environment == ["USER"])417        #expect(try WorkflowExpression.references(in: "steps.a.output", isExpressionField: false) == ExpressionReferences(), "plain template text holds no reference")418        #expect(try WorkflowExpression.references(in: "map([1], steps => steps.a)", isExpressionField: true).steps.isEmpty, "a lambda parameter named steps is not the steps root")419        #expect(throws: WorkflowExpressionError.self) { try WorkflowExpression.references(in: "${{ 1 + }}", isExpressionField: false) }420    }421422    @Test func renamingAStepRewritesOnlyRealReferences() {423        let template = "${{ steps.old.output }} and ${{ steps.older.x + steps.old.y + steps['old'].z + steps[\"old\"].w }} steps.old 'old'"424        #expect(WorkflowExpression.renamingStep(in: template, from: "old", to: "fresh-1", isExpressionField: false)425                == "${{ steps.fresh-1.output }} and ${{ steps.older.x + steps.fresh-1.y + steps['fresh-1'].z + steps[\"fresh-1\"].w }} steps.old 'old'")426        #expect(WorkflowExpression.renamingStep(in: "steps.old.status == 'old' && vars.old > 0 && params.steps.old", from: "old", to: "n", isExpressionField: true)427                == "steps.n.status == 'old' && vars.old > 0 && params.steps.old")428        #expect(WorkflowExpression.renamingStep(in: "steps.a-b.output", from: "a-b", to: "c", isExpressionField: true) == "steps.c.output")429        #expect(WorkflowExpression.renamingStep(in: "steps.old.output", from: "old", to: "n", isExpressionField: false) == "steps.old.output", "literal text is not an expression")430        #expect(WorkflowExpression.renamingStep(in: "${{ steps.old. }}", from: "old", to: "n", isExpressionField: false) == "${{ steps.old. }}", "text that does not parse is left alone")431        let renamed = WorkflowExpression.renamingStep(in: "${{ steps.old.output }}", from: "old", to: "new", isExpressionField: false)432        #expect((try? WorkflowExpression.references(in: renamed, isExpressionField: false))?.steps == ["new"])433    }434}435
Files main
Menu

Theme