|
| 1 | +# SPDX-License-Identifier: MPL-2.0 |
| 2 | + |
| 3 | +defmodule VeriSim.Query.VQLBridgeTest do |
| 4 | + @moduledoc """ |
| 5 | + Tests for VeriSim.Query.VQLBridge — the Elixir↔Deno bridge for the |
| 6 | + ReScript VQL parser. |
| 7 | +
|
| 8 | + When the external Deno/Node subprocess is unavailable (which is the |
| 9 | + common case in CI without those runtimes), the bridge falls back to |
| 10 | + a pure-Elixir built-in parser. These tests exercise that fallback |
| 11 | + path through the public GenServer API — they're guaranteed to hit |
| 12 | + the fallback because no subprocess is started in the test |
| 13 | + environment. |
| 14 | +
|
| 15 | + `typecheck/2` has no fallback (it requires the ReScript bidirectional |
| 16 | + checker), so it must return `{:error, :type_checker_unavailable}` |
| 17 | + when the port is nil. |
| 18 | + """ |
| 19 | + |
| 20 | + use ExUnit.Case, async: false |
| 21 | + |
| 22 | + alias VeriSim.Query.VQLBridge |
| 23 | + |
| 24 | + setup do |
| 25 | + case GenServer.whereis(VQLBridge) do |
| 26 | + nil -> |
| 27 | + {:ok, _pid} = VQLBridge.start_link([]) |
| 28 | + :ok |
| 29 | + |
| 30 | + _pid -> |
| 31 | + :ok |
| 32 | + end |
| 33 | + |
| 34 | + :ok |
| 35 | + end |
| 36 | + |
| 37 | + describe "parse/2 — built-in fallback" do |
| 38 | + test "parses a simple SELECT query" do |
| 39 | + assert {:ok, ast} = VQLBridge.parse("SELECT GRAPH FROM HEXAD abc-123") |
| 40 | + assert is_map(ast) |
| 41 | + assert Map.has_key?(ast, :modalities) |
| 42 | + assert Map.has_key?(ast, :source) |
| 43 | + end |
| 44 | + |
| 45 | + test "parses multi-modality SELECT" do |
| 46 | + assert {:ok, ast} = VQLBridge.parse("SELECT GRAPH, VECTOR, DOCUMENT FROM HEXAD abc-123") |
| 47 | + |
| 48 | + assert :graph in ast.modalities |
| 49 | + assert :vector in ast.modalities |
| 50 | + assert :document in ast.modalities |
| 51 | + end |
| 52 | + |
| 53 | + test "parses SELECT * as :all" do |
| 54 | + assert {:ok, ast} = VQLBridge.parse("SELECT * FROM HEXAD abc-123") |
| 55 | + assert :all in ast.modalities |
| 56 | + end |
| 57 | + |
| 58 | + test "captures the source UUID from FROM HEXAD clause" do |
| 59 | + assert {:ok, ast} = VQLBridge.parse("SELECT GRAPH FROM HEXAD entity-xyz") |
| 60 | + assert match?({:octad, "entity-xyz"}, ast.source) |
| 61 | + end |
| 62 | + |
| 63 | + test "non-SELECT input returns {:error, _}" do |
| 64 | + assert {:error, _} = VQLBridge.parse("HELLO WORLD") |
| 65 | + end |
| 66 | + |
| 67 | + test "empty string returns {:error, _}" do |
| 68 | + assert {:error, _} = VQLBridge.parse("") |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + describe "parse_slipstream/2" do |
| 73 | + test "accepts a query without PROOF" do |
| 74 | + assert {:ok, _ast} = VQLBridge.parse_slipstream("SELECT GRAPH FROM HEXAD abc-123") |
| 75 | + end |
| 76 | + |
| 77 | + test "rejects a query with PROOF clause" do |
| 78 | + assert {:error, msg} = |
| 79 | + VQLBridge.parse_slipstream( |
| 80 | + "SELECT GRAPH FROM HEXAD abc-123 PROOF EXISTENCE(entity-001)" |
| 81 | + ) |
| 82 | + |
| 83 | + assert is_binary(msg) |
| 84 | + assert String.contains?(msg, "PROOF") or String.contains?(msg, "Slipstream") |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + describe "parse_dependent/2" do |
| 89 | + test "rejects a query without PROOF clause" do |
| 90 | + assert {:error, _msg} = VQLBridge.parse_dependent("SELECT GRAPH FROM HEXAD abc-123") |
| 91 | + end |
| 92 | + |
| 93 | + test "accepts a query with PROOF clause" do |
| 94 | + assert {:ok, ast} = |
| 95 | + VQLBridge.parse_dependent( |
| 96 | + "SELECT GRAPH FROM HEXAD abc-123 PROOF EXISTENCE(entity-001)" |
| 97 | + ) |
| 98 | + |
| 99 | + assert ast.proof != nil |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + describe "parse_mutation/2" do |
| 104 | + test "parses INSERT statement" do |
| 105 | + assert {:ok, mutation} = |
| 106 | + VQLBridge.parse_mutation("INSERT HEXAD WITH GRAPH {} AND VECTOR {}") |
| 107 | + |
| 108 | + assert is_map(mutation) |
| 109 | + end |
| 110 | + |
| 111 | + test "parses UPDATE statement" do |
| 112 | + assert {:ok, mutation} = VQLBridge.parse_mutation("UPDATE HEXAD abc-123 SET GRAPH {}") |
| 113 | + |
| 114 | + assert is_map(mutation) |
| 115 | + end |
| 116 | + |
| 117 | + test "parses DELETE statement" do |
| 118 | + assert {:ok, mutation} = VQLBridge.parse_mutation("DELETE HEXAD abc-123") |
| 119 | + assert is_map(mutation) |
| 120 | + end |
| 121 | + |
| 122 | + test "non-mutation input returns {:error, _}" do |
| 123 | + assert {:error, _} = VQLBridge.parse_mutation("SELECT GRAPH FROM HEXAD x") |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + describe "parse_statement/2" do |
| 128 | + test "routes SELECT to Query tag" do |
| 129 | + assert {:ok, %{TAG: "Query"}} = VQLBridge.parse_statement("SELECT GRAPH FROM HEXAD abc-123") |
| 130 | + end |
| 131 | + |
| 132 | + test "routes INSERT to Mutation tag" do |
| 133 | + assert {:ok, %{TAG: "Mutation"}} = |
| 134 | + VQLBridge.parse_statement("INSERT HEXAD WITH GRAPH {} AND VECTOR {}") |
| 135 | + end |
| 136 | + |
| 137 | + test "routes DELETE to Mutation tag" do |
| 138 | + assert {:ok, %{TAG: "Mutation"}} = VQLBridge.parse_statement("DELETE HEXAD abc-123") |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + describe "typecheck/2" do |
| 143 | + test "returns :type_checker_unavailable when no ReScript subprocess" do |
| 144 | + # In test env the Deno/Node subprocess isn't started, so the |
| 145 | + # typecheck handler returns this specific error rather than |
| 146 | + # silently passing. Critical for VQL-DT integrity. |
| 147 | + ast = %{modalities: [:graph], source: {:octad, "abc"}} |
| 148 | + assert {:error, :type_checker_unavailable} = VQLBridge.typecheck(ast) |
| 149 | + end |
| 150 | + end |
| 151 | + |
| 152 | + describe "parse_and_execute/2 — error propagation" do |
| 153 | + test "parse failure propagates" do |
| 154 | + assert {:error, _} = VQLBridge.parse_and_execute("INVALID GIBBERISH", []) |
| 155 | + end |
| 156 | + end |
| 157 | +end |
0 commit comments