@@ -10,10 +10,12 @@ import {CREATE_PROJECT_API_VERSION, PROJECTS_API_VERSION} from '../../../service
1010import { InitCommand } from '../../init.js'
1111
1212const mocks = vi . hoisted ( ( ) => ( {
13+ confirm : vi . fn ( ) ,
1314 datasetsCreate : vi . fn ( ) ,
1415 detectFrameworkRecord : vi . fn ( ) ,
1516 getOrganizationChoices : vi . fn ( ) ,
1617 getOrganizationsWithAttachGrantInfo : vi . fn ( ) ,
18+ importDatasetRun : vi . fn ( ) ,
1719 input : vi . fn ( ) ,
1820 listDatasets : vi . fn ( ) ,
1921 select : vi . fn ( ) ,
@@ -29,6 +31,7 @@ vi.mock('@sanity/cli-core/ux', async () => {
2931
3032 return {
3133 ...actual ,
34+ confirm : mocks . confirm ,
3235 input : mocks . input ,
3336 select : mocks . select ,
3437 }
@@ -43,6 +46,7 @@ vi.mock('@sanity/cli-core', async (importOriginal) => {
4346
4447 return {
4548 ...actual ,
49+ getCliToken : vi . fn ( ) . mockResolvedValue ( 'test-token' ) ,
4650 getGlobalCliClient : vi . fn ( ) . mockResolvedValue ( {
4751 projects : {
4852 list : vi . fn ( ) . mockResolvedValue ( [
@@ -116,6 +120,10 @@ vi.mock('../../../actions/init/bootstrapTemplate.js', () => ({
116120 bootstrapTemplate : vi . fn ( ) . mockResolvedValue ( undefined ) ,
117121} ) )
118122
123+ vi . mock ( '../../dataset/import.js' , ( ) => ( {
124+ ImportDatasetCommand : { run : mocks . importDatasetRun } ,
125+ } ) )
126+
119127vi . mock ( '../../../actions/init/resolvePackageManager.js' , ( ) => ( {
120128 resolvePackageManager : vi . fn ( ) . mockResolvedValue ( 'npm' ) ,
121129} ) )
@@ -215,7 +223,7 @@ describe('#init: create new project', () => {
215223 await testCommand (
216224 InitCommand ,
217225 [
218- '--create- project=Test Project' ,
226+ '--project-name =Test Project' ,
219227 '--dataset=production' ,
220228 '--output-path=./test-project' ,
221229 '--no-nextjs-add-config-files' ,
@@ -314,7 +322,7 @@ describe('#init: create new project', () => {
314322 await testCommand (
315323 InitCommand ,
316324 [
317- '--create- project=Test Project' ,
325+ '--project-name =Test Project' ,
318326 '--dataset=production' ,
319327 '--output-path=./test-project' ,
320328 '--no-nextjs-add-config-files' ,
@@ -487,4 +495,102 @@ describe('#init: create new project', () => {
487495 expect . stringContaining ( 'already configured for Sanity MCP' ) ,
488496 )
489497 } )
498+
499+ test ( '--no-import-dataset skips dataset import for template with sample data' , async ( ) => {
500+ mocks . detectFrameworkRecord . mockResolvedValueOnce ( null )
501+
502+ setupInitSuccessMocks ( )
503+
504+ const { error} = await testCommand (
505+ InitCommand ,
506+ [
507+ '--project=project-123' ,
508+ '--dataset=production' ,
509+ '--output-path=./test-project' ,
510+ '--no-nextjs-add-config-files' ,
511+ '--no-nextjs-append-env' ,
512+ '--no-nextjs-embed-studio' ,
513+ '--no-typescript' ,
514+ '--no-overwrite-files' ,
515+ '--template=moviedb' ,
516+ '--no-import-dataset' ,
517+ ] ,
518+ { mocks : { ...defaultMocks , isInteractive : true } } ,
519+ )
520+
521+ if ( error ) throw error
522+ expect ( mocks . confirm ) . not . toHaveBeenCalled ( )
523+ expect ( mocks . importDatasetRun ) . not . toHaveBeenCalled ( )
524+ } )
525+
526+ test ( '--import-dataset forces import in unattended mode' , async ( ) => {
527+ mocks . detectFrameworkRecord . mockResolvedValueOnce ( null )
528+ mocks . importDatasetRun . mockResolvedValueOnce ( undefined )
529+
530+ // Only mock endpoints actually hit in unattended mode with --project and --dataset
531+ mockApi ( {
532+ apiVersion : PROJECTS_API_VERSION ,
533+ method : 'get' ,
534+ uri : '/projects/project-123' ,
535+ } ) . reply ( 200 , { id : 'project-123' , metadata : { cliInitializedAt : '' } } )
536+
537+ const { error} = await testCommand (
538+ InitCommand ,
539+ [
540+ '--yes' ,
541+ '--project=project-123' ,
542+ '--dataset=production' ,
543+ '--output-path=./test-project' ,
544+ '--template=moviedb' ,
545+ '--import-dataset' ,
546+ ] ,
547+ { mocks : defaultMocks } ,
548+ )
549+
550+ if ( error ) throw error
551+ expect ( mocks . confirm ) . not . toHaveBeenCalled ( )
552+ expect ( mocks . importDatasetRun ) . toHaveBeenCalledWith (
553+ expect . arrayContaining ( [
554+ 'https://public.sanity.io/moviesdb-2018-03-06.tar.gz' ,
555+ '--project-id' ,
556+ 'project-123' ,
557+ '--dataset' ,
558+ 'production' ,
559+ '--token' ,
560+ 'test-token' ,
561+ ] ) ,
562+ expect . objectContaining ( { root : expect . any ( String ) } ) ,
563+ )
564+ } )
565+
566+ test ( 'prompts for dataset import when flag is not set in interactive mode' , async ( ) => {
567+ mocks . detectFrameworkRecord . mockResolvedValueOnce ( null )
568+ mocks . confirm . mockResolvedValueOnce ( false )
569+
570+ setupInitSuccessMocks ( )
571+
572+ const { error} = await testCommand (
573+ InitCommand ,
574+ [
575+ '--project=project-123' ,
576+ '--dataset=production' ,
577+ '--output-path=./test-project' ,
578+ '--no-nextjs-add-config-files' ,
579+ '--no-nextjs-append-env' ,
580+ '--no-nextjs-embed-studio' ,
581+ '--no-typescript' ,
582+ '--no-overwrite-files' ,
583+ '--template=moviedb' ,
584+ ] ,
585+ { mocks : { ...defaultMocks , isInteractive : true } } ,
586+ )
587+
588+ if ( error ) throw error
589+ expect ( mocks . confirm ) . toHaveBeenCalledWith (
590+ expect . objectContaining ( {
591+ message : 'Add a sampling of sci-fi movies to your dataset on the hosted backend?' ,
592+ } ) ,
593+ )
594+ expect ( mocks . importDatasetRun ) . not . toHaveBeenCalled ( )
595+ } )
490596} )
0 commit comments