11import { join } from 'path'
22import { afterEach , beforeEach , describe , it , suite } from 'mocha'
3- import { restore , stub } from 'sinon'
3+ import { restore , spy , stub } from 'sinon'
44import { expect } from 'chai'
55import * as Fetch from 'node-fetch'
6+ import { commands } from 'vscode'
67import { buildInternalCommands , closeAllEditors } from './util'
78import { PROGRESS_TEST_TIMEOUT } from './timeouts'
89import { Disposable } from '../../extension'
@@ -12,6 +13,9 @@ import expShowFixture from '../fixtures/expShow/base/output'
1213import { dvcDemoPath } from '../util'
1314import { ExperimentFields } from '../../cli/dvc/contract'
1415import { Toast } from '../../vscode/toast'
16+ import { Modal } from '../../vscode/modal'
17+ import { Response } from '../../vscode/response'
18+ import { RegisteredCommands } from '../../commands/external'
1519
1620suite ( 'Patch Test Suite' , ( ) => {
1721 const disposable = Disposable . fn ( )
@@ -27,7 +31,7 @@ suite('Patch Test Suite', () => {
2731
2832 describe ( 'exp push patch' , ( ) => {
2933 it ( 'should share an experiment to Studio' , async ( ) => {
30- const mockFetch = stub ( Fetch , 'default' ) . resolves ( undefined )
34+ const mockFetch = stub ( Fetch , 'default' ) . resolves ( { } as Fetch . Response )
3135 const mockStudioAccessToken = 'isat_12123123123123123'
3236 const mockRepoUrl = 'https://github.com/iterative/vscode-dvc-demo'
3337
@@ -98,6 +102,66 @@ suite('Patch Test Suite', () => {
98102 } )
99103 } ) . timeout ( PROGRESS_TEST_TIMEOUT )
100104
105+ it ( 'should show an error modal if Studio returns a 401 response' , async ( ) => {
106+ const mockFetch = stub ( Fetch , 'default' ) . resolves ( {
107+ status : 401
108+ } as Fetch . Response )
109+ const mockStudioAccessToken = 'isat_12123123123123123'
110+ const mockRepoUrl = 'https://github.com/iterative/vscode-dvc-demo'
111+
112+ const executeCommandSpy = spy ( commands , 'executeCommand' )
113+
114+ const { internalCommands, gitReader, dvcReader } =
115+ buildInternalCommands ( disposable )
116+
117+ const mockGetRemoteUrl = stub ( gitReader , 'getRemoteUrl' ) . resolves (
118+ mockRepoUrl
119+ )
120+ const mockExpShow = stub ( dvcReader , 'expShow' ) . resolves ( expShowFixture )
121+
122+ const mockErrorWithOptions = stub ( Modal , 'errorWithOptions' ) . resolves (
123+ Response . SHOW
124+ )
125+
126+ registerPatchCommand ( internalCommands )
127+
128+ await internalCommands . executeCommand (
129+ AvailableCommands . EXP_PUSH ,
130+ mockStudioAccessToken ,
131+ dvcDemoPath ,
132+ 'exp-e7a67'
133+ )
134+
135+ expect ( mockGetRemoteUrl ) . to . be . calledOnce
136+ expect ( mockExpShow ) . to . be . calledOnce
137+ expect ( mockFetch ) . to . be . calledOnce
138+ expect ( mockErrorWithOptions ) . to . be . calledOnce
139+ expect ( executeCommandSpy ) . to . be . calledWithExactly (
140+ RegisteredCommands . CONNECT_SHOW
141+ )
142+
143+ const { name } = expShowFixture [
144+ '53c3851f46955fa3e2b8f6e1c52999acc8c9ea77'
145+ ] [ '4fb124aebddb2adf1545030907687fa9a4c80e70' ] . data as ExperimentFields
146+
147+ const baseBody = {
148+ baseline_sha : '53c3851f46955fa3e2b8f6e1c52999acc8c9ea77' ,
149+ client : 'vscode' ,
150+ name,
151+ repo_url : mockRepoUrl
152+ }
153+ const headers = {
154+ Authorization : `token ${ mockStudioAccessToken } ` ,
155+ 'Content-type' : 'application/json'
156+ }
157+
158+ expect ( mockFetch ) . to . be . calledWithExactly ( STUDIO_ENDPOINT , {
159+ body : JSON . stringify ( { ...baseBody , type : 'start' } ) ,
160+ headers,
161+ method : 'POST'
162+ } )
163+ } ) . timeout ( PROGRESS_TEST_TIMEOUT )
164+
101165 it ( 'should show an error message if the experiment cannot be found' , async ( ) => {
102166 const mockShowError = stub ( Toast , 'showError' ) . resolves ( undefined )
103167
0 commit comments