@xiaolikt/ai-vault 0.4.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@ import assert from "node:assert/strict";
3
3
  import { bind, cleanUpOnExit, useMocks } from "./helpers.mjs";
4
4
 
5
5
  const dir = useMocks();
6
- const { default: ompVault } = await import("../src/index.ts");
6
+ const { default: ompVault } = await import("../packages/pi-ai-vault/src/index.ts");
7
7
  cleanUpOnExit(dir);
8
8
 
9
9
  test("omp: one extension registers the local vault and live Bitwarden, without bw_inject", () => {
@@ -1,6 +1,6 @@
1
1
  import { test } from "node:test";
2
2
  import assert from "node:assert/strict";
3
- import { scrubDeep, scrubText } from "../src/scrub.ts";
3
+ import { scrubDeep, scrubText } from "../packages/pi-ai-vault/src/scrub.ts";
4
4
 
5
5
  const SECRET = "supersecret-value-123";
6
6
  const values = [{ env: "T1", value: SECRET }];
@@ -1,5 +1,6 @@
1
1
  import { test, before, after } from "node:test";
2
2
  import assert from "node:assert/strict";
3
+ import { spawnSync } from "node:child_process";
3
4
  import { existsSync } from "node:fs";
4
5
  import { join } from "node:path";
5
6
  import { answers, bind, cleanUpOnExit, dialogTrace, makeCtx, subCtx, textOf, useMocks } from "./helpers.mjs";
@@ -7,7 +8,8 @@ import { answers, bind, cleanUpOnExit, dialogTrace, makeCtx, subCtx, textOf, use
7
8
  // The extension is only a client: these drive the real ai-vault binary (built
8
9
  // by `npm test`) plus the mock zenity, with a throwaway store.
9
10
  const dir = useMocks();
10
- const { default: aiVault } = await import("../src/index.ts");
11
+ const { default: aiVault } = await import("../packages/pi-ai-vault/src/index.ts");
12
+ const { forgetSettings } = await import("../packages/pi-ai-vault/src/settings.ts");
11
13
  cleanUpOnExit(dir);
12
14
 
13
15
  const PW = "correct horse battery";
@@ -129,3 +131,152 @@ test("a missing entry names what is available", async () => {
129
131
  assert.match(textOf(result), /no such entry/);
130
132
  assert.match(textOf(result), /GitHub/);
131
133
  });
134
+
135
+ test("a local miss falls back to Bitwarden: read once, no import", async () => {
136
+ const b = bind(aiVault, true);
137
+ const ctx = makeCtx().ctx;
138
+ answers.master(dir, PW);
139
+ await b.cmd("unlock", ctx);
140
+ // The Bitwarden mock unlocks with "testmaster"; the two windows are the
141
+ // read approval (yes) and the import offer (no).
142
+ answers.master(dir, "testmaster");
143
+ answers.questions(dir, "y", "n");
144
+ const result = await b.tool("vault_get", { name: "TestService" }, ctx);
145
+ assert.equal(result.details.via, "bitwarden-once");
146
+ assert.match(textOf(result), /supersecret-value-123/);
147
+
148
+ answers.master(dir, PW);
149
+ const listed = await b.tool("vault_list", {}, ctx);
150
+ assert.equal(
151
+ JSON.parse(textOf(listed)).some((i) => i.name === "TestService"),
152
+ false,
153
+ "a one-off read must not write the entry",
154
+ );
155
+ });
156
+
157
+ test("a local miss can be imported from Bitwarden", async () => {
158
+ const b = bind(aiVault, true);
159
+ const ctx = makeCtx().ctx;
160
+ answers.master(dir, PW);
161
+ await b.cmd("unlock", ctx);
162
+ answers.master(dir, "testmaster");
163
+ answers.questions(dir, "y", "y");
164
+ const result = await b.tool("vault_get", { name: "TestService" }, ctx);
165
+ assert.equal(result.details.via, "imported-from-bitwarden");
166
+ assert.match(textOf(result), /supersecret-value-123/);
167
+
168
+ answers.master(dir, PW);
169
+ const listed = await b.tool("vault_list", {}, ctx);
170
+ assert.equal(
171
+ JSON.parse(textOf(listed)).some((i) => i.name === "TestService"),
172
+ true,
173
+ "the import must land in the local vault",
174
+ );
175
+ });
176
+
177
+ test("a denied Bitwarden read is not retried", async () => {
178
+ const b = bind(aiVault, true);
179
+ const ctx = makeCtx().ctx;
180
+ answers.master(dir, PW);
181
+ await b.cmd("unlock", ctx);
182
+ answers.master(dir, "testmaster");
183
+ answers.questions(dir, "n");
184
+ const result = await b.tool("vault_get", { name: "OtherService" }, ctx);
185
+ assert.equal(result.isError, true);
186
+ assert.match(textOf(result), /do not retry/);
187
+ });
188
+
189
+ test("never mode reads without any window, and the switch needs the password", async () => {
190
+ const bin = process.env.AI_VAULT_BIN;
191
+ const switchMode = (mode) =>
192
+ spawnSync(bin, ["mode", "set", mode, "--password-stdin"], {
193
+ input: `${PW}\n`,
194
+ env: process.env,
195
+ encoding: "utf8",
196
+ });
197
+ const toNever = switchMode("never");
198
+ assert.equal(toNever.status, 0, toNever.stderr);
199
+
200
+ const b = bind(aiVault, true);
201
+ const ctx = makeCtx().ctx;
202
+ const before = prompts("--question") + prompts("--password");
203
+ const result = await b.tool("vault_get", { name: "GitHub" }, ctx);
204
+ assert.equal(result.details.via, "no-password");
205
+ assert.match(textOf(result), /pw-one/);
206
+ assert.equal(prompts("--question") + prompts("--password"), before, "no window in never mode");
207
+
208
+ // A wrong password must not change the policy.
209
+ const wrong = spawnSync(bin, ["mode", "set", "session", "--password-stdin"], {
210
+ input: "definitely-wrong\n",
211
+ env: process.env,
212
+ encoding: "utf8",
213
+ });
214
+ assert.equal(wrong.status, 5);
215
+
216
+ const back = switchMode("session");
217
+ assert.equal(back.status, 0, back.stderr);
218
+ });
219
+
220
+ test("bitwarden.import=never reads once with a single window", async () => {
221
+ const bin = process.env.AI_VAULT_BIN;
222
+ spawnSync(bin, ["settings", "bitwarden.import", "never"], { env: process.env });
223
+ forgetSettings();
224
+ const b = bind(aiVault, true);
225
+ const ctx = makeCtx().ctx;
226
+ answers.master(dir, PW);
227
+ await b.cmd("unlock", ctx);
228
+ answers.master(dir, "testmaster");
229
+ const before = prompts("--question");
230
+ answers.questions(dir, "y");
231
+ const result = await b.tool("vault_get", { name: "OtherService" }, ctx);
232
+ assert.equal(result.details.via, "bitwarden-once");
233
+ assert.equal(prompts("--question"), before + 1, "never: read approval only, no import question");
234
+ });
235
+
236
+ test("bitwarden.import=always pulls the entry in without a window", async () => {
237
+ const bin = process.env.AI_VAULT_BIN;
238
+ spawnSync(bin, ["settings", "bitwarden.import", "always"], { env: process.env });
239
+ forgetSettings();
240
+ const b = bind(aiVault, true);
241
+ const ctx = makeCtx().ctx;
242
+ answers.master(dir, PW);
243
+ await b.cmd("unlock", ctx);
244
+ answers.master(dir, "testmaster");
245
+ const before = prompts("--question");
246
+ const result = await b.tool("vault_get", { name: "OtherService" }, ctx);
247
+ assert.equal(result.details.via, "imported-from-bitwarden");
248
+ assert.equal(prompts("--question"), before, "always: no window at all");
249
+
250
+ answers.master(dir, PW);
251
+ const listed = await b.tool("vault_list", {}, ctx);
252
+ assert.equal(
253
+ JSON.parse(textOf(listed)).some((i) => i.name === "OtherService"),
254
+ true,
255
+ );
256
+ spawnSync(bin, ["settings", "bitwarden.import", "ask"], { env: process.env });
257
+ forgetSettings();
258
+ });
259
+
260
+ test("a persisted Bitwarden session costs no password", async () => {
261
+ const bin = process.env.AI_VAULT_BIN;
262
+ const set = (key, value) => spawnSync(bin, ["settings", key, value], { env: process.env, encoding: "utf8" });
263
+ assert.equal(set("bitwarden.unlock", "never").status, 0);
264
+ forgetSettings();
265
+ const put = spawnSync(bin, ["bw-session", "put"], { input: "GOOD_SESSION", env: process.env, encoding: "utf8" });
266
+ assert.equal(put.status, 0, put.stderr);
267
+
268
+ const b = bind(aiVault, true);
269
+ const ctx = makeCtx().ctx;
270
+ answers.master(dir, PW);
271
+ answers.master(dir, PW); // would be consumed only by a Bitwarden prompt
272
+ const before = prompts("--password");
273
+ const listed = await b.tool("bw_list", {}, ctx);
274
+ assert.equal(listed.isError, undefined);
275
+ assert.equal(prompts("--password"), before, "the stored bw session must be used silently");
276
+
277
+ // Restoring the session policy drops the key from disk.
278
+ assert.equal(set("bitwarden.unlock", "session").status, 0);
279
+ forgetSettings();
280
+ const get = spawnSync(bin, ["bw-session", "get"], { env: process.env, encoding: "utf8" });
281
+ assert.equal(get.status, 5, "switching back to session must remove the stored key");
282
+ });
File without changes
File without changes
File without changes
File without changes