All files / src meridian-workspace.ts

88.26% Statements 158/179
72.91% Branches 35/48
87.5% Functions 14/16
88.26% Lines 158/179

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 1801x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x     6x 6x 6x 1x 8x 8x 8x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 3x 3x 4x 4x 3x 3x 4x 4x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 1x 1x 5x 5x 4x 4x 4x 4x 4x 4x 4x 5x                     5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x                   2x 1x 1x 1x 1x 2x 2x  
import * as fs from "node:fs"
import * as path from "node:path"
 
import {
  findKitRoot,
  isMeridianDocs,
  matchProjectForWorkspacePath,
  pickDefaultProjectId,
  projectById,
  readProjectsManifest,
  resolveMeridianProjects,
  type MeridianProject,
} from "./resolve-meridian-projects.js"
import { loadPlanningPayloadFromSqliteDetailed, sqliteDbExists } from "./load-from-sqlite.js"
 
export type MeridianProjectSummary = {
  id: string
  name: string
  docs: string
  packageRoot: string
  source: MeridianProject["source"]
  usCount: number
  isActive: boolean
}
 
export type MeridianWorkspaceInfo = {
  projectRoot: string
  docsRoot: string
  packageRoot: string
  projectId: string
  projectName: string
  projects: MeridianProjectSummary[]
  kitDetected: true
  docsExists: boolean
  usCount: number
}
 
export function countUserStoriesInDocs(docsRoot: string, packageRoot?: string): number {
  const pkg = packageRoot ?? path.dirname(docsRoot)
  if (!sqliteDbExists(pkg)) {
    return 0
  }
  const fromDb = loadPlanningPayloadFromSqliteDetailed(pkg)
  return fromDb.payload?.stories.length ?? 0
}
 
function docsDirExists(docsDir: string): boolean {
  return fs.existsSync(docsDir) && fs.statSync(docsDir).isDirectory()
}
 
function buildInfo(
  kitRoot: string,
  project: MeridianProject,
  allProjects: MeridianProject[],
): MeridianWorkspaceInfo {
  const docsRoot = path.join(kitRoot, ...project.docs.split("/"))
  const packageRoot = path.join(kitRoot, ...project.packageRoot.split("/"))
  const docsExists = docsDirExists(docsRoot)
 
  const projects: MeridianProjectSummary[] = allProjects.map((p) => {
    const pDocs = path.join(kitRoot, ...p.docs.split("/"))
    const exists = docsDirExists(pDocs)
    return {
      id: p.id,
      name: p.name,
      docs: p.docs,
      packageRoot: p.packageRoot,
      source: p.source,
      usCount: exists ? countUserStoriesInDocs(pDocs, path.join(kitRoot, ...p.packageRoot.split("/"))) : 0,
      isActive: p.id === project.id,
    }
  })
 
  return {
    projectRoot: kitRoot,
    docsRoot,
    packageRoot,
    projectId: project.id,
    projectName: project.name,
    projects,
    kitDetected: true,
    docsExists,
    usCount: docsExists ? countUserStoriesInDocs(docsRoot, packageRoot) : 0,
  }
}
 
export function resolveActiveProject(
  kitRoot: string,
  workspacePath: string,
  storedActiveId: string | undefined,
  configuredActiveId?: string,
): MeridianProject | null {
  const all = resolveMeridianProjects(kitRoot)
  if (all.length === 0) {
    return null
  }
 
  const manifest = readProjectsManifest(kitRoot)
  const fromWorkspace = matchProjectForWorkspacePath(all, kitRoot, workspacePath)
  const preferred = configuredActiveId ?? storedActiveId ?? fromWorkspace?.id
  const defaultId = pickDefaultProjectId(all, manifest, preferred)
  if (defaultId) {
    return projectById(all, defaultId) ?? all[0]
  }
  return all[0] ?? null
}
 
/** Aligns with `validate_meridian.py` kit detection (`.agent/MERIDIAN.md`). */
export function resolveMeridianWorkspaceFromPaths(
  workspacePath: string,
  storedActiveId?: string,
  configuredActiveId?: string,
): MeridianWorkspaceInfo | null {
  const normalized = path.resolve(workspacePath)
  const kitRoot = findKitRoot(normalized)
  if (!kitRoot) {
    return null
  }
 
  const all = resolveMeridianProjects(kitRoot)
  let active = resolveActiveProject(
    kitRoot,
    normalized,
    storedActiveId,
    configuredActiveId,
  )
 
  if (!active && normalized !== kitRoot && isMeridianDocs(path.join(normalized, "docs"))) {
    const docsRel = path.relative(kitRoot, path.join(normalized, "docs")).split(path.sep).join("/")
    const packageRel = path.relative(kitRoot, normalized).split(path.sep).join("/") || "."
    active = {
      id: packageRel === "." ? "main" : packageRel.replace(/\//g, "-"),
      name: path.basename(normalized),
      docs: docsRel,
      packageRoot: packageRel,
      source: "discovered",
    }
  }
 
  if (!active) {
    active = {
      id: "main",
      name: "Main",
      docs: "docs",
      packageRoot: ".",
      source: "discovered",
    }
  }
 
  const projectList = all.length ? all : [active]
  const resolved = projectById(projectList, active.id) ?? active
  return buildInfo(kitRoot, resolved, projectList)
}
 
export function formatStatusTooltip(info: MeridianWorkspaceInfo): string {
  const lines = [
    "Meridian kit: detected",
    `Kit root: ${info.projectRoot}`,
    `Active project: ${info.projectName} (${info.projectId})`,
    `Docs: ${info.docsRoot}`,
    `Package: ${info.packageRoot}`,
  ]
  if (info.projects.length > 1) {
    lines.push("Projects:")
    for (const p of info.projects) {
      const mark = p.isActive ? "•" : " "
      lines.push(
        `  ${mark} ${p.name} [${p.id}] → ${p.docs} (${p.usCount} US, ${p.source})`,
      )
    }
    lines.push("Switch: Meridian: Select Active Project")
  }
  if (!info.docsExists) {
    lines.push("Warning: docs/ folder missing or empty")
  } else {
    lines.push(`User stories (active): ${info.usCount}`)
  }
  return lines.join("\n")
}