Tests/OrgAppTests/BabelSessionTests.swift
32 lines · 2171 bytes
3 symbols in this file
1import Foundation
2import Testing
3import OrgCore
4@testable import OrgApp
5
6struct BabelSessionStateTests {
7 func run(_ text: String, in folder: URL) async throws -> String {
8 let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [2..<2])
9 guard case .success(let job) = Babel.plan(context) else { Issue.record("not planned: \(text)"); return "" }
10 let output = try await BabelRunner.run(job, fileDirectory: folder)
11 guard case .success(let edits) = Babel.finish(job, output: output, text: text) else { return "" }
12 let new = edits.sorted { $0.range.lowerBound > $1.range.lowerBound }.reduce(text) { $1.apply(to: $0) }
13 return String(new.dropFirst(text.count))
14 }
15
16 @Test func sessionsKeepState() async throws {
17 let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-session-\(UUID().uuidString)")
18 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
19 defer {
20 BabelSessions.shared.stopAll()
21 try? FileManager.default.removeItem(at: folder)
22 }
23 _ = try await run("#+begin_src sh :session s1\nx=5; cd /tmp\n#+end_src\n", in: folder)
24 #expect(try await run("#+begin_src sh :session s1 :results output\necho $x\n#+end_src\n", in: folder) == "\n#+RESULTS:\n: 5\n")
25 #expect(try await run("#+begin_src sh :session other :results output\necho \"[$x]\"\n#+end_src\n", in: folder) == "\n#+RESULTS:\n: []\n")
26 _ = try await run("#+begin_src python :session py :results output\na = 2\nprint('set')\n#+end_src\n", in: folder)
27 #expect(try await run("#+begin_src python :session py\na * 3\n#+end_src\n", in: folder) == "\n#+RESULTS:\n: 6\n")
28 #expect(try await run("#+begin_src python :session py :results output\nprint(a + 1)\n#+end_src\n", in: folder) == "\n#+RESULTS:\n: 3\n")
29 _ = try await run("#+begin_src python :session py :results output\nraise ValueError('x')\n#+end_src\n", in: folder)
30 #expect(try await run("#+begin_src python :session py\n[a, a]\n#+end_src\n", in: folder) == "\n#+RESULTS:\n| 2 | 2 |\n")
31 }
32}