import Foundation import HelderCore import Testing @Suite struct SchemaHighlightTests { private let model = SchemaFixtures.contractModel private func highlight(hovered: String? = nil, selected: String? = nil, column: String? = nil, query: String = "") -> SchemaHighlight { SchemaHighlight(model: model, hovered: hovered, selected: selected, hoveredColumn: column, query: query) } private func ref(_ table: String, _ column: String) -> SchemaColumnRef { SchemaColumnRef(table: table, column: column) } @Test func nothingFocusedHighlightsNothing() { let h = highlight() #expect(h.focus == nil) #expect(h.activeEdges.isEmpty) #expect(h.relatedTables.isEmpty) #expect(h.dimmedTables.isEmpty) #expect(h.highlightedColumns.isEmpty) #expect(!h.linesRecede) #expect(h.opacity(of: "users") == 1) } @Test func hoveringATableLightsEveryLineInAndOut() { let h = highlight(hovered: "conversations") #expect(h.activeEdges == ["conversations.user_id", "messages.conversation_id"]) #expect(h.relatedTables == ["conversations", "users", "messages"]) #expect(h.dimmedTables.isEmpty) #expect(h.highlightedColumns == [ref("conversations", "user_id"), ref("users", "id"), ref("messages", "conversation_id"), ref("conversations", "id")]) #expect(h.linesRecede) } @Test func unrelatedTablesDim() { let h = highlight(hovered: "users") #expect(h.activeEdges == ["conversations.user_id"]) #expect(h.relatedTables == ["users", "conversations"]) #expect(h.dimmedTables == ["messages"]) #expect(h.opacity(of: "messages") == 0.3) #expect(h.isHighlighted(table: "users", column: "id")) #expect(!h.isHighlighted(table: "conversations", column: "id")) } @Test func aHoveredKeyNarrowsToItsOneRelationship() { let h = highlight(hovered: "conversations", column: "conversations.user_id") #expect(h.activeEdges == ["conversations.user_id"]) #expect(h.relatedTables == ["conversations", "users"]) #expect(h.dimmedTables == ["messages"]) } @Test func theHoverWinsOverTheSelection() { #expect(highlight(selected: "users").focus == "users") let h = highlight(hovered: "messages", selected: "users") #expect(h.focus == "messages") #expect(h.dimmedTables == ["users"]) } @Test func aTableWithoutLinesKeepsOnlyItself() { let lonely = SchemaFixtures.model(["a", "b"], edges: []) let h = SchemaHighlight(model: lonely, hovered: "a", selected: nil, hoveredColumn: nil, query: "") #expect(h.relatedTables == ["a"]) #expect(h.dimmedTables == ["b"]) } @Test func searchDimsTheRestOnlyWithoutFocus() { let h = highlight(query: " CON ") #expect(h.searchMatches == ["conversations"]) #expect(h.dimmedTables == ["messages", "users"]) #expect(h.linesRecede) let focused = highlight(hovered: "users", query: "con") #expect(focused.searchMatches == ["conversations"]) #expect(focused.dimmedTables == ["messages"]) } @Test func activeLinesAreDrawnLast() { let h = highlight(hovered: "users") #expect(h.drawingOrder(model.edges).map(\.id) == ["messages.conversation_id", "conversations.user_id"]) } @Test func theTableListFiltersBySubstring() { #expect(SchemaSearch.filter(model, query: "S").map(\.name) == ["conversations", "messages", "users"]) #expect(SchemaSearch.filter(model, query: "mess").map(\.name) == ["messages"]) #expect(SchemaSearch.filter(model, query: " ").count == 3) #expect(SchemaSearch.listLabel(model, query: "") == "[ TABLES ] 3") #expect(SchemaSearch.listLabel(model, query: "user") == "[ TABLES ] 1 of 3") } }