( payload: Pick<WireMapPayload, 'modules' | 'links'>, options: MapLayoutOptions )
| 299 | } |
| 300 | |
| 301 | export function buildMapLayout( |
| 302 | payload: Pick<WireMapPayload, 'modules' | 'links'>, |
| 303 | options: MapLayoutOptions |
| 304 | ): MapLayout { |
| 305 | const modules = payload.modules.filter((m) => options.includeTests || !m.test); |
| 306 | const present = new Set(modules.map((m) => m.id)); |
| 307 | // Islands come off the UNFILTERED link set: a module a hidden test module |
| 308 | // depends on is depended on, whatever this screen is currently showing. |
| 309 | const depended = new Set(payload.links.map((l) => l.target)); |
| 310 | const links = payload.links.filter((l) => present.has(l.source) && present.has(l.target)); |
| 311 | const minWeight = options.minWeight ?? (options.includeTests ? MIN_WEIGHT_WITH_TESTS : MIN_WEIGHT); |
| 312 | const layerGap = options.layerGap ?? LAYER_GAP; |
| 313 | const portPitch = options.portPitch ?? 0; |
| 314 | const directional = options.ports === 'directional'; |
| 315 | |
| 316 | const declaredLinks = links.filter((l) => l.declared > 0); |
| 317 | const useDeclared = |
| 318 | links.length > 0 && declaredLinks.length >= links.length * DECLARED_BASIS_COVERAGE; |
| 319 | const weightOf = (link: WireMapLink): number => (useDeclared ? link.declared : link.count); |
| 320 | const layeringLinks = useDeclared ? declaredLinks : links; |
| 321 | |
| 322 | // --- 2-cycle break, on the layering graph only --------------------------- |
| 323 | const byPair = new Map(layeringLinks.map((l) => [linkId(l), l])); |
| 324 | const acyclic: WireMapLink[] = []; |
| 325 | const mutual: MutualPair[] = []; |
| 326 | for (const link of layeringLinks) { |
| 327 | const back = byPair.get(linkId({ source: link.target, target: link.source })); |
| 328 | if (!back) { |
| 329 | acyclic.push(link); |
| 330 | continue; |
| 331 | } |
| 332 | const mine = weightOf(link); |
| 333 | const theirs = weightOf(back); |
| 334 | // Ties broken by id so two runs over one payload agree. |
| 335 | if (theirs > mine || (theirs === mine && link.source > link.target)) { |
| 336 | mutual.push({ forward: back, back: link }); |
| 337 | continue; |
| 338 | } |
| 339 | acyclic.push(link); |
| 340 | } |
| 341 | |
| 342 | // --- longest-path layering ---------------------------------------------- |
| 343 | const out = new Map<string, string[]>(modules.map((m) => [m.id, []])); |
| 344 | for (const link of acyclic) out.get(link.source)?.push(link.target); |
| 345 | for (const list of out.values()) list.sort(); |
| 346 | |
| 347 | const layer = new Map<string, number>(); |
| 348 | if (options.layering) { |
| 349 | for (const [id, value] of options.layering(modules.map((m) => m.id), acyclic)) layer.set(id, value); |
| 350 | for (const module of modules) if (!layer.has(module.id)) layer.set(module.id, 0); |
| 351 | } else { |
| 352 | for (const module of modules) longestPath(module.id, out, layer, new Set()); |
| 353 | } |
| 354 | |
| 355 | const layerCount = Math.max(1, ...[...layer.values()].map((v) => v + 1)); |
| 356 | const rows: string[][] = Array.from({ length: layerCount }, () => []); |
| 357 | for (const module of modules) rows[layer.get(module.id) ?? 0]!.push(module.id); |
| 358 | const given = options.order; |
no test coverage detected