- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/test/key.test.ts?version=273&format=json
60 linesFirst import 95% · pinch 5%Last change pinch in version 18, 3 versions changed it
You're reading version 273. The newest is 318, and no version since then changed this file.
1import assert from "node:assert/strict";
2import { mkdtempSync, readFileSync, writeFileSync } from "node:fs";
3import { tmpdir } from "node:os";
4import { join } from "node:path";
5import { test } from "node:test";
6import { credentialLabel, installKey, tokenFor, writeConfig } from "../src/config.ts";
8function folder() {
9 const root = mkdtempSync(join(tmpdir(), "abso-key-"));
10 writeConfig(root, { server: "https://staging.abso.example", project: "p", agent: "a" });
11 return root;
12}
14const KEY = "abso_sk_abcdefghijklmnopqrstuvwxyz012345";
15const ID = "k_ab12cd34";
17test("a key in the folder is sent instead of AVCS_TOKEN, and only to its server", () => {
18 const root = folder();
19 installKey(root, JSON.stringify({ key: KEY, id: ID }));
20 const env = { AVCS_TOKEN: "shared-token-value" };
21 assert.equal(tokenFor("https://staging.abso.example", env, root), KEY);
22 assert.throws(() => tokenFor("https://abso.example", env, root), /different server/);
23 const label = credentialLabel(root, "https://staging.abso.example", env);
24 assert.deepEqual(label, { kind: "agent key", id: ID });
25 assert.equal(JSON.stringify(label).includes(KEY), false);
26});
28test("with no key, the shared token is unchanged", () => {
29 const root = folder();
30 const env = {
31 AVCS_TOKEN: "shared-token-value",
32 AVCS_TRUSTED_SERVERS: "https://abso.example",
33 AVCS_SERVERS_FILE: "no-servers.json",
34 };
35 assert.equal(tokenFor("https://abso.example", env, root), "shared-token-value");
36 assert.deepEqual(credentialLabel(root, "https://abso.example", env), { kind: "shared token", id: null });
37});
39test("install writes .avcs/key and does not leave the key in the returned value", () => {
40 const root = folder();
41 const installed = installKey(root, JSON.stringify({ key: KEY, id: ID }));
42 assert.deepEqual(installed, { id: ID });
43 const stored = readFileSync(join(root, ".avcs", "key"), "utf8");
44 assert.equal(stored.includes(KEY), true);
45 assert.equal(JSON.stringify(installed).includes(KEY), false);
46 writeFileSync(join(root, ".avcs", "key"), stored);
47});
49test("a key installed with --server before join is the one join's folder sends", () => {
50 const root = mkdtempSync(join(tmpdir(), "abso-key-"));
51 const staging = "https://staging.abso.example";
52 assert.throws(() => installKey(root, JSON.stringify({ key: KEY, id: ID })), /--server/);
53 installKey(root, JSON.stringify({ key: KEY, id: ID }), staging);
54 writeConfig(root, { server: staging, project: "p", agent: "a" });
55 assert.equal(tokenFor(staging, {}, root), KEY);
56 assert.throws(
57 () => installKey(root, JSON.stringify({ key: KEY, id: ID }), "https://abso.example"),
58 /not https:\/\/abso.example/,
59 );
60});