1+ import { IModel } from "@itwin/imodels-client-management" ;
2+ import { ITwin } from "@itwin/itwins-client" ;
3+ import { runCommand } from "@oclif/test" ;
4+ import { expect } from "chai" ;
5+
6+ import { loginToCli } from "../utils/helpers" ;
7+
8+ describe ( 'Context Integration Tests' , ( ) => {
9+ let iTwin : ITwin ;
10+ let iModel : IModel ;
11+ let anotherITwin : ITwin ;
12+
13+ before ( async ( ) => {
14+ await loginToCli ( ) ;
15+ const name = `IntegrationTestITwin_${ new Date ( ) . toISOString ( ) } ` ;
16+ const iTwinResult = await runCommand < ITwin > ( `itwin create --name "${ name } " --class Thing --sub-class Asset` ) ;
17+ expect ( iTwinResult . error ) . to . be . undefined ;
18+ expect ( iTwinResult . result ) . to . not . be . undefined ;
19+ iTwin = iTwinResult . result as ITwin ;
20+
21+ const iModelName = `IntegrationTestIModel_${ new Date ( ) . toISOString ( ) } ` ;
22+ const iModelResult = await runCommand ( `imodel create --name "${ iModelName } " --itwin-id ${ iTwin . id } ` ) ;
23+ expect ( iModelResult . error ) . to . be . undefined ;
24+ expect ( iModelResult . result ) . to . not . be . undefined ;
25+ iModel = iModelResult . result as IModel ;
26+
27+ const anotherITwinName = `AnotherITwin_${ new Date ( ) . toISOString ( ) } ` ;
28+ const anotherITwinResult = await runCommand < ITwin > ( `itwin create --name "${ anotherITwinName } " --class Thing --sub-class Asset` ) ;
29+ expect ( anotherITwinResult . error ) . to . be . undefined ;
30+ expect ( anotherITwinResult . result ) . to . not . be . undefined ;
31+ anotherITwin = anotherITwinResult . result as ITwin ;
32+ } ) ;
33+
34+ after ( async ( ) => {
35+ await runCommand ( `itwin delete --id ${ iTwin . id } ` ) ;
36+ await runCommand ( `itwin delete --id ${ anotherITwin . id } ` ) ;
37+ } ) ;
38+
39+ beforeEach ( async ( ) => {
40+ const output = await runCommand ( 'context clear' ) ;
41+ expect ( output . error ) . to . be . undefined ;
42+ } ) ;
43+
44+ it ( 'should clear the context' , async ( ) => {
45+ const output = await runCommand ( 'context clear' ) ;
46+ expect ( output . error ) . to . be . undefined ;
47+ expect ( output . stdout ) . to . contain ( 'Context cleared.' ) ;
48+
49+ const outputInfo = await runCommand ( 'context info' ) ;
50+ expect ( outputInfo . error ) . to . be . undefined ;
51+ expect ( outputInfo . result ) . to . be . undefined ;
52+ } ) ;
53+
54+ it ( 'should set the context' , async ( ) => {
55+ const output = await runCommand ( `context set --itwin-id ${ iTwin . id } --imodel-id ${ iModel . id } ` ) ;
56+ expect ( output . error ) . to . be . undefined ;
57+ expect ( output . result ) . to . deep . equal ( { iModelId : iModel . id , iTwinId : iTwin . id } ) ;
58+ } ) ;
59+
60+ it ( 'should fail to set context with invalid iTwin ID' , async ( ) => {
61+ const invalidITwinId = "invalid-id" ;
62+ const output = await runCommand ( `context set --itwin-id ${ invalidITwinId } ` ) ;
63+ expect ( output . error ) . to . not . be . undefined ;
64+ expect ( output . error ?. message ) . to . contain ( 'Requested iTwin is not available.' ) ;
65+ } ) ;
66+
67+ it ( 'should fail to set context with mismatched iModel and iTwin IDs' , async ( ) => {
68+ const output = await runCommand ( `context set --itwin-id ${ anotherITwin . id } --imodel-id ${ iModel . id } ` ) ;
69+ expect ( output . error ) . to . not . be . undefined ;
70+ expect ( output . error ?. message ) . to . contain ( `The iModel ID ${ iModel . id } does not belong to the specified iTwin ID ${ anotherITwin . id } .` ) ;
71+ } ) ;
72+
73+ it ( 'should fail to set context without iTwin or iModel ID' , async ( ) => {
74+ const output = await runCommand ( 'context set' ) ;
75+ expect ( output . error ) . to . not . be . undefined ;
76+ expect ( output . error ?. message ) . to . contain ( 'Either --itwin-id or --imodel-id must be provided.' ) ;
77+ } ) ;
78+
79+ it ( 'should display the current context' , async ( ) => {
80+ await runCommand ( `context set --itwin-id ${ iTwin . id } --imodel-id ${ iModel . id } ` ) ;
81+ const output = await runCommand ( 'context info' ) ;
82+ expect ( output . error ) . to . be . undefined ;
83+ expect ( output . result ) . to . deep . equal ( { iModelId : iModel . id , iTwinId : iTwin . id } ) ;
84+ } ) ;
85+
86+ it ( 'should display undefined context after clearing' , async ( ) => {
87+ await runCommand ( 'context clear' ) ;
88+ const output = await runCommand ( 'context info' ) ;
89+ expect ( output . error ) . to . be . undefined ;
90+ expect ( output . result ) . to . be . undefined ;
91+ } ) ;
92+
93+ it ( 'should clear context multiple times without error' , async ( ) => {
94+ await runCommand ( 'context clear' ) ;
95+ const output = await runCommand ( 'context clear' ) ;
96+ expect ( output . error ) . to . be . undefined ;
97+ expect ( output . stdout ) . to . contain ( 'Context cleared.' ) ;
98+ } ) ;
99+
100+ it ( 'should set the context with only an iTwin ID' , async ( ) => {
101+ const output = await runCommand ( `context set --itwin-id ${ iTwin . id } ` ) ;
102+ expect ( output . error ) . to . be . undefined ;
103+ expect ( output . result ) . to . deep . equal ( { iModelId : undefined , iTwinId : iTwin . id } ) ;
104+ } ) ;
105+
106+ it ( 'should set the context with only an iModel ID and resolve the correct iTwin ID' , async ( ) => {
107+ const output = await runCommand ( `context set --imodel-id ${ iModel . id } ` ) ;
108+ expect ( output . error ) . to . be . undefined ;
109+ expect ( output . result ) . to . deep . equal ( { iModelId : iModel . id , iTwinId : iTwin . id } ) ;
110+ } ) ;
111+
112+ it ( 'should fail to set context with only an iModel ID if the iModel does not exist' , async ( ) => {
113+ const invalidIModelId = "invalid-id" ;
114+ const output = await runCommand ( `context set --imodel-id ${ invalidIModelId } ` ) ;
115+ expect ( output . error ) . to . not . be . undefined ;
116+ expect ( output . error ?. message ) . to . contain ( 'Requested iModel is not available.' ) ;
117+ } ) ;
118+ } ) ;
0 commit comments