Setting the file. One moment.
Heygen Test · Media Use · heygen-com/hyperframes · Skills Docs
ContentsBack to the top of the page ⋯
scripts/11 files
(opens in a new tab)
audio/scripts/lib/ heygen.test.mjs
JavaScript · 103 lines · 3 KB
withCleanHeygenEnv
(
fn
) {
9 const previousApiKey = process.env. HEYGEN_API_KEY ;
10 const previousHyperframesApiKey = process.env. HYPERFRAMES_API_KEY ;
11 const previousConfigDir = process.env. HEYGEN_CONFIG_DIR ;
12 try {
13 delete process.env. HEYGEN_API_KEY ;
14 delete process.env. HYPERFRAMES_API_KEY ;
15 delete process.env. HEYGEN_CONFIG_DIR ;
16 return fn ();
17 } finally {
18 if (previousApiKey === undefined ) delete process.env. HEYGEN_API_KEY ;
19 else process.env. HEYGEN_API_KEY = previousApiKey;
20 if (previousHyperframesApiKey === undefined ) delete process.env. HYPERFRAMES_API_KEY ;
21 else process.env. HYPERFRAMES_API_KEY = previousHyperframesApiKey;
22 if (previousConfigDir === undefined ) delete process.env. HEYGEN_CONFIG_DIR ;
23 else process.env. HEYGEN_CONFIG_DIR = previousConfigDir;
24 }
25 }
26
27 test ( "heygenAuthHeaders does not tag API-key requests as CLI traffic, but still carries the media-use tool tag" , () => {
28 withCleanHeygenEnv (() => {
29 process.env. HEYGEN_API_KEY = "hg_test" ;
30 // API-key requests use normal billing; the backend ignores the cli-source
31 // header for them, so it's not sent. The tool-attribution header IS sent on
32 // every media-use call (any auth type) so the backend can isolate media-use.
33 assert. deepEqual ( heygenAuthHeaders (), {
34 "X-Api-Key" : "hg_test" ,
35 "X-HeyGen-Client-Source" : "media-use" ,
36 });
37 });
38 });
39
40 test ( "heygenAuthHeaders tags OAuth requests as CLI traffic and with the media-use tool tag" , () => {
41 withCleanHeygenEnv (() => {
42 const dir = mkdtempSync ( join ( tmpdir (), "heygen-cred-" ));
43 try {
44 process.env. HEYGEN_CONFIG_DIR = dir;
45 writeFileSync (
46 join (dir, "credentials" ),
47 JSON . stringify ({
48 oauth: {
49 access_token: "at_test" ,
50 expires_at: "2099-01-01T00:00:00Z" ,
51 },
52 }),
53 );
54 assert. deepEqual ( heygenAuthHeaders (), {
55 Authorization: "Bearer at_test" ,
56 "X-HeyGen-Source" : "cli" ,
57 "X-HeyGen-Client-Source" : "media-use" ,
58 });
59 } finally {
60 rmSync (dir, { recursive: true , force: true });
61 }
62 });
63 });
64
65 test ( "heygenAuthMethod returns api_key for an env API key, without tagging headers" , () => {
66 withCleanHeygenEnv (() => {
67 process.env. HEYGEN_API_KEY = "hg_test" ;
68 assert. equal ( heygenAuthMethod (), "api_key" );
69 });
70 });
71
72 test ( "heygenAuthMethod returns oauth for a live OAuth credential" , () => {
73 withCleanHeygenEnv (() => {
74 const dir = mkdtempSync ( join ( tmpdir (), "heygen-cred-" ));
75 try {
76 process.env. HEYGEN_CONFIG_DIR = dir;
77 writeFileSync (
78 join (dir, "credentials" ),
79 JSON . stringify ({
80 oauth: {
81 access_token: "at_test" ,
82 expires_at: "2099-01-01T00:00:00Z" ,
83 },
84 }),
85 );
86 assert. equal ( heygenAuthMethod (), "oauth" );
87 } finally {
88 rmSync (dir, { recursive: true , force: true });
89 }
90 });
91 });
92
93 test ( "heygenAuthMethod returns null with no credential at all" , () => {
94 withCleanHeygenEnv (() => {
95 const dir = mkdtempSync ( join ( tmpdir (), "heygen-cred-" ));
96 try {
97 process.env. HEYGEN_CONFIG_DIR = dir; // no credentials file written
98 assert. equal ( heygenAuthMethod (), null );
99 } finally {
100 rmSync (dir, { recursive: true , force: true });
101 }
102 });
103 });