@@ -456,6 +456,91 @@ describe('TokowakaClient', () => {
456456 } ) ;
457457 } ) ;
458458
459+ describe ( 'createMetaconfig' , ( ) => {
460+ it ( 'should create metaconfig with generated API key' , async ( ) => {
461+ s3Client . send . resolves ( ) ;
462+
463+ const result = await client . createMetaconfig ( 'https://example.com/page1' , 'site-123' ) ;
464+
465+ expect ( result ) . to . exist ;
466+ expect ( result . siteId ) . to . equal ( 'site-123' ) ;
467+ expect ( result . apiKeys ) . to . be . an ( 'array' ) ;
468+ expect ( result . apiKeys ) . to . have . lengthOf ( 1 ) ;
469+ expect ( result . apiKeys [ 0 ] ) . to . be . a ( 'string' ) ;
470+ expect ( result . apiKeys [ 0 ] . length ) . to . be . greaterThan ( 0 ) ;
471+ expect ( result . enhancements ) . to . equal ( true ) ;
472+ expect ( result . tokowakaEnabled ) . to . equal ( true ) ;
473+
474+ // Verify S3 upload was called
475+ expect ( s3Client . send ) . to . have . been . calledOnce ;
476+ const command = s3Client . send . firstCall . args [ 0 ] ;
477+ expect ( command . input . Bucket ) . to . equal ( 'test-bucket' ) ;
478+ expect ( command . input . Key ) . to . equal ( 'opportunities/example.com/config' ) ;
479+ expect ( command . input . ContentType ) . to . equal ( 'application/json' ) ;
480+
481+ const uploadedMetaconfig = JSON . parse ( command . input . Body ) ;
482+ expect ( uploadedMetaconfig . siteId ) . to . equal ( 'site-123' ) ;
483+ expect ( uploadedMetaconfig . apiKeys ) . to . have . lengthOf ( 1 ) ;
484+ expect ( uploadedMetaconfig . enhancements ) . to . equal ( true ) ;
485+ expect ( uploadedMetaconfig . tokowakaEnabled ) . to . equal ( true ) ;
486+ } ) ;
487+
488+ it ( 'should handle www subdomain correctly' , async ( ) => {
489+ s3Client . send . resolves ( ) ;
490+
491+ const result = await client . createMetaconfig ( 'https://www.example.com/page1' , 'site-123' ) ;
492+
493+ expect ( result . siteId ) . to . equal ( 'site-123' ) ;
494+ expect ( s3Client . send ) . to . have . been . calledOnce ;
495+
496+ const command = s3Client . send . firstCall . args [ 0 ] ;
497+ expect ( command . input . Key ) . to . equal ( 'opportunities/example.com/config' ) ;
498+ } ) ;
499+
500+ it ( 'should handle tokowakaEnabled option' , async ( ) => {
501+ s3Client . send . resolves ( ) ;
502+
503+ const result = await client . createMetaconfig ( 'https://example.com/page1' , 'site-123' , {
504+ tokowakaEnabled : false ,
505+ } ) ;
506+
507+ expect ( result . tokowakaEnabled ) . to . equal ( false ) ;
508+ } ) ;
509+
510+ it ( 'should throw error if URL is missing' , async ( ) => {
511+ try {
512+ await client . createMetaconfig ( '' , 'site-123' ) ;
513+ expect . fail ( 'Should have thrown error' ) ;
514+ } catch ( error ) {
515+ expect ( error . message ) . to . include ( 'URL is required' ) ;
516+ expect ( error . status ) . to . equal ( 400 ) ;
517+ }
518+ } ) ;
519+
520+ it ( 'should throw error if siteId is missing' , async ( ) => {
521+ try {
522+ await client . createMetaconfig ( 'https://example.com' , '' ) ;
523+ expect . fail ( 'Should have thrown error' ) ;
524+ } catch ( error ) {
525+ expect ( error . message ) . to . include ( 'Site ID is required' ) ;
526+ expect ( error . status ) . to . equal ( 400 ) ;
527+ }
528+ } ) ;
529+
530+ it ( 'should throw error on S3 upload failure' , async ( ) => {
531+ const s3Error = new Error ( 'Access Denied' ) ;
532+ s3Client . send . rejects ( s3Error ) ;
533+
534+ try {
535+ await client . createMetaconfig ( 'https://example.com/page1' , 'site-123' ) ;
536+ expect . fail ( 'Should have thrown error' ) ;
537+ } catch ( error ) {
538+ expect ( error . message ) . to . include ( 'S3 upload failed' ) ;
539+ expect ( error . status ) . to . equal ( 500 ) ;
540+ }
541+ } ) ;
542+ } ) ;
543+
459544 describe ( 'uploadConfig' , ( ) => {
460545 it ( 'should upload config to S3' , async ( ) => {
461546 const config = {
0 commit comments