11import {
2+ commands ,
23 EnvironmentVariableCollection ,
34 EnvironmentVariableMutator ,
45 Event ,
@@ -15,7 +16,14 @@ import {
1516import { dump } from "js-yaml" ;
1617import * as tmp from "tmp" ;
1718import { join } from "path" ;
18- import { writeFileSync , mkdirSync , ensureDirSync , symlinkSync } from "fs-extra" ;
19+ import {
20+ writeFileSync ,
21+ mkdirSync ,
22+ ensureDirSync ,
23+ symlinkSync ,
24+ writeFile ,
25+ mkdir ,
26+ } from "fs-extra" ;
1927import { DirResult } from "tmp" ;
2028
2129import {
@@ -24,13 +32,15 @@ import {
2432 isFolderAlreadyInWorkspace ,
2533 isLikelyDatabaseRoot ,
2634 isLikelyDbLanguageFolder ,
35+ prepareCodeTour ,
2736 showBinaryChoiceDialog ,
2837 showBinaryChoiceWithUrlDialog ,
2938 showInformationMessageWithAction ,
3039 walkDirectory ,
3140} from "../../../src/helpers" ;
3241import { reportStreamProgress } from "../../../src/commandRunner" ;
3342import { QueryLanguage } from "../../../src/common/query-language" ;
43+ import { Setting } from "../../../src/config" ;
3444
3545describe ( "helpers" , ( ) => {
3646 describe ( "Invocation rate limiter" , ( ) => {
@@ -559,3 +569,113 @@ describe("isFolderAlreadyInWorkspace", () => {
559569 expect ( isFolderAlreadyInWorkspace ( "/third/path" ) ) . toBe ( false ) ;
560570 } ) ;
561571} ) ;
572+
573+ describe ( "prepareCodeTour" , ( ) => {
574+ let dir : tmp . DirResult ;
575+ let showInformationMessageSpy : jest . SpiedFunction <
576+ typeof window . showInformationMessage
577+ > ;
578+
579+ beforeEach ( ( ) => {
580+ dir = tmp . dirSync ( ) ;
581+
582+ const mockWorkspaceFolders = [
583+ {
584+ uri : Uri . file ( dir . name ) ,
585+ name : "test" ,
586+ index : 0 ,
587+ } ,
588+ ] as WorkspaceFolder [ ] ;
589+
590+ jest
591+ . spyOn ( workspace , "workspaceFolders" , "get" )
592+ . mockReturnValue ( mockWorkspaceFolders ) ;
593+
594+ showInformationMessageSpy = jest
595+ . spyOn ( window , "showInformationMessage" )
596+ . mockResolvedValue ( { title : "Yes" } ) ;
597+ } ) ;
598+
599+ afterEach ( ( ) => {
600+ dir . removeCallback ( ) ;
601+ } ) ;
602+
603+ describe ( "if we're in the tour repo" , ( ) => {
604+ describe ( "if the workspace is not already open" , ( ) => {
605+ it ( "should open the tutorial workspace" , async ( ) => {
606+ // set up directory to have a 'tutorial.code-workspace' file
607+ const tutorialWorkspacePath = join ( dir . name , "tutorial.code-workspace" ) ;
608+ await writeFile ( tutorialWorkspacePath , "{}" ) ;
609+
610+ // set up a .tours directory to indicate we're in the tour codespace
611+ const tourDirPath = join ( dir . name , ".tours" ) ;
612+ await mkdir ( tourDirPath ) ;
613+
614+ // spy that we open the workspace file by calling the 'vscode.openFolder' command
615+ const commandSpy = jest . spyOn ( commands , "executeCommand" ) ;
616+ commandSpy . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
617+
618+ await prepareCodeTour ( ) ;
619+
620+ expect ( showInformationMessageSpy ) . toHaveBeenCalled ( ) ;
621+ expect ( commandSpy ) . toHaveBeenCalledWith (
622+ "vscode.openFolder" ,
623+ expect . objectContaining ( {
624+ path : Uri . parse ( tutorialWorkspacePath ) . fsPath ,
625+ } ) ,
626+ ) ;
627+ } ) ;
628+ } ) ;
629+
630+ describe ( "if the workspace is already open" , ( ) => {
631+ it ( "should not open the tutorial workspace" , async ( ) => {
632+ // Set isCodespaceTemplate to true to indicate the workspace has already been opened
633+ jest . spyOn ( Setting . prototype , "getValue" ) . mockReturnValue ( true ) ;
634+
635+ // set up directory to have a 'tutorial.code-workspace' file
636+ const tutorialWorkspacePath = join ( dir . name , "tutorial.code-workspace" ) ;
637+ await writeFile ( tutorialWorkspacePath , "{}" ) ;
638+
639+ // set up a .tours directory to indicate we're in the tour codespace
640+ const tourDirPath = join ( dir . name , ".tours" ) ;
641+ await mkdir ( tourDirPath ) ;
642+
643+ // spy that we open the workspace file by calling the 'vscode.openFolder' command
644+ const commandSpy = jest . spyOn ( commands , "executeCommand" ) ;
645+ commandSpy . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
646+
647+ await prepareCodeTour ( ) ;
648+
649+ expect ( commandSpy ) . not . toHaveBeenCalled ( ) ;
650+ } ) ;
651+ } ) ;
652+ } ) ;
653+
654+ describe ( "if we're in a different tour repo" , ( ) => {
655+ it ( "should not open the tutorial workspace" , async ( ) => {
656+ // set up a .tours directory
657+ const tourDirPath = join ( dir . name , ".tours" ) ;
658+ await mkdir ( tourDirPath ) ;
659+
660+ // spy that we open the workspace file by calling the 'vscode.openFolder' command
661+ const commandSpy = jest . spyOn ( commands , "executeCommand" ) ;
662+ commandSpy . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
663+
664+ await prepareCodeTour ( ) ;
665+
666+ expect ( commandSpy ) . not . toHaveBeenCalled ( ) ;
667+ } ) ;
668+ } ) ;
669+
670+ describe ( "if we're in a different repo with no tour" , ( ) => {
671+ it ( "should not open the tutorial workspace" , async ( ) => {
672+ // spy that we open the workspace file by calling the 'vscode.openFolder' command
673+ const commandSpy = jest . spyOn ( commands , "executeCommand" ) ;
674+ commandSpy . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
675+
676+ await prepareCodeTour ( ) ;
677+
678+ expect ( commandSpy ) . not . toHaveBeenCalled ( ) ;
679+ } ) ;
680+ } ) ;
681+ } ) ;
0 commit comments