LCOV - code coverage report
Current view: top level - src/platform/navigation - preservedQueryTracker.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 41.2 % 34 14
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { LocationQuery, Router } from 'vue-router'
       2              : 
       3              : import {
       4              :   capturePreservedQuery,
       5              :   clearPreservedQuery,
       6              :   hydratePreservedQuery
       7              : } from '@/platform/navigation/preservedQueryManager'
       8              : 
       9              : export interface PreservedQueryDefinition {
      10              :   namespace: string
      11              :   keys: string[]
      12              :   requiredKey?: string
      13              :   /**
      14              :    * When set, keys present in the query are removed from the client-side URL
      15              :    * before navigation completes. Later guards, afterEach hooks, and views must
      16              :    * read a strip-marked key from the preserved-query stash instead of
      17              :    * route.query or fullPath. Because the stash is the only carrier after
      18              :    * stripping, captures for the namespace merge into the existing stash and an
      19              :    * explicitly empty value clears the stashed key; non-strip namespaces keep
      20              :    * replace-on-capture semantics.
      21              :    */
      22              :   stripAfterCapture?: boolean
      23              :   /**
      24              :    * When set, a key present as a repeated (array) value, or present with no
      25              :    * value at all (`?key`, which the router reads as `null`, in a single
      26              :    * value or inside an array), is captured as an ambiguous marker instead of
      27              :    * silently picking one: repeated values join with `,`; a lone `null`
      28              :    * becomes a bare `,`. Both are strings outside any real id's charset, so
      29              :    * they read back as invalid rather than resolving to one of several
      30              :    * colliding values or to "absent" (`capturePreservedQuery` itself drops a
      31              :    * null/empty value as not worth capturing).
      32              :    */
      33              :   rejectRepeated?: true
      34              :   /** With `rejectRepeated`, an empty value (`?key=`) is captured as `,`. */
      35              :   markEmpty?: true
      36              : }
      37              : 
      38            0 : function withAmbiguityMarked(
      39              :   query: LocationQuery,
      40              :   keys: string[],
      41              :   markEmpty: boolean
      42              : ): LocationQuery {
      43            0 :   const result = { ...query }
      44            0 :   for (const key of keys) {
      45            0 :     const value = result[key]
      46            0 :     const values = Array.isArray(value) ? value : [value]
      47            0 :     const isRepeated = values.length > 1
      48            0 :     const hasBareEntry =
      49              :       values.includes(null) || (markEmpty && values.includes(''))
      50            0 :     if (!isRepeated && !hasBareEntry) continue
      51            0 :     result[key] = values.map((entry) => entry ?? '').join(',') || ','
      52              :   }
      53            0 :   return result
      54              : }
      55              : 
      56            0 : function hasValue(value: LocationQuery[string] | undefined): boolean {
      57            0 :   return (Array.isArray(value) ? value : [value]).some(Boolean)
      58              : }
      59              : 
      60         2356 : export const installPreservedQueryTracker = (
      61              :   router: Router,
      62              :   definitions: PreservedQueryDefinition[]
      63              : ) => {
      64         2355 :   router.beforeEach((to) => {
      65         3983 :     const queryKeys = new Set(Object.keys(to.query))
      66         3983 :     const keysToStrip = new Set<string>()
      67              : 
      68         3983 :     definitions.forEach(
      69              :       ({
      70              :         namespace,
      71              :         keys,
      72              :         requiredKey,
      73              :         stripAfterCapture,
      74              :         rejectRepeated,
      75              :         markEmpty
      76              :       }) => {
      77        59745 :         hydratePreservedQuery(namespace)
      78        59745 :         const presentKeys = keys.filter((key) => queryKeys.has(key))
      79        59745 :         if (presentKeys.length === 0) return
      80        59745 :         if (requiredKey && !queryKeys.has(requiredKey)) {
      81            0 :           clearPreservedQuery(namespace)
      82            0 :           return
      83              :         }
      84        59745 :         const captureQuery = rejectRepeated
      85              :           ? withAmbiguityMarked(to.query, keys, markEmpty === true)
      86              :           : to.query
      87        59745 :         if (
      88              :           rejectRepeated &&
      89              :           !presentKeys.some((key) => hasValue(captureQuery[key]))
      90              :         ) {
      91            0 :           clearPreservedQuery(namespace)
      92            0 :           return
      93              :         }
      94            4 :         capturePreservedQuery(namespace, captureQuery, keys, {
      95              :           merge: stripAfterCapture
      96              :         })
      97          105 :         if (stripAfterCapture) {
      98            0 :           presentKeys.forEach((key) => keysToStrip.add(key))
      99              :         }
     100              :       }
     101              :     )
     102              : 
     103         3983 :     if (keysToStrip.size === 0) return
     104              : 
     105            0 :     const cleanedQuery = { ...to.query }
     106            0 :     keysToStrip.forEach((key) => delete cleanedQuery[key])
     107            0 :     return { path: to.path, query: cleanedQuery, hash: to.hash }
     108              :   })
     109              : }
        

Generated by: LCOV version 2.0-1