@@ -2,8 +2,13 @@ package gptscript
2
2
3
3
import (
4
4
"context"
5
+ "crypto/rand"
6
+ "encoding/hex"
7
+ "os"
5
8
"runtime"
6
9
"testing"
10
+
11
+ "github.com/stretchr/testify/require"
7
12
)
8
13
9
14
func TestRestartingErrorRun (t * testing.T ) {
@@ -42,3 +47,44 @@ func TestRestartingErrorRun(t *testing.T) {
42
47
t .Errorf ("executing run with input of 0 should not fail: %v" , err )
43
48
}
44
49
}
50
+
51
+ func TestStackedContexts (t * testing.T ) {
52
+ const name = "testcred"
53
+
54
+ wd , err := os .Getwd ()
55
+ require .NoError (t , err )
56
+
57
+ bytes := make ([]byte , 32 )
58
+ _ , err = rand .Read (bytes )
59
+ require .NoError (t , err )
60
+
61
+ context1 := hex .EncodeToString (bytes )[:16 ]
62
+ context2 := hex .EncodeToString (bytes )[16 :]
63
+
64
+ run , err := g .Run (context .Background (), wd + "/test/credential.gpt" , Options {
65
+ CredentialContexts : []string {context1 , context2 },
66
+ })
67
+ require .NoError (t , err )
68
+
69
+ _ , err = run .Text ()
70
+ require .NoError (t , err )
71
+
72
+ // The credential should exist in context1 now.
73
+ cred , err := g .RevealCredential (context .Background (), []string {context1 , context2 }, name )
74
+ require .NoError (t , err )
75
+ require .Equal (t , cred .Context , context1 )
76
+
77
+ // Now change the context order and run the script again.
78
+ run , err = g .Run (context .Background (), wd + "/test/credential.gpt" , Options {
79
+ CredentialContexts : []string {context2 , context1 },
80
+ })
81
+ require .NoError (t , err )
82
+
83
+ _ , err = run .Text ()
84
+ require .NoError (t , err )
85
+
86
+ // Now make sure the credential exists in context1 still.
87
+ cred , err = g .RevealCredential (context .Background (), []string {context2 , context1 }, name )
88
+ require .NoError (t , err )
89
+ require .Equal (t , cred .Context , context1 )
90
+ }
0 commit comments