refactor: extract pure YouTube ingest formatting into server/lib/youtubeIngestFormat.js - #6132
Merged
Conversation
`server/services/youtubeIngest.js` mixed six responsibilities in one 865-line file, so unit-testing YAML escaping or the CoS agent prompt meant importing the whole service graph (child process, brain/cos/media stores, fs) behind a wall of `vi.mock` stubs. Move the pure half — `parseVideoMetadata`, `buildIngestNote`, `buildAgentTaskContext`, `resolveObsidianPointer`, `sanitizeFilename`, `formatDuration`, `yamlString` — verbatim into the new `server/lib/youtubeIngestFormat.js`. The moved bodies are byte-identical; the service imports them and keeps orchestration, storage, and the yt-dlp spawn. The new module is surfaced through the `server/lib` barrel as a NAMESPACE export (`youtubeIngestFormat.*`) rather than a flat one: `formatDuration` collides with `fileCore.js` and `sanitizeFilename` with `mimeTypes.js`, which the barrel's collision guard rejects for flat `export *`. Tests for those four public contracts move to `server/lib/youtubeIngestFormat.test.js` and now run with no mocks at all — if that file ever needs one, something impure leaked back into lib/. The service suite keeps the URL-allowlist and cancel-escalation cases. Added coverage for the untrusted-transcript boundary notice in the agent prompt, which nothing pinned before. Claude-Session: https://claude.ai/code/session_01RA3pD5YM2dukQwbZ3pC6WA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Pure-extraction refactor of the brain's YouTube ingest.
server/services/youtubeIngest.jshad grown to 865 lines mixing six responsibilities, so unit-testing YAML frontmatter escaping or the CoS agent prompt required importing the whole service graph (child process, brain/cos/media stores, fs) behind eightvi.mockstubs.Moved the pure half verbatim into a new
server/lib/youtubeIngestFormat.js:parseVideoMetadata— yt-dlp--dump-single-json→ the stored metadata shapebuildIngestNote— the Obsidian note (frontmatter the user's vault queries key on)buildAgentTaskContext— the CoS follow-up prompt, incl. the untrusted-transcript boundaryresolveObsidianPointer— the index's Obsidian-pointer reconciliation (Confirm whether an open-for-write materializes a dataless iCloud file (obsidian updateNote) #3706)sanitizeFilename,formatDuration,yamlStringThe service imports them and keeps orchestration, storage, and the yt-dlp spawn. No fs / db / childProcess / SSE imports in the new module.
Behavior preservation: the seven moved bodies are byte-identical to the originals (verified by diffing each extracted function against
origin/main's copy). The only edits were doc-comment wording where a comment described the old location ("Pure + exported so…").Barrel wiring: surfaced as a namespace export (
youtubeIngestFormat.*) rather than flat —formatDurationcollides withfileCore.jsandsanitizeFilenamewithmimeTypes.js, whichindex.test.js's collision guard rejects for flatexport *. Catalog row added toserver/lib/README.md.Tests: the four public contracts move to
server/lib/youtubeIngestFormat.test.js, which now runs with zero mocks — a standing check that nothing impure leaks back intolib/.server/services/youtubeIngest.test.jskeeps the URL-allowlist and cancel-escalation cases (and drops six now-unneeded mocks' worth of justification). Added one case that pins the untrusted-transcript notice in the agent prompt, which nothing covered before.Test plan
cd server && npm test— full server suite: 1918 files passed / 1 skipped, 38707 tests passed / 14 skippednpm test -- lib/youtubeIngestFormat.test.js services/youtubeIngest.test.js lib/index.test.js— 27 passed (barrel/README parity + collision guard included)Closes #6015
https://claude.ai/code/session_01RA3pD5YM2dukQwbZ3pC6WA