import { sync } from "../actions/sync" ; import { defaultPlasmicJson , expectProject1Components , expectProject1PlasmicJson , expectProjectAndDepPlasmicJson , mockApi , opts , project1Config , standardTestSetup , standardTestTeardown , tmpRepo , } from "../test-common/fixtures" ; jest . mock ( "../api" ) ; beforeEach ( ( ) => { standardTestSetup ( false ) ; } ) ; afterEach ( ( ) => { standardTestTeardown ( ) ; } ) ; function removeAuth ( ) { opts . auth = undefined ; tmpRepo . deletePlasmicAuth ( ) ; } describe ( "Project API tokens" , ( ) => { test ( "is filled in by auth'd user so that a second run does not need auth" , async ( ) => { opts . projects = [ "projectId1" ] ; await expect ( sync ( opts ) ) . resolves . toBeUndefined ( ) ; expectProject1Components ( ) ; expectProject1PlasmicJson ( ) ; removeAuth ( ) ; await expect ( sync ( opts ) ) . resolves . toBeUndefined ( ) ; } ) ; test ( "can be passed in from command-line args" , async ( ) => { removeAuth ( ) ; opts . projects = [ "projectId1:abc" ] ; await expect ( sync ( opts ) ) . resolves . toBeUndefined ( ) ; opts . projects = [ "projectId1:abc@1.2.3" ] ; await expect ( sync ( opts ) ) . resolves . toBeUndefined ( ) ; opts . projects = [ "projectId1:123" ] ; await expect ( sync ( opts ) ) . rejects . toThrow ( "No user+token, and project API tokens don't match" ) ; } ) ; test ( "is corrected by auth'd user if token was initially incorrect" , async ( ) => { opts . projects = [ "projectId1" ] ; tmpRepo . writePlasmicJson ( { ... defaultPlasmicJson , projects : [ { ... project1Config , projectApiToken : "blah" } ] , } ) ; await expect ( sync ( opts ) ) . resolves . toBeUndefined ( ) ; expectProject1Components ( ) ; expectProject1PlasmicJson ( ) ; removeAuth ( ) ; await expect ( sync ( opts ) ) . resolves . toBeUndefined ( ) ; } ) ; test ( "is filled in by auth'd user if project exists but token was initially missing" , async ( ) => { opts . projects = [ "projectId1" ] ; tmpRepo . writePlasmicJson ( { ... defaultPlasmicJson , projects : [ project1Config ] , } ) ; await expect ( sync ( opts ) ) . resolves . toBeUndefined ( ) ; expectProject1Components ( ) ; expectProject1PlasmicJson ( ) ; removeAuth ( ) ; await expect ( sync ( opts ) ) . resolves . toBeUndefined ( ) ; } ) ; test ( "when not available, should prompt for auth" , async ( ) => { opts . projects = [ "projectId1" ] ; removeAuth ( ) ; await expect ( sync ( opts ) ) . rejects . toThrow ( "Unable to authenticate" ) ; } ) ; test ( "when incorrect, should fail" , async ( ) => { opts . projects = [ "projectId1" ] ; removeAuth ( ) ; tmpRepo . writePlasmicJson ( { ... defaultPlasmicJson , projects : [ { ... project1Config , projectApiToken : "blah" } ] , } ) ; await expect ( sync ( opts ) ) . rejects . toThrow ( "No user+token, and project API tokens don't match" ) ; } ) ; test ( "works even if you have tokens for base but not deps" , async ( ) => { standardTestSetup ( ) ; opts . projects = [ "projectId1" ] ; removeAuth ( ) ; tmpRepo . writePlasmicJson ( { ... defaultPlasmicJson , projects : [ { ... project1Config , projectApiToken : "abc" } ] , } ) ; await expect ( sync ( opts ) ) . resolves . toBeUndefined ( ) ; expectProject1Components ( ) ; expectProjectAndDepPlasmicJson ( ) ; } ) ; test ( "works even if dependency was determined to not need an update" , async ( ) => { standardTestSetup ( ) ; opts . projects = [ "projectId1" ] ; await expect ( sync ( opts ) ) . resolves . toBeUndefined ( ) ; opts . force = false ; removeAuth ( ) ; mockApi . getMockProject ( "projectId1" , "1.2.3" ) . version = "1.2.4" ; await expect ( sync ( opts ) ) . resolves . toBeUndefined ( ) ; } ) ; test ( "should prompt for auth if you have only irrelevant tokens" , async ( ) => { opts . projects = [ "dependencyId1" ] ; removeAuth ( ) ; tmpRepo . writePlasmicJson ( { ... defaultPlasmicJson , projects : [ { ... project1Config , projectApiToken : "abc" } ] , } ) ; await expect ( sync ( opts ) ) . rejects . toThrow ( "Unable to authenticate" ) ; } ) ; test ( "should use plasmic-loader.json for API tokens in loader mode" , async ( ) => { process . env . PLASMIC_LOADER = "1" ; opts . projects = [ "projectId1" ] ; await expect ( sync ( opts ) ) . resolves . toBeUndefined ( ) ; const loaderConfig = tmpRepo . readPlasmicLoaderJson ( ) ; expect ( loaderConfig ) . toEqual ( { projects : [ { projectId : "projectId1" , projectApiToken : "abc" , } , ] , } ) ; removeAuth ( ) ; tmpRepo . writePlasmicJson ( defaultPlasmicJson ) ; await expect ( sync ( opts ) ) . resolves . toBeUndefined ( ) ; delete process . env [ "PLASMIC_LOADER" ] ; } ) ; test ( "works in PlasmicLoader mode even if root project was determined to not need an update" , async ( ) => { process . env . PLASMIC_LOADER = "1" ; standardTestSetup ( ) ; opts . projects = [ "projectId1" , "dependencyId1" ] ; await expect ( sync ( opts ) ) . resolves . toBeUndefined ( ) ; opts . force = false ; removeAuth ( ) ; mockApi . getMockProject ( "projectId1" , "1.2.3" ) . version = "1.2.4" ; await expect ( sync ( opts ) ) . resolves . toBeUndefined ( ) ; } ) ; test ( "should fail in loader mode if not available" , async ( ) => { process . env . PLASMIC_LOADER = "1" ; opts . projects = [ "projectId1" ] ; removeAuth ( ) ; await expect ( sync ( opts ) ) . rejects . toThrow ( ) ; delete process . env [ "PLASMIC_LOADER" ] ; } ) ; } ) ;