LCOV - code coverage report
Current view: top level - src/platform/workspace/composables - useDeploymentRows.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 23.7 % 59 14
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type { WorkspaceDeployment } from '@/platform/workspace/api/workspaceApi'
       2              : import { partition } from 'es-toolkit'
       3              : import { storeToRefs } from 'pinia'
       4              : import { computed } from 'vue'
       5              : import { useI18n } from 'vue-i18n'
       6              : 
       7              : import { useFeatureFlags } from '@/composables/useFeatureFlags'
       8              : import type { DeploymentCompatibilityMark } from '@/platform/workspace/composables/useDeploymentCompatibility'
       9              : import { useDeploymentCompatibility } from '@/platform/workspace/composables/useDeploymentCompatibility'
      10              : import type { DeploymentRowLabels } from '@/platform/workspace/composables/useDeploymentLabels'
      11              : import {
      12              :   COMFY_CLOUD_ROW,
      13              :   useDeploymentLabels
      14              : } from '@/platform/workspace/composables/useDeploymentLabels'
      15              : import type {
      16              :   DeploymentRowAction,
      17              :   DeploymentRowReason,
      18              :   DeploymentStart,
      19              :   DeploymentStartRefusal
      20              : } from '@/platform/workspace/deploymentStartState'
      21              : import {
      22              :   deploymentRowAction,
      23              :   deploymentRowReason
      24              : } from '@/platform/workspace/deploymentStartState'
      25              : import { useDeploymentPickStore } from '@/platform/workspace/stores/deploymentPickStore'
      26              : import { useDeploymentStartStore } from '@/platform/workspace/stores/deploymentStartStore'
      27              : 
      28              : type DeploymentSectionKey = 'runOn' | 'current' | 'compatible' | 'incompatible'
      29              : 
      30              : interface MarkedRow {
      31              :   value: string
      32              :   mark: DeploymentCompatibilityMark | null
      33              : }
      34              : 
      35              : interface DeploymentStartProblem {
      36              :   summary: string
      37              :   message: string | null
      38              :   fix: string
      39              : }
      40              : 
      41              : /** One row of the Run on menu, ready to render. */
      42              : export interface DeploymentRow extends DeploymentRowLabels, MarkedRow {
      43              :   note: string | null
      44              :   action: DeploymentRowAction
      45              :   actionLabel: string | null
      46              :   actionIcon: string | null
      47              :   ariaLabel: string | undefined
      48              :   problem: DeploymentStartProblem | null
      49              : }
      50              : 
      51              : interface DeploymentSection<Row> {
      52              :   key: DeploymentSectionKey
      53              :   rows: Row[]
      54              : }
      55              : 
      56         2355 : const SECTION_LABEL_KEYS: Record<DeploymentSectionKey, string> = {
      57              :   runOn: 'deploymentSwitcher.runOn',
      58              :   current: 'deploymentSwitcher.runningOn',
      59              :   compatible: 'deploymentSwitcher.compatibleEnvironments',
      60              :   incompatible: 'deploymentSwitcher.incompatibleEnvironments'
      61              : }
      62              : 
      63         2355 : const ACTION_KEYS: Partial<
      64              :   Record<
      65              :     DeploymentRowAction,
      66              :     { label: string; ariaLabel: string; icon?: string }
      67              :   >
      68              : > = {
      69              :   start: {
      70              :     label: 'deploymentSwitcher.start',
      71              :     ariaLabel: 'deploymentSwitcher.startLabel'
      72              :   },
      73              :   retry: {
      74              :     label: 'deploymentSwitcher.retry',
      75              :     ariaLabel: 'deploymentSwitcher.retryLabel'
      76              :   },
      77              :   open: {
      78              :     label: 'deploymentSwitcher.start',
      79              :     ariaLabel: 'deploymentSwitcher.startOnPlatformLabel',
      80              :     icon: 'icon-[lucide--arrow-up-right]'
      81              :   }
      82              : }
      83              : 
      84         2355 : const FIX_KEYS: Record<DeploymentStartRefusal, string> = {
      85              :   noCredits: 'deploymentSwitcher.startFix.noCredits',
      86              :   tooManyRunning: 'deploymentSwitcher.startFix.tooManyRunning',
      87              :   unavailable: 'deploymentSwitcher.startFix.unavailable',
      88              :   failed: 'deploymentSwitcher.startFix.failed'
      89              : }
      90              : 
      91         2355 : const REASON_KEYS: Record<DeploymentRowReason, string> = {
      92              :   stopped: 'deploymentSwitcher.reason.stopped',
      93              :   failed: 'deploymentSwitcher.reason.failed',
      94              :   starting: 'deploymentSwitcher.reason.starting',
      95              :   startElsewhere: 'deploymentSwitcher.reason.startElsewhere',
      96              :   notReady: 'deploymentSwitcher.reason.notReady'
      97              : }
      98              : 
      99              : /**
     100              :  * Puts the picked row first in one Run on list or, once every row is marked
     101              :  * and the marks disagree, alone above the compatible and incompatible rows.
     102              :  */
     103            0 : export function sectionRows<Row extends MarkedRow>(
     104              :   rows: readonly Row[],
     105              :   pickedValue: string
     106              : ): DeploymentSection<Row>[] {
     107            0 :   const [current, others] = partition(rows, (row) => row.value === pickedValue)
     108            0 :   const runs = (row: Row) => row.mark?.kind === 'runs'
     109              :   const marksDisagree =
     110              :     rows.every((row) => row.mark !== null) &&
     111              :     rows.some(runs) &&
     112              :     !rows.every(runs)
     113            0 :   if (!marksDisagree) return [{ key: 'runOn', rows: [...current, ...others] }]
     114            0 :   const [compatible, incompatible] = partition(others, runs)
     115              :   const sections: DeploymentSection<Row>[] = [
     116              :     { key: 'current', rows: current },
     117              :     { key: 'compatible', rows: compatible },
     118              :     { key: 'incompatible', rows: incompatible }
     119              :   ]
     120            0 :   return sections.filter((section) => section.rows.length > 0)
     121              : }
     122              : 
     123              : /**
     124              :  * The Run on menu's rows under their headings, and the picked row's value.
     125              :  * Only a ready deployment can be picked. One the listing says can start
     126              :  * offers Start, in place when the `deployment_start_enabled` flag is on and on
     127              :  * the Developer Platform when it is off. Any row but the current one that
     128              :  * cannot be picked says why.
     129              :  */
     130         2009 : export function useDeploymentRows() {
     131         2009 :   const { t, locale } = useI18n()
     132         2009 :   const { flags } = useFeatureFlags()
     133         2009 :   const { deployments, pickedDeployment, pickedDeploymentId } = storeToRefs(
     134              :     useDeploymentPickStore()
     135              :   )
     136         2009 :   const { starts } = storeToRefs(useDeploymentStartStore())
     137         2009 :   const { deploymentRowLabels, deploymentFullLabel } = useDeploymentLabels()
     138         2009 :   const { markFor } = useDeploymentCompatibility()
     139              : 
     140            0 :   function text(key: string, label: string): string {
     141            0 :     return t(key, { deployment: label }, { escapeParameter: false })
     142              :   }
     143              : 
     144            0 :   function nodeList(nodeTypes: string[]): string {
     145            0 :     return new Intl.ListFormat(locale.value).format(nodeTypes)
     146              :   }
     147              : 
     148            0 :   function compatibilityNote(
     149              :     mark: DeploymentCompatibilityMark | null,
     150              :     deployment: string
     151              :   ): string | null {
     152            0 :     if (mark?.kind === 'missing') {
     153            0 :       return t(
     154              :         'deploymentSwitcher.missingNodesNamed',
     155              :         { deployment, nodes: nodeList(mark.nodeTypes) },
     156              :         { escapeParameter: false }
     157              :       )
     158              :     }
     159            0 :     if (mark?.kind !== 'unknown') return null
     160            0 :     return t(
     161              :       'deploymentSwitcher.notCheckedNamed',
     162              :       { deployment },
     163              :       { escapeParameter: false }
     164              :     )
     165              :   }
     166              : 
     167            0 :   function compatibilityAfterReason(
     168              :     mark: DeploymentCompatibilityMark | null
     169              :   ): string | null {
     170            0 :     if (mark?.kind === 'missing') {
     171            0 :       return t(
     172              :         'deploymentSwitcher.missingNodesAfterReason',
     173              :         { nodes: nodeList(mark.nodeTypes) },
     174              :         { escapeParameter: false }
     175              :       )
     176              :     }
     177            0 :     if (mark?.kind !== 'unknown') return null
     178            0 :     return t('deploymentSwitcher.notCheckedAfterReason')
     179              :   }
     180              : 
     181            0 :   function note(
     182              :     mark: DeploymentCompatibilityMark | null,
     183              :     label: string,
     184              :     reason: string | null
     185              :   ): string | null {
     186            0 :     if (reason === null) return compatibilityNote(mark, label)
     187            0 :     const compatibility = compatibilityAfterReason(mark)
     188            0 :     return compatibility === null ? reason : `${reason} ${compatibility}`
     189              :   }
     190              : 
     191            0 :   function problemFor(
     192              :     label: string,
     193              :     start: DeploymentStart | undefined
     194              :   ): DeploymentStartProblem | null {
     195            0 :     if (start?.phase !== 'refused') return null
     196            0 :     return {
     197              :       summary: text('deploymentSwitcher.startFailed', label),
     198              :       message: start.message,
     199              :       fix: t(FIX_KEYS[start.reason])
     200              :     }
     201              :   }
     202              : 
     203            0 :   function actionFor(
     204              :     action: DeploymentRowAction,
     205              :     label: string
     206              :   ): Pick<
     207              :     DeploymentRow,
     208              :     'action' | 'actionLabel' | 'actionIcon' | 'ariaLabel'
     209              :   > {
     210            0 :     const keys = ACTION_KEYS[action]
     211            0 :     if (!keys) {
     212            0 :       return {
     213              :         action,
     214              :         actionLabel: null,
     215              :         actionIcon: null,
     216              :         ariaLabel: undefined
     217              :       }
     218              :     }
     219            0 :     return {
     220              :       action,
     221              :       actionLabel: t(keys.label),
     222              :       actionIcon: keys.icon ?? null,
     223              :       ariaLabel: text(keys.ariaLabel, label)
     224              :     }
     225              :   }
     226              : 
     227            0 :   function cloudRow(): DeploymentRow {
     228            0 :     const mark = markFor(null)
     229            0 :     const label = deploymentFullLabel(null)
     230            0 :     return {
     231              :       ...deploymentRowLabels(null),
     232              :       mark,
     233              :       note: note(mark, label, null),
     234              :       ...actionFor('pick', label),
     235              :       problem: null
     236              :     }
     237              :   }
     238              : 
     239            0 :   function deploymentRow(deployment: WorkspaceDeployment): DeploymentRow {
     240            0 :     const labels = deploymentRowLabels(deployment)
     241            0 :     const label = deploymentFullLabel(deployment)
     242            0 :     const mark = markFor(deployment.deployment_id)
     243            0 :     const start = starts.value.get(deployment.deployment_id)
     244            0 :     const action = deploymentRowAction(deployment, start, {
     245              :       startOffered: flags.deploymentStartEnabled
     246              :     })
     247            0 :     const isStarting = start?.phase === 'requested'
     248            0 :     const reason =
     249              :       deployment.deployment_id === pickedDeploymentId.value
     250              :         ? null
     251              :         : deploymentRowReason(deployment, action, start)
     252            0 :     return {
     253              :       ...labels,
     254              :       detail: isStarting
     255              :         ? t('deploymentSwitcher.status.starting')
     256              :         : labels.detail,
     257              :       tone: isStarting ? 'pending' : labels.tone,
     258              :       mark,
     259              :       note: note(
     260              :         mark,
     261              :         label,
     262              :         reason === null ? null : text(REASON_KEYS[reason], label)
     263              :       ),
     264              :       ...actionFor(action, label),
     265              :       problem: problemFor(label, start)
     266              :     }
     267              :   }
     268              : 
     269         2009 :   const rows = computed(() => [
     270              :     cloudRow(),
     271              :     ...deployments.value.map(deploymentRow)
     272              :   ])
     273              : 
     274         2009 :   const pickedValue = computed(
     275              :     () => pickedDeploymentId.value ?? COMFY_CLOUD_ROW
     276              :   )
     277              : 
     278              :   const sections = computed(() =>
     279              :     sectionRows(rows.value, pickedValue.value).map((section) => ({
     280              :       ...section,
     281              :       label: t(SECTION_LABEL_KEYS[section.key])
     282              :     }))
     283              :   )
     284              : 
     285              :   const pickedRow = computed(() =>
     286              :     pickedDeployment.value ? deploymentRow(pickedDeployment.value) : null
     287              :   )
     288              :   const isAnyStarting = computed(() =>
     289              :     rows.value.some((row) => row.tone === 'pending')
     290              :   )
     291              : 
     292         2009 :   return { sections, pickedValue, pickedRow, isAnyStarting }
     293              : }
        

Generated by: LCOV version 2.0-1