All files / web/src/lib/song-share getSharedSong.ts

72.98% Statements 181/248
93.54% Branches 29/31
50% Functions 1/2
72.98% Lines 181/248

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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 2491x 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 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 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                       1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 2x 7x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 7x 6x 1x 7x 3x 2x 2x 3x 2x 2x 3x 7x 2x 2x 7x 2x 2x 2x 2x 2x 7x 7x 7x 7x 3x 2x 2x 3x 2x 2x 3x 2x 2x 3x 7x 3x 2x 2x 3x 3x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x  
/**
 * Server-side projection for a shared celebration song.
 *
 * Single source of truth for what a share code reveals — used by the public
 * API route (`/api/song-shares/[code]`), the public page (`/song/[code]`), and
 * its OG image. The raw `promptInput` never leaves this function; only the
 * fields the share's visibility toggles permit are returned, and the lyric
 * annotation engine runs *here*, after the gate, fed only permitted facts.
 * This is the privacy boundary for a child's data.
 */
 
import { eq, sql } from 'drizzle-orm'
import { db, schema } from '@/db'
import type {
  SongProblemMoment,
  SongSkillSpotlight,
} from '@/lib/session-song/extract-session-stats'
import {
  type AnnotateFacts,
  type AnnotatedSongSection,
  annotateSections,
} from '@/lib/song-share/annotate'
import { formatSkill } from '@/lib/song-share/sessionFacts'
import { parseSongPlan } from '@/lib/song-share/songPlan'
import { DEFAULT_SONG_SHARE_VISIBILITY, type SongShareVisibility } from '@/db/schema/song-shares'
 
interface PromptInputShape {
  player?: { name?: string; emoji?: string; age?: number }
  currentSession?: {
    accuracy?: number
    problemsDone?: number
    problemsTotal?: number
    bestCorrectStreak?: number
    skillsPracticed?: string[]
  }
  practiceDrama?: {
    storyAngle?: string
    arcs?: string[]
    problemMoments?: SongProblemMoment[]
    skillSpotlights?: SongSkillSpotlight[]
  }
  gameBreak?: { gameName?: string; headline?: string }
}
 
export interface SharedSongStats {
  age?: number
  accuracyPct?: number
  problemsDone?: number
  problemsTotal?: number
  bestCorrectStreak?: number
  skills?: string[]
  storyAngle?: string
  /** A few human "what happened this session" lines (gated by problem detail). */
  highlights?: string[]
}
 
export interface SharedSongPayload {
  player: { name: string; emoji: string }
  song: {
    /** The underlying session_songs row id — used by derived-asset routes
     *  (audio, alignment, preview MP4) that key on the file path
     *  data/audio/songs/{songId}.{ext}. Already exposed via audioPath; this
     *  field just saves callers from string-parsing the URL. */
    id: string
    title: string | null
    audioPath: string
    /** Sidecar JSON path served by /api/audio/songs/{id}/alignment, or null if not present. */
    alignmentPath: string | null
    styles: string[]
    sections: AnnotatedSongSection[]
    createdAt: number
  }
  stats: SharedSongStats
  visibility: SongShareVisibility
}
 
/**
 * Resolve a share code to its projected payload, or `null` if the code is
 * missing/revoked or the song isn't a completed song (callers map null → 404).
 *
 * @param opts.bumpView increment the view counter (only the page render should;
 *   the OG image / API must not, or crawlers inflate the count)
 */
export async function getSharedSong(
  code: string,
  opts: { bumpView?: boolean } = {}
): Promise<SharedSongPayload | null> {
  const [share] = await db
    .select({
      id: schema.songShares.id,
      songId: schema.songShares.songId,
      visibility: schema.songShares.visibility,
      status: schema.songShares.status,
    })
    .from(schema.songShares)
    .where(eq(schema.songShares.id, code))
    .limit(1)

  if (!share || share.status !== 'active') return null

  const [song] = await db
    .select({
      id: schema.sessionSongs.id,
      status: schema.sessionSongs.status,
      playerId: schema.sessionSongs.playerId,
      llmOutput: schema.sessionSongs.llmOutput,
      promptInput: schema.sessionSongs.promptInput,
      createdAt: schema.sessionSongs.createdAt,
    })
    .from(schema.sessionSongs)
    .where(eq(schema.sessionSongs.id, share.songId))
    .limit(1)

  if (!song || song.status !== 'completed') return null

  const [player] = await db
    .select({ name: schema.players.name, emoji: schema.players.emoji })
    .from(schema.players)
    .where(eq(schema.players.id, song.playerId))
    .limit(1)

  if (opts.bumpView) {
    try {
      await db
        .update(schema.songShares)
        .set({ views: sql`${schema.songShares.views} + 1`, lastViewedAt: new Date() })
        .where(eq(schema.songShares.id, code))
    } catch (e) {
      console.error('song-share view bump failed:', e)
    }
  }

  // Legacy rows (written before a key was added to SongShareVisibility) lack
  // newer fields in the JSON column — spread the default first so every key
  // has a value before the page reads them.
  const visibility: SongShareVisibility = {
    ...DEFAULT_SONG_SHARE_VISIBILITY,
    ...(share.visibility as Partial<SongShareVisibility>),
  }

  return projectSharedSong({
    visibility,
    llmOutput: song.llmOutput,
    promptInput: song.promptInput,
    playerName: player?.name ?? 'A learner',
    playerEmoji: player?.emoji ?? '🧮',
    songId: song.id,
    createdAt: song.createdAt instanceof Date ? song.createdAt.getTime() : song.createdAt,
  })
}
 
/**
 * The privacy projection itself — pure, no I/O. Separated from the DB reads so
 * the toggle-gating contract can be unit-tested directly (and so the boundary
 * has exactly one implementation). `promptInput`/`llmOutput` come straight off
 * the row as `unknown`; nothing here returns them verbatim.
 */
export function projectSharedSong(input: {
  visibility: SongShareVisibility
  llmOutput: unknown
  promptInput: unknown
  playerName: string
  playerEmoji: string
  songId: string
  createdAt: number
}): SharedSongPayload {
  const { visibility, playerName, playerEmoji, songId, createdAt } = input
  const plan = parseSongPlan(input.llmOutput)
  const pi = (input.promptInput ?? {}) as PromptInputShape
  const firstName = playerName.trim().split(/\s+/)[0] || playerName
 
  // ---- Projection (privacy boundary) ----
  const stats: SharedSongStats = {}
  if (visibility.showAge && typeof pi.player?.age === 'number') {
    stats.age = pi.player.age
  }
  if (visibility.showAccuracy && pi.currentSession) {
    if (typeof pi.currentSession.accuracy === 'number') {
      stats.accuracyPct = Math.round(pi.currentSession.accuracy * 100)
    }
    if (typeof pi.currentSession.problemsDone === 'number') {
      stats.problemsDone = pi.currentSession.problemsDone
    }
    if (typeof pi.currentSession.problemsTotal === 'number') {
      stats.problemsTotal = pi.currentSession.problemsTotal
    }
  }
  const rawSkills = Array.isArray(pi.currentSession?.skillsPracticed)
    ? pi.currentSession!.skillsPracticed!
    : []
  if (visibility.showStreakSkills && pi.currentSession) {
    if (typeof pi.currentSession.bestCorrectStreak === 'number') {
      stats.bestCorrectStreak = pi.currentSession.bestCorrectStreak
    }
    if (rawSkills.length > 0) {
      stats.skills = rawSkills.map(formatSkill).slice(0, 8)
    }
  }
  if (visibility.showProblemDetail && typeof pi.practiceDrama?.storyAngle === 'string') {
    stats.storyAngle = pi.practiceDrama.storyAngle
  }
  if (visibility.showProblemDetail && Array.isArray(pi.practiceDrama?.arcs)) {
    const arcs = pi.practiceDrama.arcs.filter(
      (a): a is string => typeof a === 'string' && !!a.trim()
    )
    if (arcs.length > 0) stats.highlights = arcs.slice(0, 3)
  }
 
  // ---- Gated facts for the lyric annotation engine ----
  const facts: AnnotateFacts = { playerName: firstName }
  if (visibility.showProblemDetail) {
    if (Array.isArray(pi.practiceDrama?.problemMoments)) {
      facts.problemMoments = pi.practiceDrama.problemMoments
    }
    if (typeof pi.practiceDrama?.storyAngle === 'string') {
      facts.storyAngle = pi.practiceDrama.storyAngle
    }
    if (pi.gameBreak?.gameName) {
      facts.gameBreak = { gameName: pi.gameBreak.gameName, headline: pi.gameBreak.headline }
    }
  }
  if (visibility.showStreakSkills) {
    if (Array.isArray(pi.practiceDrama?.skillSpotlights)) {
      facts.skillSpotlights = pi.practiceDrama.skillSpotlights
    }
    if (rawSkills.length > 0) facts.skills = rawSkills
  }
 
  return {
    player: {
      name: playerName,
      emoji: playerEmoji,
    },
    song: {
      id: songId,
      title: plan.title,
      audioPath: `/api/audio/songs/${songId}`,
      // Alignment sidecar — route 404s gracefully for legacy songs without timestamps,
      // and the player falls back to static lyrics in that case.
      alignmentPath: `/api/audio/songs/${songId}/alignment`,
      styles: plan.globalStyles,
      sections: annotateSections(plan.sections, facts),
      createdAt,
    },
    stats,
    visibility,
  }
}