|
| 1 | +"""Tests for the StreamEncoder.""" |
| 2 | + |
| 3 | +import io |
| 4 | + |
| 5 | +from gcf import StreamEncoder, Symbol, Edge, decode |
| 6 | + |
| 7 | + |
| 8 | +def test_stream_basic(): |
| 9 | + buf = io.StringIO() |
| 10 | + enc = StreamEncoder(buf, "context_for_task", token_budget=5000) |
| 11 | + |
| 12 | + enc.write_symbol(Symbol(qualified_name="pkg.Auth", kind="function", score=0.78, provenance="lsp_resolved", distance=0)) |
| 13 | + enc.write_symbol(Symbol(qualified_name="pkg.Server", kind="function", score=0.54, provenance="lsp_resolved", distance=1)) |
| 14 | + enc.write_edge(Edge(source="pkg.Server", target="pkg.Auth", edge_type="calls")) |
| 15 | + enc.close() |
| 16 | + |
| 17 | + out = buf.getvalue() |
| 18 | + assert "GCF tool=context_for_task budget=5000\n" in out |
| 19 | + assert "## targets\n" in out |
| 20 | + assert "@0 fn pkg.Auth 0.78 lsp_resolved\n" in out |
| 21 | + assert "## related\n" in out |
| 22 | + assert "@1 fn pkg.Server 0.54 lsp_resolved\n" in out |
| 23 | + assert "## edges [?]\n" in out |
| 24 | + assert "@0<@1 calls\n" in out |
| 25 | + assert "## _summary symbols=2 edges=1" in out |
| 26 | + |
| 27 | + # Header should not have symbols= or edges= |
| 28 | + header = out.split("\n")[0] |
| 29 | + assert "symbols=" not in header |
| 30 | + assert "edges=" not in header |
| 31 | + |
| 32 | + |
| 33 | +def test_stream_round_trip(): |
| 34 | + buf = io.StringIO() |
| 35 | + enc = StreamEncoder(buf, "blast_radius", token_budget=10000) |
| 36 | + |
| 37 | + enc.write_symbol(Symbol(qualified_name="pkg.Auth", kind="function", score=0.95, provenance="lsp", distance=0)) |
| 38 | + enc.write_symbol(Symbol(qualified_name="pkg.Config", kind="type", score=0.80, provenance="ast", distance=0)) |
| 39 | + enc.write_symbol(Symbol(qualified_name="pkg.Server", kind="function", score=0.60, provenance="lsp", distance=1)) |
| 40 | + enc.write_edge(Edge(source="pkg.Server", target="pkg.Auth", edge_type="calls")) |
| 41 | + enc.write_edge(Edge(source="pkg.Auth", target="pkg.Config", edge_type="references")) |
| 42 | + enc.close() |
| 43 | + |
| 44 | + p = decode(buf.getvalue()) |
| 45 | + assert p.tool == "blast_radius" |
| 46 | + assert len(p.symbols) == 3 |
| 47 | + assert len(p.edges) == 2 |
| 48 | + |
| 49 | + |
| 50 | +def test_stream_no_edges(): |
| 51 | + buf = io.StringIO() |
| 52 | + enc = StreamEncoder(buf, "test") |
| 53 | + |
| 54 | + enc.write_symbol(Symbol(qualified_name="a.A", kind="function", score=0.9, provenance="x", distance=0)) |
| 55 | + enc.close() |
| 56 | + |
| 57 | + out = buf.getvalue() |
| 58 | + assert "## edges" not in out |
| 59 | + assert "edges=0" in out |
| 60 | + |
| 61 | + |
| 62 | +def test_stream_multiple_groups(): |
| 63 | + buf = io.StringIO() |
| 64 | + enc = StreamEncoder(buf, "test") |
| 65 | + |
| 66 | + enc.write_symbol(Symbol(qualified_name="a", kind="function", score=1.0, provenance="x", distance=0)) |
| 67 | + enc.write_symbol(Symbol(qualified_name="b", kind="function", score=0.8, provenance="x", distance=1)) |
| 68 | + enc.write_symbol(Symbol(qualified_name="c", kind="function", score=0.6, provenance="x", distance=2)) |
| 69 | + enc.write_symbol(Symbol(qualified_name="d", kind="function", score=0.4, provenance="x", distance=5)) |
| 70 | + enc.close() |
| 71 | + |
| 72 | + out = buf.getvalue() |
| 73 | + assert "## targets\n" in out |
| 74 | + assert "## related\n" in out |
| 75 | + assert "## extended\n" in out |
| 76 | + assert "## distance_5\n" in out |
| 77 | + assert "sections=targets:1,related:1,extended:1,distance_5:1" in out |
| 78 | + |
| 79 | + |
| 80 | +def test_stream_skips_unknown_refs(): |
| 81 | + buf = io.StringIO() |
| 82 | + enc = StreamEncoder(buf, "test") |
| 83 | + |
| 84 | + enc.write_symbol(Symbol(qualified_name="a.A", kind="function", score=0.9, provenance="x", distance=0)) |
| 85 | + enc.write_edge(Edge(source="unknown.B", target="a.A", edge_type="calls")) |
| 86 | + enc.close() |
| 87 | + |
| 88 | + out = buf.getvalue() |
| 89 | + assert "calls" not in out |
| 90 | + assert "edges=0" in out |
| 91 | + |
| 92 | + |
| 93 | +def test_stream_incremental(): |
| 94 | + buf = io.StringIO() |
| 95 | + enc = StreamEncoder(buf, "test") |
| 96 | + |
| 97 | + # Header written immediately. |
| 98 | + assert buf.tell() > 0 |
| 99 | + pos_after_header = buf.tell() |
| 100 | + |
| 101 | + enc.write_symbol(Symbol(qualified_name="a.A", kind="function", score=0.9, provenance="x", distance=0)) |
| 102 | + assert buf.tell() > pos_after_header |
| 103 | + |
| 104 | + |
| 105 | +def test_stream_bare_ref(): |
| 106 | + buf = io.StringIO() |
| 107 | + enc = StreamEncoder(buf, "test", session=True) |
| 108 | + |
| 109 | + enc.write_bare_ref("pkg.Auth", 0) |
| 110 | + enc.write_symbol(Symbol(qualified_name="pkg.New", kind="function", score=0.85, provenance="lsp", distance=0)) |
| 111 | + enc.close() |
| 112 | + |
| 113 | + out = buf.getvalue() |
| 114 | + assert "session=true" in out |
| 115 | + assert "@0 # previously transmitted" in out |
| 116 | + assert "@1 fn pkg.New 0.85 lsp" in out |
0 commit comments