@@ -962,8 +962,11 @@ test("identical grade resolve hits the project cache without re-freezing", () =>
962962// resolve that reaches track("media_use_resolve", ...) with tracking allowed
963963// posts to a local HTTP server instead of production, and the server actually
964964// receives it (not just "nothing happened because nothing was listening").
965- test ( "track() posts to MEDIA_USE_TELEMETRY_HOST when set, proving real interception" , async ( ) => {
966- setup ( ) ;
965+ // Spawns a real resolve that hits the manifest for `provider`, intercepts the
966+ // telemetry POST it makes, and hands back the media_use_resolve event actually
967+ // sent. Nothing is stubbed: the CLI runs as its own process, telemetry.mjs builds
968+ // the URL, and a local server reads the payload off the wire.
969+ async function captureResolveEvent ( { provider, type = "bgm" , intent } ) {
967970 const received = [ ] ;
968971 const server = createServer ( ( req , res ) => {
969972 let body = "" ;
@@ -972,7 +975,7 @@ test("track() posts to MEDIA_USE_TELEMETRY_HOST when set, proving real intercept
972975 try {
973976 received . push ( JSON . parse ( body ) ) ;
974977 } catch {
975- // ignore malformed body; assertions below fail on empty `received`
978+ // ignore malformed body; callers assert on empty `received`
976979 }
977980 res . writeHead ( 200 , { "Content-Type" : "application/json" } ) ;
978981 res . end ( "{}" ) ;
@@ -983,8 +986,14 @@ test("track() posts to MEDIA_USE_TELEMETRY_HOST when set, proving real intercept
983986 const sandboxHome = mkdtempSync ( join ( tmpdir ( ) , "mu-resolve-telemetry-home-" ) ) ;
984987
985988 try {
989+ // The record's type must match the --type below, otherwise the manifest
990+ // never matches, the cascade calls a live provider, and the run fails for
991+ // reasons that have nothing to do with the tier.
986992 const record = makeRecord ( {
987- provenance : { prompt : "telemetry seam test" , provider : "test" } ,
993+ id : `${ type } _tier_001` ,
994+ type,
995+ path : `.media/audio/${ type } /${ type } _tier_001.wav` ,
996+ provenance : { prompt : intent , provider } ,
988997 } ) ;
989998 appendRecord ( tmp , record ) ;
990999 const filePath = join ( tmp , record . path ) ;
@@ -1000,7 +1009,7 @@ test("track() posts to MEDIA_USE_TELEMETRY_HOST when set, proving real intercept
10001009 // their real email into this test's local-server payload despite HOME
10011010 // being sandboxed (HEYGEN_CONFIG_DIR, not HOME, resolves the credentials
10021011 // path). Every other test in this file keeps its untouched default env.
1003- runResolve ( [ "--type" , "bgm" , "--intent" , "telemetry seam test" , "--project" , tmp , "--json" ] , {
1012+ runResolve ( [ "--type" , type , "--intent" , intent , "--project" , tmp , "--json" ] , {
10041013 env : {
10051014 DO_NOT_TRACK : "0" ,
10061015 HYPERFRAMES_NO_TELEMETRY : "0" ,
@@ -1013,7 +1022,7 @@ test("track() posts to MEDIA_USE_TELEMETRY_HOST when set, proving real intercept
10131022 } ) ;
10141023
10151024 // runResolve blocks synchronously (execFileSync) until the child exits, which
1016- // pauses this process's own event loop for that whole span — the child's
1025+ // pauses this process's own event loop for that whole span -- the child's
10171026 // request to our local server sits accepted-but-unprocessed in the kernel
10181027 // backlog until control returns here. Poll briefly to let the event loop
10191028 // drain it rather than asserting before the server has had a turn to run.
@@ -1023,16 +1032,70 @@ test("track() posts to MEDIA_USE_TELEMETRY_HOST when set, proving real intercept
10231032 } finally {
10241033 await new Promise ( ( resolve ) => server . close ( resolve ) ) ;
10251034 rmSync ( sandboxHome , { recursive : true , force : true } ) ;
1026- cleanup ( ) ;
10271035 }
10281036
10291037 assert . ok ( received . length > 0 , "expected the local telemetry server to receive a POST" ) ;
1030- const resolveEvent = received [ 0 ] . batch . find ( ( event ) => event . event === "media_use_resolve" ) ;
1031- assert . ok ( resolveEvent , "expected a media_use_resolve event in the intercepted batch" ) ;
1032- assert . equal ( resolveEvent . properties . provider , "test" ) ;
1033- assert . equal ( resolveEvent . properties . type , "bgm" ) ;
1038+ const event = received [ 0 ] . batch . find ( ( e ) => e . event === "media_use_resolve" ) ;
1039+ assert . ok ( event , "expected a media_use_resolve event in the intercepted batch" ) ;
1040+ return event ;
1041+ }
1042+
1043+ test ( "track() posts to MEDIA_USE_TELEMETRY_HOST when set, proving real interception" , async ( ) => {
1044+ setup ( ) ;
1045+ try {
1046+ const event = await captureResolveEvent ( { provider : "test" , intent : "telemetry seam test" } ) ;
1047+ assert . equal ( event . properties . provider , "test" ) ;
1048+ assert . equal ( event . properties . type , "bgm" ) ;
1049+ // "test" is not a declared registry provider, so the tier is absent rather
1050+ // than guessed, the same sparseness rule auth_method follows.
1051+ assert . equal (
1052+ "provider_tier" in event . properties && event . properties . provider_tier !== undefined ,
1053+ false ,
1054+ "an undeclared provider must not be assigned a cost tier" ,
1055+ ) ;
1056+ } finally {
1057+ cleanup ( ) ;
1058+ }
10341059} ) ;
10351060
1061+ // The registry-derived tier has to survive the whole path -- registry lookup,
1062+ // result(), track(), JSON body -- not just a unit call to providerTierFor. Each
1063+ // case names a provider the registry declares at a different tier and asserts the
1064+ // tier that actually reaches the wire.
1065+ for ( const [ provider , type , expected ] of [
1066+ [ "heygen.tts" , "voice" , "network_paid" ] ,
1067+ [ "heygen.audio.sounds" , "bgm" , "network_free" ] ,
1068+ [ "bundled.sfx" , "sfx" , "local" ] ,
1069+ ] ) {
1070+ test ( `a resolve won by ${ provider } sends provider_tier: ${ expected } ` , async ( ) => {
1071+ setup ( ) ;
1072+ try {
1073+ const event = await captureResolveEvent ( {
1074+ provider,
1075+ type,
1076+ intent : `tier seam ${ provider } ` ,
1077+ } ) ;
1078+ assert . equal ( event . properties . provider , provider ) ;
1079+ assert . equal (
1080+ event . properties . provider_tier ,
1081+ expected ,
1082+ `${ provider } must reach the wire as ${ expected } ` ,
1083+ ) ;
1084+ // The tier is derived from the registry and the auth method from the
1085+ // credential state; they must not become entangled. A non-heygen provider
1086+ // carries a tier and no auth method, whatever credentials exist locally.
1087+ if ( ! provider . startsWith ( "heygen." ) )
1088+ assert . equal (
1089+ event . properties . auth_method ,
1090+ undefined ,
1091+ "a non-heygen provider must carry a tier without an auth method" ,
1092+ ) ;
1093+ } finally {
1094+ cleanup ( ) ;
1095+ }
1096+ } ) ;
1097+ }
1098+
10361099// --- run ---
10371100
10381101async function main ( ) {
0 commit comments