@agent-wiki/mcp 0.21.5 → 0.21.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,84 @@
1
1
  import { describe, it, expect } from "vitest";
2
2
  import { parse } from "../parser.js";
3
3
  import { extractModel } from "../extractors.js";
4
- import { buildDb2TableLineage, parseReplacingPairs } from "../db2-table-lineage.js";
4
+ import { buildDb2TableLineage, parseReplacingPairs, parseSqlColumnBindings } from "../db2-table-lineage.js";
5
+ describe("parseSqlColumnBindings (#41 Phase A)", () => {
6
+ it("pairs INSERT INTO T (col-list) VALUES (host-list) positionally", () => {
7
+ expect(parseSqlColumnBindings("EXEC SQL INSERT INTO CUSTOMERS (ID, NAME, EMAIL) VALUES (:WS-ID, :WS-NAME, :WS-EMAIL) END-EXEC", "INSERT")).toEqual([
8
+ { column: "ID", hostVar: "WS-ID" },
9
+ { column: "NAME", hostVar: "WS-NAME" },
10
+ { column: "EMAIL", hostVar: "WS-EMAIL" },
11
+ ]);
12
+ });
13
+ it("returns empty for INSERT without column list (positional binding impossible)", () => {
14
+ expect(parseSqlColumnBindings("EXEC SQL INSERT INTO CUSTOMERS VALUES (:WS-ID, :WS-NAME) END-EXEC", "INSERT")).toEqual([]);
15
+ });
16
+ it("aborts INSERT binding when column count != value count (arity mismatch)", () => {
17
+ expect(parseSqlColumnBindings("EXEC SQL INSERT INTO CUSTOMERS (ID, NAME) VALUES (:WS-ID, :WS-NAME, :WS-EMAIL) END-EXEC", "INSERT")).toEqual([]);
18
+ });
19
+ it("skips non-host-var INSERT values (literals, NULL, function calls) but keeps the rest", () => {
20
+ expect(parseSqlColumnBindings("EXEC SQL INSERT INTO T (A, B, C) VALUES (:WS-A, 'literal', :WS-C) END-EXEC", "INSERT")).toEqual([
21
+ { column: "A", hostVar: "WS-A" },
22
+ { column: "C", hostVar: "WS-C" },
23
+ ]);
24
+ });
25
+ it("pairs SELECT col-list INTO host-list FROM... positionally", () => {
26
+ expect(parseSqlColumnBindings("EXEC SQL SELECT ID, NAME INTO :WS-ID, :WS-NAME FROM CUSTOMERS END-EXEC", "SELECT")).toEqual([
27
+ { column: "ID", hostVar: "WS-ID" },
28
+ { column: "NAME", hostVar: "WS-NAME" },
29
+ ]);
30
+ });
31
+ it("returns empty for SELECT without INTO (subquery, no host-var landing)", () => {
32
+ expect(parseSqlColumnBindings("EXEC SQL SELECT ID FROM CUSTOMERS WHERE NAME = :WS-NAME END-EXEC", "SELECT")).toEqual([]);
33
+ });
34
+ it("pairs UPDATE SET col = :host explicitly; WHERE host vars do NOT bind", () => {
35
+ expect(parseSqlColumnBindings("EXEC SQL UPDATE CUSTOMERS SET NAME = :WS-NAME, EMAIL = :WS-EMAIL WHERE ID = :WS-ID END-EXEC", "UPDATE")).toEqual([
36
+ { column: "NAME", hostVar: "WS-NAME" },
37
+ { column: "EMAIL", hostVar: "WS-EMAIL" },
38
+ ]);
39
+ });
40
+ it("UPDATE without WHERE: last assignment still binds (END-EXEC anchor)", () => {
41
+ // Without an END-EXEC anchor in the regex, the trailing token would
42
+ // leak into the last assignment string and the strict
43
+ // `col = :host` match would drop it. Locks the END-EXEC behavior.
44
+ expect(parseSqlColumnBindings("EXEC SQL UPDATE T SET A = :WS-A, B = :WS-B END-EXEC", "UPDATE")).toEqual([
45
+ { column: "A", hostVar: "WS-A" },
46
+ { column: "B", hostVar: "WS-B" },
47
+ ]);
48
+ });
49
+ it("UPDATE: arithmetic / function on the right-hand side drops that assignment", () => {
50
+ expect(parseSqlColumnBindings("EXEC SQL UPDATE T SET COUNT = COUNT + 1, NAME = :WS-NAME WHERE ID = :WS-ID END-EXEC", "UPDATE")).toEqual([
51
+ { column: "NAME", hostVar: "WS-NAME" },
52
+ ]);
53
+ });
54
+ it("does not split commas inside nested parens (function calls in column list)", () => {
55
+ // `COALESCE(A, B)` shouldn't shatter the column list into 4 entries
56
+ // and produce a wrong pairing. Pattern doesn't match (function isn't
57
+ // a plain column name) → bindings drop cleanly for that position;
58
+ // the bare-column / host-var positions still bind.
59
+ expect(parseSqlColumnBindings("EXEC SQL SELECT COALESCE(A, B), C INTO :WS-AB, :WS-C FROM T END-EXEC", "SELECT")).toEqual([
60
+ { column: "C", hostVar: "WS-C" },
61
+ ]);
62
+ });
63
+ it("returns empty for FETCH (column list lives on the DECLARE CURSOR, not here)", () => {
64
+ expect(parseSqlColumnBindings("EXEC SQL FETCH C1 INTO :WS-ID, :WS-NAME END-EXEC", "FETCH")).toEqual([]);
65
+ });
66
+ it("returns empty when operation is undefined", () => {
67
+ expect(parseSqlColumnBindings("anything", undefined)).toEqual([]);
68
+ });
69
+ it("handles multi-line SQL (newlines between clauses)", () => {
70
+ const sql = `EXEC SQL
71
+ INSERT INTO CUSTOMERS
72
+ (ID, NAME)
73
+ VALUES
74
+ (:WS-ID, :WS-NAME)
75
+ END-EXEC`;
76
+ expect(parseSqlColumnBindings(sql, "INSERT")).toEqual([
77
+ { column: "ID", hostVar: "WS-ID" },
78
+ { column: "NAME", hostVar: "WS-NAME" },
79
+ ]);
80
+ });
81
+ });
5
82
  describe("parseReplacingPairs (#37 Phase A)", () => {
6
83
  it("parses single-token form X BY Y", () => {
7
84
  expect(parseReplacingPairs(["X", "BY", "Y"])).toEqual([{ from: "X", to: "Y" }]);
@@ -655,6 +732,459 @@ describe("buildDb2TableLineage", () => {
655
732
  // … but the typo and missing-copybook breadcrumbs should still be there.
656
733
  expect(unresolved?.rationale).toContain("typos");
657
734
  });
735
+ describe("DB2 column-level host-var binding (#41 Phase A)", () => {
736
+ // SQL fixtures: every line stays inside COBOL fixed-format columns
737
+ // 8-72. `programWithSql` glues an array of pre-indented SQL lines
738
+ // into a PROCEDURE DIVISION around a fixed three-field WS layout.
739
+ function programWithSql(programId, sqlLines) {
740
+ const lines = sqlLines.map((l) => ` ${l}`).join("\n");
741
+ return `
742
+ IDENTIFICATION DIVISION.
743
+ PROGRAM-ID. ${programId}.
744
+ DATA DIVISION.
745
+ WORKING-STORAGE SECTION.
746
+ 01 WS-ID PIC X(10).
747
+ 01 WS-NAME PIC X(30).
748
+ 01 WS-EMAIL PIC X(50).
749
+ PROCEDURE DIVISION.
750
+ A000-MAIN SECTION.
751
+ A100-START.
752
+ ${lines}
753
+ STOP RUN.
754
+ `;
755
+ }
756
+ const reader = programWithSql("READER", [
757
+ "EXEC SQL SELECT ID, NAME INTO :WS-ID, :WS-NAME",
758
+ " FROM CUSTOMERS END-EXEC.",
759
+ ]);
760
+ it("attaches column to writer host vars from INSERT (col-list) VALUES (host-list)", () => {
761
+ const writer = programWithSql("WRITER", [
762
+ "EXEC SQL INSERT INTO CUSTOMERS (ID, NAME, EMAIL)",
763
+ " VALUES (:WS-ID, :WS-NAME, :WS-EMAIL) END-EXEC.",
764
+ ]);
765
+ const lineage = buildDb2TableLineage([
766
+ model(writer, "WRITER.cbl"),
767
+ model(reader, "READER.cbl"),
768
+ ]);
769
+ const entry = lineage.entries[0];
770
+ const byName = new Map(entry.writer.hostVars.map((hv) => [hv.name, hv]));
771
+ expect(byName.get("WS-ID")?.columns).toEqual(["ID"]);
772
+ expect(byName.get("WS-NAME")?.columns).toEqual(["NAME"]);
773
+ expect(byName.get("WS-EMAIL")?.columns).toEqual(["EMAIL"]);
774
+ });
775
+ it("attaches column to reader host vars from SELECT col-list INTO host-list", () => {
776
+ const writer = programWithSql("WRITER", [
777
+ "EXEC SQL INSERT INTO CUSTOMERS (ID, NAME)",
778
+ " VALUES (:WS-ID, :WS-NAME) END-EXEC.",
779
+ ]);
780
+ const lineage = buildDb2TableLineage([
781
+ model(writer, "WRITER.cbl"),
782
+ model(reader, "READER.cbl"),
783
+ ]);
784
+ const readerVars = lineage.entries[0].reader.hostVars;
785
+ expect(readerVars.find((hv) => hv.name === "WS-ID")?.columns).toEqual(["ID"]);
786
+ expect(readerVars.find((hv) => hv.name === "WS-NAME")?.columns).toEqual(["NAME"]);
787
+ });
788
+ it("attaches column on UPDATE SET pairs; WHERE-clause host vars stay unbound", () => {
789
+ const updater = programWithSql("UPDATER", [
790
+ "EXEC SQL UPDATE CUSTOMERS",
791
+ " SET NAME = :WS-NAME, EMAIL = :WS-EMAIL",
792
+ " WHERE ID = :WS-ID END-EXEC.",
793
+ ]);
794
+ const lineage = buildDb2TableLineage([
795
+ model(updater, "UPDATER.cbl"),
796
+ model(reader, "READER.cbl"),
797
+ ]);
798
+ const writerVars = lineage.entries[0].writer.hostVars;
799
+ const byName = new Map(writerVars.map((hv) => [hv.name, hv]));
800
+ expect(byName.get("WS-NAME")?.columns).toEqual(["NAME"]);
801
+ expect(byName.get("WS-EMAIL")?.columns).toEqual(["EMAIL"]);
802
+ // WHERE clause filter — no column binding.
803
+ expect(byName.get("WS-ID")?.columns).toEqual([]);
804
+ expect(byName.get("WS-ID")?.column).toBeUndefined();
805
+ // But the host var itself is still resolved (has a dataItem).
806
+ expect(byName.get("WS-ID")?.dataItem?.picture).toBe("X(10)");
807
+ });
808
+ it("falls through to no column when INSERT has no column list", () => {
809
+ const writer = programWithSql("WRITER", [
810
+ "EXEC SQL INSERT INTO CUSTOMERS",
811
+ " VALUES (:WS-ID, :WS-NAME, :WS-EMAIL) END-EXEC.",
812
+ ]);
813
+ const lineage = buildDb2TableLineage([
814
+ model(writer, "WRITER.cbl"),
815
+ model(reader, "READER.cbl"),
816
+ ]);
817
+ const writerVars = lineage.entries[0].writer.hostVars;
818
+ // Host vars still listed; just no column attached.
819
+ expect(writerVars.find((hv) => hv.name === "WS-ID")?.dataItem?.picture).toBe("X(10)");
820
+ expect(writerVars.every((hv) => hv.columns.length === 0)).toBe(true);
821
+ });
822
+ it("a later SQL ref can attach a column to a host var first seen unbound", () => {
823
+ // Two SQL blocks in the same program against the same table: the
824
+ // first uses a WHERE filter (no column binding), the second is an
825
+ // INSERT with a column list. The bump-merge logic attaches the
826
+ // column from the second block to the existing host-var entry.
827
+ const writer = programWithSql("WRITER", [
828
+ "EXEC SQL SELECT NAME FROM CUSTOMERS",
829
+ " WHERE ID = :WS-ID END-EXEC.",
830
+ "EXEC SQL INSERT INTO CUSTOMERS (ID, NAME)",
831
+ " VALUES (:WS-ID, :WS-NAME) END-EXEC.",
832
+ ]);
833
+ const lineage = buildDb2TableLineage([
834
+ model(writer, "WRITER.cbl"),
835
+ model(reader, "READER.cbl"),
836
+ ]);
837
+ const writerVars = lineage.entries[0].writer.hostVars;
838
+ expect(writerVars.find((hv) => hv.name === "WS-ID")?.columns).toEqual(["ID"]);
839
+ expect(writerVars.find((hv) => hv.name === "WS-NAME")?.columns).toEqual(["NAME"]);
840
+ });
841
+ it("aborts column binding on arity mismatch (3 cols, 2 hosts) — host vars still listed without column", () => {
842
+ // Caller wrote a buggy SQL: 3 columns, only 2 values. We refuse
843
+ // to guess a positional pairing for the surviving values, drop
844
+ // ALL bindings for that ref.
845
+ const writer = programWithSql("WRITER", [
846
+ "EXEC SQL INSERT INTO CUSTOMERS (ID, NAME, EMAIL)",
847
+ " VALUES (:WS-ID, :WS-NAME) END-EXEC.",
848
+ ]);
849
+ const lineage = buildDb2TableLineage([
850
+ model(writer, "WRITER.cbl"),
851
+ model(reader, "READER.cbl"),
852
+ ]);
853
+ const writerVars = lineage.entries[0].writer.hostVars;
854
+ expect(writerVars.every((hv) => hv.columns.length === 0)).toBe(true);
855
+ });
856
+ });
857
+ describe("DB2 cross-program column-level pairing (#41 Phase B)", () => {
858
+ // Reuses the column-binding writer / reader shapes from Phase A.
859
+ // The new assertion target is `entry.columnPairs` — each pair
860
+ // asserts `writerHostVar → column → readerHostVar` cross-program
861
+ // flow when both sides bind the same column.
862
+ function makeWriter(programId, sql) {
863
+ const lines = sql.map((l) => ` ${l}`).join("\n");
864
+ return `
865
+ IDENTIFICATION DIVISION.
866
+ PROGRAM-ID. ${programId}.
867
+ DATA DIVISION.
868
+ WORKING-STORAGE SECTION.
869
+ 01 WR-ID PIC X(10).
870
+ 01 WR-NAME PIC X(30).
871
+ 01 WR-EMAIL PIC X(50).
872
+ PROCEDURE DIVISION.
873
+ A000-MAIN SECTION.
874
+ A100-START.
875
+ ${lines}
876
+ STOP RUN.
877
+ `;
878
+ }
879
+ function makeReader(programId, sql) {
880
+ const lines = sql.map((l) => ` ${l}`).join("\n");
881
+ return `
882
+ IDENTIFICATION DIVISION.
883
+ PROGRAM-ID. ${programId}.
884
+ DATA DIVISION.
885
+ WORKING-STORAGE SECTION.
886
+ 01 RD-ID PIC X(10).
887
+ 01 RD-NAME PIC X(30).
888
+ PROCEDURE DIVISION.
889
+ A000-MAIN SECTION.
890
+ A100-START.
891
+ ${lines}
892
+ STOP RUN.
893
+ `;
894
+ }
895
+ it("emits a column pair per shared (writer-bound, reader-bound) column", () => {
896
+ const writer = makeWriter("WRITER", [
897
+ "EXEC SQL INSERT INTO CUSTOMERS (ID, NAME, EMAIL)",
898
+ " VALUES (:WR-ID, :WR-NAME, :WR-EMAIL) END-EXEC.",
899
+ ]);
900
+ const reader = makeReader("READER", [
901
+ "EXEC SQL SELECT ID, NAME INTO :RD-ID, :RD-NAME",
902
+ " FROM CUSTOMERS END-EXEC.",
903
+ ]);
904
+ const lineage = buildDb2TableLineage([
905
+ model(writer, "WRITER.cbl"),
906
+ model(reader, "READER.cbl"),
907
+ ]);
908
+ const entry = lineage.entries[0];
909
+ // EMAIL drops because the reader doesn't bind it; ID and NAME pair.
910
+ expect(entry.columnPairs).toEqual([
911
+ { column: "ID", writerHostVar: "WR-ID", readerHostVar: "RD-ID" },
912
+ { column: "NAME", writerHostVar: "WR-NAME", readerHostVar: "RD-NAME" },
913
+ ]);
914
+ expect(lineage.summary.columnPairs).toBe(2);
915
+ });
916
+ it("emits no column pairs when one side has no column bindings", () => {
917
+ // Writer uses INSERT-without-column-list → no bindings on writer side.
918
+ // Reader's INTO bindings exist but can't intersect.
919
+ const writer = makeWriter("WRITER", [
920
+ "EXEC SQL INSERT INTO CUSTOMERS",
921
+ " VALUES (:WR-ID, :WR-NAME, :WR-EMAIL) END-EXEC.",
922
+ ]);
923
+ const reader = makeReader("READER", [
924
+ "EXEC SQL SELECT ID, NAME INTO :RD-ID, :RD-NAME",
925
+ " FROM CUSTOMERS END-EXEC.",
926
+ ]);
927
+ const lineage = buildDb2TableLineage([
928
+ model(writer, "WRITER.cbl"),
929
+ model(reader, "READER.cbl"),
930
+ ]);
931
+ expect(lineage.entries[0].columnPairs).toEqual([]);
932
+ expect(lineage.summary.columnPairs).toBe(0);
933
+ });
934
+ it("emits no column pairs when both sides bind but on disjoint columns", () => {
935
+ const writer = makeWriter("WRITER", [
936
+ "EXEC SQL INSERT INTO CUSTOMERS (ID)",
937
+ " VALUES (:WR-ID) END-EXEC.",
938
+ ]);
939
+ const reader = makeReader("READER", [
940
+ "EXEC SQL SELECT NAME INTO :RD-NAME",
941
+ " FROM CUSTOMERS END-EXEC.",
942
+ ]);
943
+ const lineage = buildDb2TableLineage([
944
+ model(writer, "WRITER.cbl"),
945
+ model(reader, "READER.cbl"),
946
+ ]);
947
+ expect(lineage.entries[0].columnPairs).toEqual([]);
948
+ });
949
+ it("handles UPDATE writer + SELECT INTO reader (SET-clause columns only)", () => {
950
+ // The writer's WHERE clause binds :WR-ID against ID for filtering,
951
+ // not column-writing, so :WR-ID has no column. The SET pairs do
952
+ // bind. The reader's SELECT INTO binds both columns.
953
+ const writer = makeWriter("WRITER", [
954
+ "EXEC SQL UPDATE CUSTOMERS",
955
+ " SET NAME = :WR-NAME, EMAIL = :WR-EMAIL",
956
+ " WHERE ID = :WR-ID END-EXEC.",
957
+ ]);
958
+ const reader = makeReader("READER", [
959
+ "EXEC SQL SELECT NAME, EMAIL INTO :RD-NAME, :RD-EMAIL",
960
+ " FROM CUSTOMERS END-EXEC.",
961
+ ]);
962
+ // RD-EMAIL needs a declaration matching the reader fixture.
963
+ const reader2 = `
964
+ IDENTIFICATION DIVISION.
965
+ PROGRAM-ID. READER.
966
+ DATA DIVISION.
967
+ WORKING-STORAGE SECTION.
968
+ 01 RD-NAME PIC X(30).
969
+ 01 RD-EMAIL PIC X(50).
970
+ PROCEDURE DIVISION.
971
+ A000-MAIN SECTION.
972
+ A100-START.
973
+ EXEC SQL SELECT NAME, EMAIL INTO :RD-NAME, :RD-EMAIL
974
+ FROM CUSTOMERS END-EXEC.
975
+ STOP RUN.
976
+ `;
977
+ // Ignore reader from the helper (didn't have RD-EMAIL), use reader2.
978
+ void reader;
979
+ const lineage = buildDb2TableLineage([
980
+ model(writer, "WRITER.cbl"),
981
+ model(reader2, "READER.cbl"),
982
+ ]);
983
+ const entry = lineage.entries[0];
984
+ // WR-ID drops (no column, WHERE filter); NAME and EMAIL pair.
985
+ expect(entry.columnPairs).toEqual([
986
+ { column: "EMAIL", writerHostVar: "WR-EMAIL", readerHostVar: "RD-EMAIL" },
987
+ { column: "NAME", writerHostVar: "WR-NAME", readerHostVar: "RD-NAME" },
988
+ ]);
989
+ });
990
+ it("Cartesian intersection emits one pair per (writer-side, reader-side) host var when both bind the same column", () => {
991
+ // Writer has two host vars binding NAME via different SQL refs:
992
+ // INSERT NAME = :WR-NAME and UPDATE SET NAME = :WR-EMAIL. The bump
993
+ // merge dedupes per-host-var-name (not per-column), so both
994
+ // bindings survive. The reader has one host var on NAME. The
995
+ // Cartesian intersection therefore emits two pairs — both writer
996
+ // host vars feed the same column that the reader receives from.
997
+ // This is the documented behavior; downstream dedup is the
998
+ // consumer's call.
999
+ const writer = makeWriter("WRITER", [
1000
+ "EXEC SQL INSERT INTO CUSTOMERS (NAME)",
1001
+ " VALUES (:WR-NAME) END-EXEC.",
1002
+ "EXEC SQL UPDATE CUSTOMERS SET NAME = :WR-EMAIL",
1003
+ " WHERE ID = '1' END-EXEC.",
1004
+ ]);
1005
+ const reader = makeReader("READER", [
1006
+ "EXEC SQL SELECT NAME INTO :RD-NAME",
1007
+ " FROM CUSTOMERS END-EXEC.",
1008
+ ]);
1009
+ const lineage = buildDb2TableLineage([
1010
+ model(writer, "WRITER.cbl"),
1011
+ model(reader, "READER.cbl"),
1012
+ ]);
1013
+ const pairs = lineage.entries[0].columnPairs;
1014
+ // Two writer host vars × one reader host var on the same column → 2 pairs.
1015
+ // Sorted by writerHostVar.
1016
+ expect(pairs).toEqual([
1017
+ { column: "NAME", writerHostVar: "WR-EMAIL", readerHostVar: "RD-NAME" },
1018
+ { column: "NAME", writerHostVar: "WR-NAME", readerHostVar: "RD-NAME" },
1019
+ ]);
1020
+ });
1021
+ it("regression: column pairs are deterministically sorted", () => {
1022
+ // Two columns bound on both sides; the sort must be stable regardless
1023
+ // of host-var iteration order. Lock against future Map / Set order
1024
+ // changes by asserting full equality.
1025
+ const writer = makeWriter("WRITER", [
1026
+ "EXEC SQL INSERT INTO CUSTOMERS (NAME, ID)",
1027
+ " VALUES (:WR-NAME, :WR-ID) END-EXEC.",
1028
+ ]);
1029
+ const reader = makeReader("READER", [
1030
+ "EXEC SQL SELECT NAME, ID INTO :RD-NAME, :RD-ID",
1031
+ " FROM CUSTOMERS END-EXEC.",
1032
+ ]);
1033
+ const lineage = buildDb2TableLineage([
1034
+ model(writer, "WRITER.cbl"),
1035
+ model(reader, "READER.cbl"),
1036
+ ]);
1037
+ // Sort key: column → writerHostVar → readerHostVar (alphabetical).
1038
+ expect(lineage.entries[0].columnPairs).toEqual([
1039
+ { column: "ID", writerHostVar: "WR-ID", readerHostVar: "RD-ID" },
1040
+ { column: "NAME", writerHostVar: "WR-NAME", readerHostVar: "RD-NAME" },
1041
+ ]);
1042
+ });
1043
+ it("multi-column binding on a single host var: each column produces its own pair (Codex review fix)", () => {
1044
+ // The Codex review on #43 caught that the pre-fix code stored only
1045
+ // the first column per host var, silently dropping later bindings
1046
+ // and the Phase B intersection along with them. Concrete case:
1047
+ // INSERT binds :WR-ID to BOTH ID and ALT_ID; a reader selecting
1048
+ // ALT_ID INTO :RD-ALT-ID must still pair through the second
1049
+ // column.
1050
+ const writer = makeWriter("WRITER", [
1051
+ "EXEC SQL INSERT INTO CUSTOMERS (ID, ALT_ID)",
1052
+ " VALUES (:WR-ID, :WR-ID) END-EXEC.",
1053
+ ]);
1054
+ const reader = `
1055
+ IDENTIFICATION DIVISION.
1056
+ PROGRAM-ID. READER.
1057
+ DATA DIVISION.
1058
+ WORKING-STORAGE SECTION.
1059
+ 01 RD-ID PIC X(10).
1060
+ 01 RD-ALT-ID PIC X(10).
1061
+ PROCEDURE DIVISION.
1062
+ A000-MAIN SECTION.
1063
+ A100-START.
1064
+ EXEC SQL SELECT ALT_ID INTO :RD-ALT-ID
1065
+ FROM CUSTOMERS END-EXEC.
1066
+ STOP RUN.
1067
+ `;
1068
+ const lineage = buildDb2TableLineage([
1069
+ model(writer, "WRITER.cbl"),
1070
+ model(reader, "READER.cbl"),
1071
+ ]);
1072
+ // Writer's :WR-ID carries both columns.
1073
+ const wrId = lineage.entries[0].writer.hostVars.find((hv) => hv.name === "WR-ID");
1074
+ expect(wrId?.columns).toEqual(["ALT_ID", "ID"]); // sorted for byte-stable artifact
1075
+ // `column` is the #41 Phase A back-compat alias — first-observed
1076
+ // in the SQL, not alphabetically first. INSERT lists ID before
1077
+ // ALT_ID, so column === "ID".
1078
+ expect(wrId?.column).toBe("ID");
1079
+ // The Phase B intersection now produces the ALT_ID flow that the
1080
+ // pre-fix code silently dropped.
1081
+ expect(lineage.entries[0].columnPairs).toEqual([
1082
+ { column: "ALT_ID", writerHostVar: "WR-ID", readerHostVar: "RD-ALT-ID" },
1083
+ ]);
1084
+ });
1085
+ it("multi-table SQL refs skip column binding to avoid false cross-table pairs (Codex review fix)", () => {
1086
+ // The Codex review on #43 caught that a SELECT touching multiple
1087
+ // tables (via subqueries, joins, or WHERE EXISTS) had the same
1088
+ // column bindings attached to every table — so a writer to T2
1089
+ // would falsely pair through a reader that actually reads from
1090
+ // T1. Fix: when ref.tables.length > 1, parseSqlColumnBindings is
1091
+ // skipped and no host var carries a column. The Phase B
1092
+ // intersection therefore emits no false pairs.
1093
+ const writer = makeWriter("WRITER", [
1094
+ "EXEC SQL INSERT INTO ORDERS (ID)",
1095
+ " VALUES (:WR-ID) END-EXEC.",
1096
+ ]);
1097
+ // Reader's SELECT mentions both ORDERS (FROM) and CUSTOMERS
1098
+ // (in a subquery). extractSqlTableNames returns both tables.
1099
+ const reader = `
1100
+ IDENTIFICATION DIVISION.
1101
+ PROGRAM-ID. READER.
1102
+ DATA DIVISION.
1103
+ WORKING-STORAGE SECTION.
1104
+ 01 RD-ID PIC X(10).
1105
+ PROCEDURE DIVISION.
1106
+ A000-MAIN SECTION.
1107
+ A100-START.
1108
+ EXEC SQL SELECT ID INTO :RD-ID FROM CUSTOMERS
1109
+ WHERE ID IN (SELECT CUST_ID FROM ORDERS) END-EXEC.
1110
+ STOP RUN.
1111
+ `;
1112
+ const lineage = buildDb2TableLineage([
1113
+ model(writer, "WRITER.cbl"),
1114
+ model(reader, "READER.cbl"),
1115
+ ]);
1116
+ // ORDERS is the shared table → entry exists.
1117
+ const ordersEntry = lineage.entries.find((e) => e.table === "ORDERS");
1118
+ expect(ordersEntry).toBeDefined();
1119
+ // Reader's :RD-ID has NO column binding because the multi-table
1120
+ // ref triggered the guard. So no false pair through ORDERS.ID.
1121
+ const rdId = ordersEntry.reader.hostVars.find((hv) => hv.name === "RD-ID");
1122
+ expect(rdId?.columns).toEqual([]);
1123
+ expect(ordersEntry.columnPairs).toEqual([]);
1124
+ });
1125
+ it("unresolved host vars do not produce column-flow pairs (Codex review #3 fix)", () => {
1126
+ // Writer references :WS-MISSING (not declared anywhere) but the
1127
+ // SQL DOES bind it to NAME. Reader's :RD-NAME is fully resolved.
1128
+ // Pre-fix Phase B emitted `WS-MISSING → NAME → RD-NAME` despite
1129
+ // WS-MISSING already being diagnosed as host-var-unresolved.
1130
+ // Now: unresolved host vars are gated out of `computeColumnPairs`
1131
+ // (no dataItem → skipped), so the pair drops cleanly. The
1132
+ // host-var-unresolved diagnostic still fires separately.
1133
+ const writer = `
1134
+ IDENTIFICATION DIVISION.
1135
+ PROGRAM-ID. WRITER.
1136
+ DATA DIVISION.
1137
+ WORKING-STORAGE SECTION.
1138
+ 01 WR-OTHER PIC X(10).
1139
+ PROCEDURE DIVISION.
1140
+ A000-MAIN SECTION.
1141
+ A100-START.
1142
+ EXEC SQL INSERT INTO CUSTOMERS (NAME)
1143
+ VALUES (:WS-MISSING) END-EXEC.
1144
+ STOP RUN.
1145
+ `;
1146
+ const reader = makeReader("READER", [
1147
+ "EXEC SQL SELECT NAME INTO :RD-NAME",
1148
+ " FROM CUSTOMERS END-EXEC.",
1149
+ ]);
1150
+ const lineage = buildDb2TableLineage([
1151
+ model(writer, "WRITER.cbl"),
1152
+ model(reader, "READER.cbl"),
1153
+ ]);
1154
+ const entry = lineage.entries[0];
1155
+ // The unresolved host var is still in the writer's hostVars list
1156
+ // (with empty dataItem) so the cell can render `(?)`, but the
1157
+ // column-flow intersection skips it.
1158
+ expect(entry.columnPairs).toEqual([]);
1159
+ // And the host-var-unresolved diagnostic is intact.
1160
+ expect(lineage.diagnostics.some((d) => d.kind === "host-var-unresolved" && d.hostVar === "WS-MISSING")).toBe(true);
1161
+ });
1162
+ it("summary.columnPairs sums across multiple writer-reader entries", () => {
1163
+ // One writer, two readers — produces two entries (one per writer-
1164
+ // reader pair). Each entry contributes its own columnPairs; the
1165
+ // summary counter is the sum.
1166
+ const writer = makeWriter("WRITER", [
1167
+ "EXEC SQL INSERT INTO CUSTOMERS (ID, NAME)",
1168
+ " VALUES (:WR-ID, :WR-NAME) END-EXEC.",
1169
+ ]);
1170
+ const reader1 = makeReader("READER1", [
1171
+ "EXEC SQL SELECT ID, NAME INTO :RD-ID, :RD-NAME",
1172
+ " FROM CUSTOMERS END-EXEC.",
1173
+ ]);
1174
+ const reader2 = makeReader("READER2", [
1175
+ "EXEC SQL SELECT NAME INTO :RD-NAME",
1176
+ " FROM CUSTOMERS END-EXEC.",
1177
+ ]);
1178
+ const lineage = buildDb2TableLineage([
1179
+ model(writer, "WRITER.cbl"),
1180
+ model(reader1, "READER1.cbl"),
1181
+ model(reader2, "READER2.cbl"),
1182
+ ]);
1183
+ // Two entries: (WRITER, READER1) → 2 pairs, (WRITER, READER2) → 1 pair.
1184
+ expect(lineage.entries.length).toBe(2);
1185
+ expect(lineage.summary.columnPairs).toBe(3);
1186
+ });
1187
+ });
658
1188
  describe("REPLACING-aware host-var resolution (#37 Phase A)", () => {
659
1189
  const customerCopybook = `
660
1190
  01 CUSTOMER-REC.