|
| 1 | +import { expect, test, describe } from "@jest/globals" |
| 2 | +import { propagateChainTypes } from "./chainType.js" |
| 3 | +import type { SectionContent } from "~/config/sidebar.js" |
| 4 | + |
| 5 | +describe("propagateChainTypes", () => { |
| 6 | + test("children without chainTypes inherit from parent", () => { |
| 7 | + const input: SectionContent[] = [ |
| 8 | + { |
| 9 | + title: "API Reference", |
| 10 | + url: "ccip/api-reference/evm", |
| 11 | + chainTypes: ["evm"], |
| 12 | + children: [ |
| 13 | + { title: "Messages", url: "ccip/api-reference/evm/messages" }, |
| 14 | + { title: "Router", url: "ccip/api-reference/evm/router" }, |
| 15 | + ], |
| 16 | + }, |
| 17 | + ] |
| 18 | + |
| 19 | + const result = propagateChainTypes(input) |
| 20 | + |
| 21 | + expect(result[0].children![0].chainTypes).toEqual(["evm"]) |
| 22 | + expect(result[0].children![1].chainTypes).toEqual(["evm"]) |
| 23 | + }) |
| 24 | + |
| 25 | + test("children with explicit chainTypes are not overridden", () => { |
| 26 | + const input: SectionContent[] = [ |
| 27 | + { |
| 28 | + title: "Parent", |
| 29 | + url: "parent", |
| 30 | + chainTypes: ["evm"], |
| 31 | + children: [ |
| 32 | + { title: "Child1", url: "child1" }, // Should inherit |
| 33 | + { title: "Child2", url: "child2", chainTypes: ["solana", "aptos"] }, // Should keep its own |
| 34 | + ], |
| 35 | + }, |
| 36 | + ] |
| 37 | + |
| 38 | + const result = propagateChainTypes(input) |
| 39 | + |
| 40 | + expect(result[0].children![0].chainTypes).toEqual(["evm"]) |
| 41 | + expect(result[0].children![1].chainTypes).toEqual(["solana", "aptos"]) |
| 42 | + }) |
| 43 | + |
| 44 | + test("nested children (grandchildren) inherit correctly", () => { |
| 45 | + const input: SectionContent[] = [ |
| 46 | + { |
| 47 | + title: "Grandparent", |
| 48 | + url: "grandparent", |
| 49 | + chainTypes: ["evm"], |
| 50 | + children: [ |
| 51 | + { |
| 52 | + title: "Parent", |
| 53 | + url: "parent", |
| 54 | + children: [ |
| 55 | + { title: "Child", url: "child" }, |
| 56 | + { title: "AnotherChild", url: "another-child" }, |
| 57 | + ], |
| 58 | + }, |
| 59 | + ], |
| 60 | + }, |
| 61 | + ] |
| 62 | + |
| 63 | + const result = propagateChainTypes(input) |
| 64 | + |
| 65 | + // Parent inherits from grandparent |
| 66 | + expect(result[0].children![0].chainTypes).toEqual(["evm"]) |
| 67 | + // Children inherit from parent (which inherited from grandparent) |
| 68 | + expect(result[0].children![0].children![0].chainTypes).toEqual(["evm"]) |
| 69 | + expect(result[0].children![0].children![1].chainTypes).toEqual(["evm"]) |
| 70 | + }) |
| 71 | + |
| 72 | + test("parent with chainTypes, intermediate child without, grandchild should inherit from parent", () => { |
| 73 | + const input: SectionContent[] = [ |
| 74 | + { |
| 75 | + title: "API Reference", |
| 76 | + url: "api", |
| 77 | + chainTypes: ["solana"], |
| 78 | + children: [ |
| 79 | + { |
| 80 | + title: "v1.6.0", |
| 81 | + url: "v1.6.0", |
| 82 | + // No chainTypes - should inherit solana |
| 83 | + children: [ |
| 84 | + { title: "Messages", url: "messages" }, // Should inherit solana |
| 85 | + ], |
| 86 | + }, |
| 87 | + ], |
| 88 | + }, |
| 89 | + ] |
| 90 | + |
| 91 | + const result = propagateChainTypes(input) |
| 92 | + |
| 93 | + expect(result[0].children![0].chainTypes).toEqual(["solana"]) |
| 94 | + expect(result[0].children![0].children![0].chainTypes).toEqual(["solana"]) |
| 95 | + }) |
| 96 | + |
| 97 | + test("universal items (no chainTypes) remain universal", () => { |
| 98 | + const input: SectionContent[] = [ |
| 99 | + { |
| 100 | + title: "Overview", |
| 101 | + url: "overview", |
| 102 | + // No chainTypes - should remain undefined (universal) |
| 103 | + }, |
| 104 | + ] |
| 105 | + |
| 106 | + const result = propagateChainTypes(input) |
| 107 | + |
| 108 | + expect(result[0].chainTypes).toBeUndefined() |
| 109 | + }) |
| 110 | + |
| 111 | + test("universal parent with chain-specific children", () => { |
| 112 | + const input: SectionContent[] = [ |
| 113 | + { |
| 114 | + title: "Universal Parent", |
| 115 | + url: "parent", |
| 116 | + // No chainTypes |
| 117 | + children: [ |
| 118 | + { title: "EVM Child", url: "evm-child", chainTypes: ["evm"] }, |
| 119 | + { title: "Solana Child", url: "solana-child", chainTypes: ["solana"] }, |
| 120 | + ], |
| 121 | + }, |
| 122 | + ] |
| 123 | + |
| 124 | + const result = propagateChainTypes(input) |
| 125 | + |
| 126 | + // Parent remains universal |
| 127 | + expect(result[0].chainTypes).toBeUndefined() |
| 128 | + // Children keep their explicit chainTypes |
| 129 | + expect(result[0].children![0].chainTypes).toEqual(["evm"]) |
| 130 | + expect(result[0].children![1].chainTypes).toEqual(["solana"]) |
| 131 | + }) |
| 132 | + |
| 133 | + test("empty array of contents returns empty array", () => { |
| 134 | + const input: SectionContent[] = [] |
| 135 | + const result = propagateChainTypes(input) |
| 136 | + expect(result).toEqual([]) |
| 137 | + }) |
| 138 | + |
| 139 | + test("items without children are handled correctly", () => { |
| 140 | + const input: SectionContent[] = [ |
| 141 | + { title: "Item1", url: "item1", chainTypes: ["evm"] }, |
| 142 | + { title: "Item2", url: "item2", chainTypes: ["solana"] }, |
| 143 | + ] |
| 144 | + |
| 145 | + const result = propagateChainTypes(input) |
| 146 | + |
| 147 | + expect(result[0].chainTypes).toEqual(["evm"]) |
| 148 | + expect(result[1].chainTypes).toEqual(["solana"]) |
| 149 | + }) |
| 150 | + |
| 151 | + test("multiple chain types are preserved", () => { |
| 152 | + const input: SectionContent[] = [ |
| 153 | + { |
| 154 | + title: "Multi-chain Parent", |
| 155 | + url: "parent", |
| 156 | + chainTypes: ["evm", "solana", "aptos"], |
| 157 | + children: [{ title: "Child", url: "child" }], |
| 158 | + }, |
| 159 | + ] |
| 160 | + |
| 161 | + const result = propagateChainTypes(input) |
| 162 | + |
| 163 | + expect(result[0].children![0].chainTypes).toEqual(["evm", "solana", "aptos"]) |
| 164 | + }) |
| 165 | + |
| 166 | + test("does not mutate original input", () => { |
| 167 | + const input: SectionContent[] = [ |
| 168 | + { |
| 169 | + title: "Parent", |
| 170 | + url: "parent", |
| 171 | + chainTypes: ["evm"], |
| 172 | + children: [{ title: "Child", url: "child" }], |
| 173 | + }, |
| 174 | + ] |
| 175 | + |
| 176 | + const originalChildChainTypes = input[0].children![0].chainTypes |
| 177 | + |
| 178 | + propagateChainTypes(input) |
| 179 | + |
| 180 | + // Original should not have been mutated |
| 181 | + expect(input[0].children![0].chainTypes).toBe(originalChildChainTypes) |
| 182 | + }) |
| 183 | + |
| 184 | + test("real-world scenario: CCIP API Reference structure", () => { |
| 185 | + // Simulates the actual CCIP sidebar structure |
| 186 | + const input: SectionContent[] = [ |
| 187 | + { |
| 188 | + title: "Tools and Resources", |
| 189 | + url: "ccip/tools-resources", |
| 190 | + children: [ |
| 191 | + { |
| 192 | + title: "API Reference", |
| 193 | + url: "ccip/api-reference/svm", |
| 194 | + chainTypes: ["solana"], |
| 195 | + children: [ |
| 196 | + { |
| 197 | + title: "v1.6.0", |
| 198 | + url: "ccip/api-reference/svm/v1.6.0", |
| 199 | + isCollapsible: true, |
| 200 | + children: [ |
| 201 | + { title: "Messages", url: "ccip/api-reference/svm/v1.6.0/messages" }, |
| 202 | + { title: "Router", url: "ccip/api-reference/svm/v1.6.0/router" }, |
| 203 | + { title: "Receiver", url: "ccip/api-reference/svm/v1.6.0/receiver" }, |
| 204 | + ], |
| 205 | + }, |
| 206 | + ], |
| 207 | + }, |
| 208 | + ], |
| 209 | + }, |
| 210 | + ] |
| 211 | + |
| 212 | + const result = propagateChainTypes(input) |
| 213 | + |
| 214 | + // Top-level has no chainTypes (universal) |
| 215 | + expect(result[0].chainTypes).toBeUndefined() |
| 216 | + |
| 217 | + // API Reference has solana |
| 218 | + expect(result[0].children![0].chainTypes).toEqual(["solana"]) |
| 219 | + |
| 220 | + // v1.6.0 inherits solana |
| 221 | + expect(result[0].children![0].children![0].chainTypes).toEqual(["solana"]) |
| 222 | + |
| 223 | + // All API endpoints inherit solana |
| 224 | + const endpoints = result[0].children![0].children![0].children! |
| 225 | + expect(endpoints[0].chainTypes).toEqual(["solana"]) // Messages |
| 226 | + expect(endpoints[1].chainTypes).toEqual(["solana"]) // Router |
| 227 | + expect(endpoints[2].chainTypes).toEqual(["solana"]) // Receiver |
| 228 | + }) |
| 229 | +}) |
0 commit comments