Repository Public License
1// ORIX OS Automation2// File: Packages/ORIXFeatureCode/Tests/ORIXFeatureCodeTests/CompletionTests.swift3// Description: Unit tests of completion: context detection at the cursor and the candidates built from tool manifests and the document.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import ORIXAppUI9import Testing10@testable import ORIXFeatureCode1112/// Analyses `text` with the cursor at the `|` marker.13func analyze(_ marked: String, format: WorkflowFormat = .yaml) -> (CompletionRequest, String) {14 let cursor = (marked as NSString).range(of: "|").location15 let text = (marked as NSString).replacingCharacters(in: NSRange(location: cursor, length: 1), with: "")16 return (CompletionAnalyzer.analyze(text: text, cursor: cursor, format: format), text)17}1819let sampleTools: [ToolManifest] = [20 ToolManifest(identifier: "filesystem.list", name: "List", summary: "Lists the entries of a folder.", family: "filesystem",21 inputSchema: .object(["path": .string("Folder to list."), "recursive": .boolean("Descend into subfolders."), "limit": .integer("Maximum entries.")], required: ["path"]),22 outputSchema: .object(["entries": .array(of: .string()), "count": .integer()]), sideEffect: .read, defaultTier: .readOnly),23 ToolManifest(identifier: "filesystem.write", name: "Write", summary: "Creates or overwrites a file.", family: "filesystem",24 inputSchema: .object(["path": .string("File."), "content": .string("Text.")], required: ["path", "content"]), sideEffect: .write, defaultTier: .reversibleWrite),25 ToolManifest(identifier: "shell.run", name: "Run", summary: "Runs one executable with arguments.", family: "shell",26 inputSchema: .object(["command": .string(), "args": .array(of: .string())], required: ["command"]), sideEffect: .external, defaultTier: .consequential),27]2829let document = """30apiVersion: orix.ai/v131kind: Automation32metadata:33 id: demo34 name: Demo35spec:36 parameters:37 - name: folder38 type: path39 - name: limit40 type: number41 variables:42 target: ~/out43 steps:44 - id: list45 tool: filesystem.list46 with:47 path: ${{ params.folder }}48 - id: write49 needs: [list]50 tool: filesystem.write51 with:52 path: ~/out/a.txt5354"""5556@Suite("Unit · completion") struct CompletionTests {57 @Test func toolIDAfterToolKey() {58 let (r, text) = analyze(document + " - id: more\n tool: files|")59 #expect(r.context == .toolID(prefix: "files"))60 #expect(r.automatic)61 #expect((text as NSString).substring(with: r.replaceRange) == "files")62 let items = CompletionEngine.candidates(r.context, catalog: CompletionCatalog(tools: sampleTools))63 #expect(items.map(\.label) == ["filesystem.list", "filesystem.write"])64 #expect(items[0].documentation == "Lists the entries of a folder.")65 #expect(items[0].detail.contains("tier 0"))66 // A dash-led step and an empty prefix.67 #expect(analyze("steps:\n - tool: |").0.context == .toolID(prefix: ""))68 // Contains-matches come after prefix matches.69 #expect(CompletionEngine.candidates(.toolID(prefix: "run"), catalog: CompletionCatalog(tools: sampleTools)).map(\.label) == ["shell.run"])70 // Nothing when the only candidate is what is already typed.71 #expect(CompletionEngine.candidates(.toolID(prefix: "shell.run"), catalog: CompletionCatalog(tools: sampleTools)).isEmpty)72 }7374 @Test func argumentNamesInsideWith() {75 let (r, _) = analyze(document.replacingOccurrences(of: " path: ${{ params.folder }}\n", with: " path: ${{ params.folder }}\n |\n"))76 #expect(r.context == .argumentName(tool: "filesystem.list", prefix: "", existing: ["path"]))77 #expect(r.automatic)78 let items = CompletionEngine.candidates(r.context, catalog: CompletionCatalog(tools: sampleTools))79 #expect(items.map(\.label) == ["limit", "recursive"])80 #expect(items[1].insertText == "recursive: ")81 #expect(items[1].detail == "boolean")82 #expect(items[1].documentation == "Descend into subfolders.")83 // Required arguments come first and say so; `tool:` may come after `with:`.84 let (r2, _) = analyze("steps:\n - id: w\n with:\n c|\n tool: filesystem.write\n")85 #expect(r2.context == .argumentName(tool: "filesystem.write", prefix: "c", existing: []))86 let all = CompletionEngine.candidates(.argumentName(tool: "filesystem.write", prefix: "", existing: []), catalog: CompletionCatalog(tools: sampleTools))87 #expect(Set(all.map(\.label)) == ["path", "content"])88 #expect(all.allSatisfy { $0.detail == "string · required" })89 // A step written with the dash on the tool line.90 let (r3, _) = analyze("steps:\n - tool: shell.run\n with:\n |")91 #expect(r3.context == .argumentName(tool: "shell.run", prefix: "", existing: []))92 #expect(CompletionEngine.candidates(r3.context, catalog: CompletionCatalog(tools: sampleTools)).map(\.label) == ["command", "args"])93 }9495 @Test func needsInFlowAndBlockForm() {96 let (r, text) = analyze(document + " - id: third\n needs: [list, wr|")97 #expect(r.context == .stepReference(prefix: "wr", listed: ["list"], currentStep: "third"))98 let catalog = CompletionCatalog.scrape(text, tools: sampleTools)99 #expect(catalog.stepIDs == ["list", "write", "third"])100 #expect(catalog.stepTools == ["list": "filesystem.list", "write": "filesystem.write"])101 #expect(catalog.parameters == ["folder", "limit"])102 #expect(catalog.variables == ["target"])103 #expect(CompletionEngine.candidates(r.context, catalog: catalog).map(\.label) == ["write"])104 let (b, _) = analyze(document + " - id: third\n needs:\n - list\n - |")105 #expect(b.context == .stepReference(prefix: "", listed: ["list"], currentStep: "third"))106 #expect(CompletionEngine.candidates(b.context, catalog: catalog).map(\.label) == ["write"])107 }108109 @Test func expressions() {110 let catalog = CompletionCatalog.scrape(document, tools: sampleTools)111 let (root, _) = analyze("with:\n path: ${{ pa|")112 #expect(root.context == .expressionRoot(prefix: "pa"))113 #expect(root.automatic)114 #expect(CompletionEngine.candidates(root.context, catalog: catalog).map(\.label) == ["params", "joinPath"]) // prefix matches first, then contains115 let empty = CompletionEngine.candidates(.expressionRoot(prefix: ""), catalog: catalog)116 #expect(empty.prefix(4).map(\.label) == ["params", "vars", "steps", "env"])117 #expect(WorkflowExpression.functionNames.allSatisfy { name in empty.contains { $0.label == name && $0.insertText == name + "(" } })118119 #expect(analyze("x: ${{ params.|").0.context == .expressionMember(path: ["params"], prefix: ""))120 #expect(CompletionEngine.candidates(.expressionMember(path: ["params"], prefix: "f"), catalog: catalog).map(\.label) == ["folder"])121 #expect(CompletionEngine.candidates(.expressionMember(path: ["vars"], prefix: ""), catalog: catalog).map(\.label) == ["target"])122 #expect(CompletionEngine.candidates(.expressionMember(path: ["env"], prefix: "DA"), catalog: catalog).map(\.label) == ["DATE", "DATETIME"])123 #expect(analyze("x: \"a ${{ len(steps.li|").0.context == .expressionMember(path: ["steps"], prefix: "li"))124 #expect(CompletionEngine.candidates(.expressionMember(path: ["steps"], prefix: ""), catalog: catalog).map(\.label) == ["list", "write"])125 #expect(analyze("x: ${{ steps.list.|").0.context == .expressionMember(path: ["steps", "list"], prefix: ""))126 #expect(CompletionEngine.candidates(.expressionMember(path: ["steps", "list"], prefix: ""), catalog: catalog).map(\.label) == ["output", "status", "error", "attempts"])127 #expect(CompletionEngine.candidates(.expressionMember(path: ["steps", "list", "output"], prefix: ""), catalog: catalog).map(\.label) == ["count", "entries"])128 // A dash-named step, a closed expression, and a string literal inside an expression.129 #expect(analyze("x: ${{ steps.fetch-da|").0.context == .expressionMember(path: ["steps"], prefix: "fetch-da"))130 #expect(analyze("x: ${{ a }} and |").0.context == .none)131 #expect(analyze("x: ${{ contains(a, \"par|").0.context == .none)132 // A second expression on the line.133 #expect(analyze("x: ${{ a }}/${{ va|").0.context == .expressionRoot(prefix: "va"))134 }135136 @Test func stepAndMappingKeys() {137 let (s, _) = analyze(document + " - |")138 #expect(s.context == .stepKey(prefix: "", existing: []))139 #expect(!s.automatic)140 let fresh = CompletionEngine.candidates(s.context, catalog: CompletionCatalog())141 #expect(fresh.first?.label == "id")142 #expect(CompletionEngine.stepKinds.allSatisfy { kind in fresh.contains { $0.label == kind.0 && $0.kind == .keyword } })143144 let (k, _) = analyze(document.replacingOccurrences(of: " needs: [list]\n", with: " needs: [list]\n ti|\n"))145 #expect(k.context == .stepKey(prefix: "ti", existing: ["needs", "id", "tool", "with"]))146 let fields = CompletionEngine.candidates(k.context, catalog: CompletionCatalog())147 #expect(fields.map(\.label) == ["timeoutSeconds", "continueOnError"])148 // Once a kind is chosen the other kinds are not offered, its own fields are.149 let agent = CompletionEngine.candidates(.stepKey(prefix: "", existing: ["id", "agent"]), catalog: CompletionCatalog()).map(\.label)150 #expect(agent.contains("objective") && !agent.contains("tool") && !agent.contains("id"))151152 #expect(analyze("apiVersion: orix.ai/v1\n|").0.context == .mappingKey(parent: "", prefix: "", existing: ["apiVersion"]))153 #expect(CompletionEngine.candidates(.mappingKey(parent: "", prefix: "", existing: ["apiVersion"]), catalog: CompletionCatalog()).map(\.label) == ["kind", "metadata", "spec"])154 #expect(analyze("metadata:\n id: a\n na|").0.context == .mappingKey(parent: "metadata", prefix: "na", existing: ["id"]))155 let (spec, _) = analyze("spec:\n triggers: []\n |\n steps: []\n")156 #expect(spec.context == .mappingKey(parent: "spec", prefix: "", existing: ["triggers", "steps"]))157 #expect(analyze("spec:\n permissions:\n filesystem:\n |").0.context == .mappingKey(parent: "filesystem", prefix: "", existing: []))158 #expect(analyze("spec:\n parameters:\n - name: a\n |").0.context == .mappingKey(parent: "parameter", prefix: "", existing: ["name"]))159 // A nested step list (foreach body).160 #expect(analyze("steps:\n - id: each\n foreach: ${{ vars.x }}\n steps:\n - id: inner\n |").0.context == .stepKey(prefix: "", existing: ["id"]))161 // A value position offers nothing.162 #expect(analyze("metadata:\n name: Dem|").0.context == .none)163 }164165 @Test func jsonSource() {166 let (r, _) = analyze("{\n \"spec\": { \"steps\": [ { \"id\": \"a\", \"tool\": \"shell.|", format: .json)167 #expect(r.context == .toolID(prefix: "shell."))168 #expect(analyze("{ \"path\": \"${{ steps.|", format: .json).0.context == .expressionMember(path: ["steps"], prefix: ""))169 #expect(analyze("{ \"na|", format: .json).0.context == .none)170 #expect(CompletionCatalog.scrape("{\"spec\": {\"steps\": [{\"id\": \"one\"}, {\"id\": \"two\"}]}}", tools: []).stepIDs == ["one", "two"])171 }172173 @Test func toolDocumentation() {174 let text = ToolDocs.text(sampleTools[0])175 #expect(text.contains("filesystem.list — List"))176 #expect(text.contains("Arguments: path*: string, limit: integer, recursive: boolean"))177 #expect(text.contains("Tier 0"))178 #expect(text.contains("side effect: read"))179 let ranges = ToolDocs.ranges(in: document)180 #expect(ranges.map(\.id) == ["filesystem.list", "filesystem.write"])181 let offset = (document as NSString).range(of: "filesystem.write").location + 4182 #expect(ToolDocs.toolID(in: document, at: offset)?.id == "filesystem.write")183 #expect(ToolDocs.toolID(in: document, at: 3) == nil)184 #expect(ToolDocs.ranges(in: "{\"tool\": \"shell.run\"}").map(\.id) == ["shell.run"])185 }186}187