@@ -4,6 +4,19 @@ import { fromFileUrl } from "@std/path"
44const keyringUrl = new URL ( "../src/keyring/index.ts" , import . meta. url )
55const denoJsonPath = fromFileUrl ( new URL ( "../deno.json" , import . meta. url ) )
66
7+ const MOCK_BACKEND = `
8+ const _store = new Map();
9+ _setBackend({
10+ get(account) { return Promise.resolve(_store.get(account) ?? null) },
11+ set(account, password) { _store.set(account, password); return Promise.resolve() },
12+ delete(account) { _store.delete(account); return Promise.resolve() },
13+ });
14+ ` . trim ( )
15+
16+ function mockAndImport ( imports : string ) : string {
17+ return `import { ${ imports } , _setBackend } from "${ keyringUrl } ";\n${ MOCK_BACKEND } `
18+ }
19+
720async function runWithKeyring ( code : string ) : Promise < string > {
821 const command = new Deno . Command ( "deno" , {
922 args : [
@@ -28,13 +41,7 @@ async function runWithKeyring(code: string): Promise<string> {
2841
2942Deno . test ( "keyring - getPassword returns null when not set" , async ( ) => {
3043 const code = `
31- import { getPassword, _setBackend } from "${ keyringUrl } ";
32- _setBackend({
33- store: new Map(),
34- get(account) { return Promise.resolve(this.store.get(account) ?? null) },
35- set(account, password) { this.store.set(account, password); return Promise.resolve() },
36- delete(account) { this.store.delete(account); return Promise.resolve() },
37- });
44+ ${ mockAndImport ( "getPassword" ) }
3845 const result = await getPassword("missing");
3946 console.log(result === null ? "null" : result);
4047 `
@@ -44,13 +51,7 @@ Deno.test("keyring - getPassword returns null when not set", async () => {
4451
4552Deno . test ( "keyring - setPassword and getPassword round-trip" , async ( ) => {
4653 const code = `
47- import { getPassword, setPassword, _setBackend } from "${ keyringUrl } ";
48- _setBackend({
49- store: new Map(),
50- get(account) { return Promise.resolve(this.store.get(account) ?? null) },
51- set(account, password) { this.store.set(account, password); return Promise.resolve() },
52- delete(account) { this.store.delete(account); return Promise.resolve() },
53- });
54+ ${ mockAndImport ( "getPassword, setPassword" ) }
5455 await setPassword("my-account", "secret123");
5556 const result = await getPassword("my-account");
5657 console.log(result);
@@ -61,13 +62,7 @@ Deno.test("keyring - setPassword and getPassword round-trip", async () => {
6162
6263Deno . test ( "keyring - deletePassword removes stored password" , async ( ) => {
6364 const code = `
64- import { getPassword, setPassword, deletePassword, _setBackend } from "${ keyringUrl } ";
65- _setBackend({
66- store: new Map(),
67- get(account) { return Promise.resolve(this.store.get(account) ?? null) },
68- set(account, password) { this.store.set(account, password); return Promise.resolve() },
69- delete(account) { this.store.delete(account); return Promise.resolve() },
70- });
65+ ${ mockAndImport ( "getPassword, setPassword, deletePassword" ) }
7166 await setPassword("my-account", "secret123");
7267 await deletePassword("my-account");
7368 const result = await getPassword("my-account");
@@ -79,13 +74,7 @@ Deno.test("keyring - deletePassword removes stored password", async () => {
7974
8075Deno . test ( "keyring - setPassword overwrites existing value" , async ( ) => {
8176 const code = `
82- import { getPassword, setPassword, _setBackend } from "${ keyringUrl } ";
83- _setBackend({
84- store: new Map(),
85- get(account) { return Promise.resolve(this.store.get(account) ?? null) },
86- set(account, password) { this.store.set(account, password); return Promise.resolve() },
87- delete(account) { this.store.delete(account); return Promise.resolve() },
88- });
77+ ${ mockAndImport ( "getPassword, setPassword" ) }
8978 await setPassword("my-account", "first");
9079 await setPassword("my-account", "second");
9180 const result = await getPassword("my-account");
@@ -97,13 +86,7 @@ Deno.test("keyring - setPassword overwrites existing value", async () => {
9786
9887Deno . test ( "keyring - multiple accounts are independent" , async ( ) => {
9988 const code = `
100- import { getPassword, setPassword, _setBackend } from "${ keyringUrl } ";
101- _setBackend({
102- store: new Map(),
103- get(account) { return Promise.resolve(this.store.get(account) ?? null) },
104- set(account, password) { this.store.set(account, password); return Promise.resolve() },
105- delete(account) { this.store.delete(account); return Promise.resolve() },
106- });
89+ ${ mockAndImport ( "getPassword, setPassword" ) }
10790 await setPassword("account-a", "password-a");
10891 await setPassword("account-b", "password-b");
10992 const a = await getPassword("account-a");
@@ -118,13 +101,7 @@ Deno.test("keyring - multiple accounts are independent", async () => {
118101
119102Deno . test ( "keyring - deletePassword on missing account is a no-op" , async ( ) => {
120103 const code = `
121- import { deletePassword, _setBackend } from "${ keyringUrl } ";
122- _setBackend({
123- store: new Map(),
124- get(account) { return Promise.resolve(this.store.get(account) ?? null) },
125- set(account, password) { this.store.set(account, password); return Promise.resolve() },
126- delete(account) { this.store.delete(account); return Promise.resolve() },
127- });
104+ ${ mockAndImport ( "deletePassword" ) }
128105 await deletePassword("nonexistent");
129106 console.log("ok");
130107 `
0 commit comments