@indigoai-us/hq-cloud 5.1.0 → 5.1.8
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.
- package/dist/bin/sync-runner.d.ts +111 -0
- package/dist/bin/sync-runner.d.ts.map +1 -0
- package/dist/bin/sync-runner.js +285 -0
- package/dist/bin/sync-runner.js.map +1 -0
- package/dist/bin/sync-runner.test.d.ts +10 -0
- package/dist/bin/sync-runner.test.d.ts.map +1 -0
- package/dist/bin/sync-runner.test.js +492 -0
- package/dist/bin/sync-runner.test.js.map +1 -0
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/share.js +2 -2
- package/dist/cli/share.js.map +1 -1
- package/dist/cli/share.test.js +9 -1
- package/dist/cli/share.test.js.map +1 -1
- package/dist/cli/sync.d.ts +28 -0
- package/dist/cli/sync.d.ts.map +1 -1
- package/dist/cli/sync.js +33 -10
- package/dist/cli/sync.js.map +1 -1
- package/dist/cli/sync.test.js +15 -4
- package/dist/cli/sync.test.js.map +1 -1
- package/dist/cognito-auth.d.ts.map +1 -1
- package/dist/cognito-auth.js +19 -1
- package/dist/cognito-auth.js.map +1 -1
- package/dist/cognito-auth.test.d.ts +9 -0
- package/dist/cognito-auth.test.d.ts.map +1 -0
- package/dist/cognito-auth.test.js +113 -0
- package/dist/cognito-auth.test.js.map +1 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +1 -0
- package/dist/context.js.map +1 -1
- package/dist/daemon-worker.d.ts +6 -1
- package/dist/daemon-worker.d.ts.map +1 -1
- package/dist/daemon-worker.js +12 -16
- package/dist/daemon-worker.js.map +1 -1
- package/dist/daemon.d.ts +2 -0
- package/dist/daemon.d.ts.map +1 -1
- package/dist/daemon.js +2 -0
- package/dist/daemon.js.map +1 -1
- package/dist/ignore.d.ts +13 -2
- package/dist/ignore.d.ts.map +1 -1
- package/dist/ignore.js +69 -12
- package/dist/ignore.js.map +1 -1
- package/dist/index.d.ts +24 -28
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +19 -134
- package/dist/index.js.map +1 -1
- package/dist/journal.d.ts +20 -4
- package/dist/journal.d.ts.map +1 -1
- package/dist/journal.js +45 -8
- package/dist/journal.js.map +1 -1
- package/dist/journal.test.d.ts +9 -0
- package/dist/journal.test.d.ts.map +1 -0
- package/dist/journal.test.js +114 -0
- package/dist/journal.test.js.map +1 -0
- package/dist/s3.d.ts +18 -6
- package/dist/s3.d.ts.map +1 -1
- package/dist/s3.js +57 -56
- package/dist/s3.js.map +1 -1
- package/dist/types.d.ts +34 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/vault-client.d.ts +16 -0
- package/dist/vault-client.d.ts.map +1 -1
- package/dist/vault-client.js +19 -0
- package/dist/vault-client.js.map +1 -1
- package/dist/vault-client.test.js +25 -0
- package/dist/vault-client.test.js.map +1 -1
- package/dist/watcher.d.ts +7 -1
- package/dist/watcher.d.ts.map +1 -1
- package/dist/watcher.js +11 -5
- package/dist/watcher.js.map +1 -1
- package/package.json +15 -3
- package/src/bin/sync-runner.test.ts +617 -0
- package/src/bin/sync-runner.ts +390 -0
- package/src/cli/accept.ts +97 -0
- package/src/cli/conflict.ts +119 -0
- package/src/cli/index.ts +25 -0
- package/src/cli/invite.test.ts +247 -0
- package/src/cli/invite.ts +180 -0
- package/src/cli/promote.ts +123 -0
- package/src/cli/share.test.ts +155 -0
- package/src/cli/share.ts +212 -0
- package/src/cli/sync.test.ts +225 -0
- package/src/cli/sync.ts +225 -0
- package/src/cognito-auth.test.ts +156 -0
- package/src/cognito-auth.ts +18 -1
- package/src/context.test.ts +202 -0
- package/src/context.ts +178 -0
- package/src/daemon-worker.ts +13 -19
- package/src/daemon.ts +2 -0
- package/src/ignore.ts +76 -12
- package/src/index.ts +93 -165
- package/src/journal.test.ts +146 -0
- package/src/journal.ts +53 -11
- package/src/s3.ts +76 -66
- package/src/types.ts +37 -0
- package/src/vault-client.test.ts +390 -0
- package/src/vault-client.ts +400 -0
- package/src/watcher.ts +12 -5
- package/test/invite-flow.integration.test.ts +244 -0
- package/test/share-sync.integration.test.ts +210 -0
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* invite CLI command tests (VLT-7 US-002).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect, vi, beforeEach, afterEach, type MockInstance } from "vitest";
|
|
6
|
+
import { invite, listInvites, revokeInvite } from "./invite.js";
|
|
7
|
+
import type { VaultServiceConfig } from "../types.js";
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Helpers
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
function jsonResponse(status: number, body: unknown): Response {
|
|
14
|
+
return new Response(JSON.stringify(body), {
|
|
15
|
+
status,
|
|
16
|
+
headers: { "Content-Type": "application/json" },
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const VAULT_CONFIG: VaultServiceConfig = {
|
|
21
|
+
apiUrl: "https://vault.test.example.com",
|
|
22
|
+
authToken: "test-token",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
let fetchSpy: MockInstance<typeof fetch>;
|
|
26
|
+
|
|
27
|
+
beforeEach(() => {
|
|
28
|
+
fetchSpy = vi.spyOn(globalThis, "fetch");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
afterEach(() => {
|
|
32
|
+
vi.restoreAllMocks();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// invite()
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
|
|
39
|
+
describe("invite", () => {
|
|
40
|
+
it("creates invite for email target and returns magic link", async () => {
|
|
41
|
+
// First call: entity.findBySlug to resolve company
|
|
42
|
+
fetchSpy
|
|
43
|
+
.mockResolvedValueOnce(
|
|
44
|
+
jsonResponse(200, { entity: { uid: "cmp_abc", slug: "acme", type: "company", status: "active" } }),
|
|
45
|
+
)
|
|
46
|
+
// Second call: createInvite
|
|
47
|
+
.mockResolvedValueOnce(
|
|
48
|
+
jsonResponse(200, {
|
|
49
|
+
membership: { membershipKey: "psn_1#cmp_abc", role: "member", status: "pending" },
|
|
50
|
+
inviteToken: "tok_secure123",
|
|
51
|
+
}),
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
const result = await invite({
|
|
55
|
+
target: "alice@example.com",
|
|
56
|
+
role: "member",
|
|
57
|
+
company: "acme",
|
|
58
|
+
vaultConfig: VAULT_CONFIG,
|
|
59
|
+
callerUid: "psn_admin",
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
expect(result.magicLink).toBe("hq://accept/tok_secure123");
|
|
63
|
+
expect(result.inviteToken).toBe("tok_secure123");
|
|
64
|
+
expect(result.membership.status).toBe("pending");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("creates invite for person UID target", async () => {
|
|
68
|
+
// Company is already a UID — no entity lookup needed
|
|
69
|
+
fetchSpy.mockResolvedValueOnce(
|
|
70
|
+
jsonResponse(200, {
|
|
71
|
+
membership: { membershipKey: "psn_bob#cmp_abc", role: "admin", status: "pending" },
|
|
72
|
+
inviteToken: "tok_456",
|
|
73
|
+
}),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
const result = await invite({
|
|
77
|
+
target: "psn_bob",
|
|
78
|
+
role: "admin",
|
|
79
|
+
company: "cmp_abc",
|
|
80
|
+
vaultConfig: VAULT_CONFIG,
|
|
81
|
+
callerUid: "psn_admin",
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
expect(result.magicLink).toBe("hq://accept/tok_456");
|
|
85
|
+
|
|
86
|
+
// Should have called createInvite with personUid, not inviteeEmail
|
|
87
|
+
const body = JSON.parse(fetchSpy.mock.calls[0][1]?.body as string);
|
|
88
|
+
expect(body.personUid).toBe("psn_bob");
|
|
89
|
+
expect(body.inviteeEmail).toBeUndefined();
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("rejects --paths without --role guest", async () => {
|
|
93
|
+
await expect(
|
|
94
|
+
invite({
|
|
95
|
+
target: "alice@example.com",
|
|
96
|
+
role: "member",
|
|
97
|
+
paths: "docs/",
|
|
98
|
+
company: "acme",
|
|
99
|
+
vaultConfig: VAULT_CONFIG,
|
|
100
|
+
callerUid: "psn_admin",
|
|
101
|
+
}),
|
|
102
|
+
).rejects.toThrow("--paths is only valid with --role guest");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("allows --paths with --role guest", async () => {
|
|
106
|
+
fetchSpy
|
|
107
|
+
.mockResolvedValueOnce(
|
|
108
|
+
jsonResponse(200, { entity: { uid: "cmp_abc", slug: "acme", type: "company", status: "active" } }),
|
|
109
|
+
)
|
|
110
|
+
.mockResolvedValueOnce(
|
|
111
|
+
jsonResponse(200, {
|
|
112
|
+
membership: { membershipKey: "psn_1#cmp_abc", role: "guest", status: "pending", allowedPrefixes: ["docs/", "shared/"] },
|
|
113
|
+
inviteToken: "tok_guest",
|
|
114
|
+
}),
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
const result = await invite({
|
|
118
|
+
target: "alice@example.com",
|
|
119
|
+
role: "guest",
|
|
120
|
+
paths: "docs/, shared/",
|
|
121
|
+
company: "acme",
|
|
122
|
+
vaultConfig: VAULT_CONFIG,
|
|
123
|
+
callerUid: "psn_admin",
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
expect(result.membership.allowedPrefixes).toEqual(["docs/", "shared/"]);
|
|
127
|
+
|
|
128
|
+
// Verify allowedPrefixes sent to API
|
|
129
|
+
const body = JSON.parse(fetchSpy.mock.calls[1][1]?.body as string);
|
|
130
|
+
expect(body.allowedPrefixes).toEqual(["docs/", "shared/"]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("maps VaultPermissionDeniedError to human-readable message", async () => {
|
|
134
|
+
fetchSpy
|
|
135
|
+
.mockResolvedValueOnce(
|
|
136
|
+
jsonResponse(200, { entity: { uid: "cmp_abc", slug: "acme", type: "company", status: "active" } }),
|
|
137
|
+
)
|
|
138
|
+
.mockResolvedValueOnce(
|
|
139
|
+
jsonResponse(403, { message: "Admin required" }),
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
await expect(
|
|
143
|
+
invite({
|
|
144
|
+
target: "alice@example.com",
|
|
145
|
+
company: "acme",
|
|
146
|
+
vaultConfig: VAULT_CONFIG,
|
|
147
|
+
callerUid: "psn_member",
|
|
148
|
+
}),
|
|
149
|
+
).rejects.toThrow("Permission denied — only admins and owners can invite members");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("throws when no company specified", async () => {
|
|
153
|
+
await expect(
|
|
154
|
+
invite({
|
|
155
|
+
target: "alice@example.com",
|
|
156
|
+
vaultConfig: VAULT_CONFIG,
|
|
157
|
+
callerUid: "psn_admin",
|
|
158
|
+
}),
|
|
159
|
+
).rejects.toThrow("No company specified");
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("maps VaultConflictError to human-readable message", async () => {
|
|
163
|
+
fetchSpy
|
|
164
|
+
.mockResolvedValueOnce(
|
|
165
|
+
jsonResponse(200, { entity: { uid: "cmp_abc", slug: "acme", type: "company", status: "active" } }),
|
|
166
|
+
)
|
|
167
|
+
.mockResolvedValueOnce(
|
|
168
|
+
jsonResponse(409, { message: "Already exists" }),
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
await expect(
|
|
172
|
+
invite({
|
|
173
|
+
target: "alice@example.com",
|
|
174
|
+
company: "acme",
|
|
175
|
+
vaultConfig: VAULT_CONFIG,
|
|
176
|
+
callerUid: "psn_admin",
|
|
177
|
+
}),
|
|
178
|
+
).rejects.toThrow("already has a membership or pending invite");
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// ---------------------------------------------------------------------------
|
|
183
|
+
// listInvites()
|
|
184
|
+
// ---------------------------------------------------------------------------
|
|
185
|
+
|
|
186
|
+
describe("listInvites", () => {
|
|
187
|
+
it("returns pending invites for a company", async () => {
|
|
188
|
+
fetchSpy
|
|
189
|
+
.mockResolvedValueOnce(
|
|
190
|
+
jsonResponse(200, { entity: { uid: "cmp_abc", slug: "acme", type: "company", status: "active" } }),
|
|
191
|
+
)
|
|
192
|
+
.mockResolvedValueOnce(
|
|
193
|
+
jsonResponse(200, {
|
|
194
|
+
invites: [
|
|
195
|
+
{ membershipKey: "psn_1#cmp_abc", status: "pending", role: "member" },
|
|
196
|
+
{ membershipKey: "psn_2#cmp_abc", status: "pending", role: "guest" },
|
|
197
|
+
],
|
|
198
|
+
}),
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
const invites = await listInvites({
|
|
202
|
+
company: "acme",
|
|
203
|
+
vaultConfig: VAULT_CONFIG,
|
|
204
|
+
callerUid: "psn_admin",
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
expect(invites).toHaveLength(2);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// ---------------------------------------------------------------------------
|
|
212
|
+
// revokeInvite()
|
|
213
|
+
// ---------------------------------------------------------------------------
|
|
214
|
+
|
|
215
|
+
describe("revokeInvite", () => {
|
|
216
|
+
it("revokes a pending invite", async () => {
|
|
217
|
+
fetchSpy
|
|
218
|
+
.mockResolvedValueOnce(
|
|
219
|
+
jsonResponse(200, { entity: { uid: "cmp_abc", slug: "acme", type: "company", status: "active" } }),
|
|
220
|
+
)
|
|
221
|
+
.mockResolvedValueOnce(new Response(null, { status: 204 }));
|
|
222
|
+
|
|
223
|
+
await expect(
|
|
224
|
+
revokeInvite({
|
|
225
|
+
tokenOrKey: "psn_1#cmp_abc",
|
|
226
|
+
company: "acme",
|
|
227
|
+
vaultConfig: VAULT_CONFIG,
|
|
228
|
+
}),
|
|
229
|
+
).resolves.toBeUndefined();
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("maps 404 to human-readable message", async () => {
|
|
233
|
+
fetchSpy
|
|
234
|
+
.mockResolvedValueOnce(
|
|
235
|
+
jsonResponse(200, { entity: { uid: "cmp_abc", slug: "acme", type: "company", status: "active" } }),
|
|
236
|
+
)
|
|
237
|
+
.mockResolvedValueOnce(jsonResponse(404, { message: "Not found" }));
|
|
238
|
+
|
|
239
|
+
await expect(
|
|
240
|
+
revokeInvite({
|
|
241
|
+
tokenOrKey: "psn_1#cmp_abc",
|
|
242
|
+
company: "acme",
|
|
243
|
+
vaultConfig: VAULT_CONFIG,
|
|
244
|
+
}),
|
|
245
|
+
).rejects.toThrow("Invite not found");
|
|
246
|
+
});
|
|
247
|
+
});
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `hq invite` command — create pending membership + magic link (VLT-7 US-002).
|
|
3
|
+
*
|
|
4
|
+
* Thin UX layer over VaultClient.createInvite(). Handles arg parsing,
|
|
5
|
+
* validation (paths only with guest role), and formats the magic link output.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { VaultServiceConfig } from "../types.js";
|
|
9
|
+
import {
|
|
10
|
+
VaultClient,
|
|
11
|
+
VaultAuthError,
|
|
12
|
+
VaultPermissionDeniedError,
|
|
13
|
+
VaultNotFoundError,
|
|
14
|
+
VaultConflictError,
|
|
15
|
+
} from "../vault-client.js";
|
|
16
|
+
import type { MembershipRole, Membership } from "../vault-client.js";
|
|
17
|
+
|
|
18
|
+
export interface InviteOptions {
|
|
19
|
+
/** Target — email address or person slug/uid */
|
|
20
|
+
target: string;
|
|
21
|
+
/** Role for the invitee (default: member) */
|
|
22
|
+
role?: MembershipRole;
|
|
23
|
+
/** Comma-separated allowed prefixes (only valid with role=guest) */
|
|
24
|
+
paths?: string;
|
|
25
|
+
/** Company slug or UID (defaults to active company) */
|
|
26
|
+
company?: string;
|
|
27
|
+
/** Vault service config */
|
|
28
|
+
vaultConfig: VaultServiceConfig;
|
|
29
|
+
/** Caller's person UID */
|
|
30
|
+
callerUid: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface InviteResult {
|
|
34
|
+
inviteToken: string;
|
|
35
|
+
magicLink: string;
|
|
36
|
+
membership: Membership;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface InviteListOptions {
|
|
40
|
+
company?: string;
|
|
41
|
+
vaultConfig: VaultServiceConfig;
|
|
42
|
+
callerUid: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface InviteRevokeOptions {
|
|
46
|
+
tokenOrKey: string;
|
|
47
|
+
/** Company slug or UID — required so the server can authorize the caller */
|
|
48
|
+
company: string;
|
|
49
|
+
vaultConfig: VaultServiceConfig;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Create a pending membership invite and return a magic link.
|
|
54
|
+
*/
|
|
55
|
+
export async function invite(options: InviteOptions): Promise<InviteResult> {
|
|
56
|
+
const { target, role = "member", paths, company, vaultConfig, callerUid } = options;
|
|
57
|
+
|
|
58
|
+
// Validate: --paths only with --role guest
|
|
59
|
+
if (paths && role !== "guest") {
|
|
60
|
+
throw new Error("--paths is only valid with --role guest (allowedPrefixes are only meaningful for the guest role)");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const client = new VaultClient(vaultConfig);
|
|
64
|
+
|
|
65
|
+
// Resolve company UID
|
|
66
|
+
const companyUid = await resolveCompanyUid(client, company);
|
|
67
|
+
|
|
68
|
+
// Parse paths
|
|
69
|
+
const allowedPrefixes = paths
|
|
70
|
+
? paths.split(",").map((p) => p.trim()).filter(Boolean)
|
|
71
|
+
: undefined;
|
|
72
|
+
|
|
73
|
+
// Determine if target is email or person identifier
|
|
74
|
+
const isEmail = target.includes("@");
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
const result = await client.createInvite({
|
|
78
|
+
...(isEmail ? { inviteeEmail: target } : { personUid: target }),
|
|
79
|
+
companyUid,
|
|
80
|
+
role,
|
|
81
|
+
allowedPrefixes,
|
|
82
|
+
invitedBy: callerUid,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const magicLink = `hq://accept/${result.inviteToken}`;
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
inviteToken: result.inviteToken,
|
|
89
|
+
magicLink,
|
|
90
|
+
membership: result.membership,
|
|
91
|
+
};
|
|
92
|
+
} catch (err) {
|
|
93
|
+
if (err instanceof VaultAuthError) {
|
|
94
|
+
throw new Error("Authentication failed — run `hq auth` to refresh your session");
|
|
95
|
+
}
|
|
96
|
+
if (err instanceof VaultPermissionDeniedError) {
|
|
97
|
+
throw new Error("Permission denied — only admins and owners can invite members");
|
|
98
|
+
}
|
|
99
|
+
if (err instanceof VaultConflictError) {
|
|
100
|
+
throw new Error("This person already has a membership or pending invite for this company");
|
|
101
|
+
}
|
|
102
|
+
throw err;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* List pending invites for a company.
|
|
108
|
+
*/
|
|
109
|
+
export async function listInvites(options: InviteListOptions): Promise<Membership[]> {
|
|
110
|
+
const { company, vaultConfig } = options;
|
|
111
|
+
const client = new VaultClient(vaultConfig);
|
|
112
|
+
const companyUid = await resolveCompanyUid(client, company);
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
return await client.listPendingInvites(companyUid);
|
|
116
|
+
} catch (err) {
|
|
117
|
+
if (err instanceof VaultAuthError) {
|
|
118
|
+
throw new Error("Authentication failed — run `hq auth` to refresh your session");
|
|
119
|
+
}
|
|
120
|
+
if (err instanceof VaultPermissionDeniedError) {
|
|
121
|
+
throw new Error("Permission denied — only admins and owners can list invites");
|
|
122
|
+
}
|
|
123
|
+
throw err;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Revoke a pending invite.
|
|
129
|
+
*/
|
|
130
|
+
export async function revokeInvite(options: InviteRevokeOptions): Promise<void> {
|
|
131
|
+
const { tokenOrKey, company, vaultConfig } = options;
|
|
132
|
+
const client = new VaultClient(vaultConfig);
|
|
133
|
+
const companyUid = await resolveCompanyUid(client, company);
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
await client.revokeMembership(tokenOrKey, companyUid);
|
|
137
|
+
} catch (err) {
|
|
138
|
+
if (err instanceof VaultAuthError) {
|
|
139
|
+
throw new Error("Authentication failed — run `hq auth` to refresh your session");
|
|
140
|
+
}
|
|
141
|
+
if (err instanceof VaultPermissionDeniedError) {
|
|
142
|
+
throw new Error("Permission denied — only admins and owners can revoke invites");
|
|
143
|
+
}
|
|
144
|
+
if (err instanceof VaultNotFoundError) {
|
|
145
|
+
throw new Error("Invite not found — it may have already been accepted or revoked");
|
|
146
|
+
}
|
|
147
|
+
throw err;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ---------------------------------------------------------------------------
|
|
152
|
+
// Helpers
|
|
153
|
+
// ---------------------------------------------------------------------------
|
|
154
|
+
|
|
155
|
+
async function resolveCompanyUid(
|
|
156
|
+
client: VaultClient,
|
|
157
|
+
companyRef?: string,
|
|
158
|
+
): Promise<string> {
|
|
159
|
+
if (!companyRef) {
|
|
160
|
+
throw new Error(
|
|
161
|
+
"No company specified. Use --company <slug> or set up .hq/config.json",
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// If already a UID, return it
|
|
166
|
+
if (companyRef.startsWith("cmp_")) {
|
|
167
|
+
return companyRef;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Resolve slug → UID via entity registry
|
|
171
|
+
try {
|
|
172
|
+
const entity = await client.entity.findBySlug("company", companyRef);
|
|
173
|
+
return entity.uid;
|
|
174
|
+
} catch (err) {
|
|
175
|
+
if (err instanceof VaultNotFoundError) {
|
|
176
|
+
throw new Error(`Company "${companyRef}" not found in the vault registry`);
|
|
177
|
+
}
|
|
178
|
+
throw err;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `hq promote` command — change an existing member's role (VLT-7 US-003).
|
|
3
|
+
*
|
|
4
|
+
* Admin+ only. Surfaces last-owner demotion errors as human messages.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { VaultServiceConfig } from "../types.js";
|
|
8
|
+
import {
|
|
9
|
+
VaultClient,
|
|
10
|
+
VaultAuthError,
|
|
11
|
+
VaultPermissionDeniedError,
|
|
12
|
+
VaultNotFoundError,
|
|
13
|
+
VaultConflictError,
|
|
14
|
+
} from "../vault-client.js";
|
|
15
|
+
import type { MembershipRole, Membership } from "../vault-client.js";
|
|
16
|
+
|
|
17
|
+
export interface PromoteOptions {
|
|
18
|
+
/** Person slug or UID of the member to promote */
|
|
19
|
+
target: string;
|
|
20
|
+
/** New role to assign */
|
|
21
|
+
newRole: MembershipRole;
|
|
22
|
+
/** Allowed prefixes (only valid with guest role) */
|
|
23
|
+
paths?: string;
|
|
24
|
+
/** Company slug or UID */
|
|
25
|
+
company?: string;
|
|
26
|
+
/** Caller's person UID */
|
|
27
|
+
callerUid: string;
|
|
28
|
+
/** Vault service config */
|
|
29
|
+
vaultConfig: VaultServiceConfig;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface PromoteResult {
|
|
33
|
+
membership: Membership;
|
|
34
|
+
previousRole?: MembershipRole;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Change a member's role.
|
|
39
|
+
*/
|
|
40
|
+
export async function promote(options: PromoteOptions): Promise<PromoteResult> {
|
|
41
|
+
const { target, newRole, paths, company, callerUid, vaultConfig } = options;
|
|
42
|
+
|
|
43
|
+
// Validate: --paths only with guest role
|
|
44
|
+
if (paths && newRole !== "guest") {
|
|
45
|
+
throw new Error("--paths is only valid with --role guest (allowedPrefixes are only meaningful for the guest role)");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const client = new VaultClient(vaultConfig);
|
|
49
|
+
|
|
50
|
+
// Resolve company UID
|
|
51
|
+
const companyUid = await resolveCompanyUid(client, company);
|
|
52
|
+
|
|
53
|
+
// Build membership key from target + company
|
|
54
|
+
const membershipKey = buildMembershipKey(target, companyUid);
|
|
55
|
+
|
|
56
|
+
const allowedPrefixes = paths
|
|
57
|
+
? paths.split(",").map((p) => p.trim()).filter(Boolean)
|
|
58
|
+
: undefined;
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
const membership = await client.updateRole({
|
|
62
|
+
membershipKey,
|
|
63
|
+
newRole,
|
|
64
|
+
allowedPrefixes,
|
|
65
|
+
updaterUid: callerUid,
|
|
66
|
+
companyUid,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
return { membership };
|
|
70
|
+
} catch (err) {
|
|
71
|
+
if (err instanceof VaultAuthError) {
|
|
72
|
+
throw new Error("Authentication failed — run `hq auth` to refresh your session");
|
|
73
|
+
}
|
|
74
|
+
if (err instanceof VaultPermissionDeniedError) {
|
|
75
|
+
throw new Error("Permission denied — only admins and owners can change member roles");
|
|
76
|
+
}
|
|
77
|
+
if (err instanceof VaultNotFoundError) {
|
|
78
|
+
throw new Error(`Member "${target}" not found in this company`);
|
|
79
|
+
}
|
|
80
|
+
if (err instanceof VaultConflictError) {
|
|
81
|
+
throw new Error("Cannot leave company without an owner — promote another member to owner first");
|
|
82
|
+
}
|
|
83
|
+
throw err;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ---------------------------------------------------------------------------
|
|
88
|
+
// Helpers
|
|
89
|
+
// ---------------------------------------------------------------------------
|
|
90
|
+
|
|
91
|
+
function buildMembershipKey(personRef: string, companyUid: string): string {
|
|
92
|
+
// If already a composite key, use as-is
|
|
93
|
+
if (personRef.includes("#")) {
|
|
94
|
+
return personRef;
|
|
95
|
+
}
|
|
96
|
+
// Build composite key: personUid#companyUid
|
|
97
|
+
return `${personRef}#${companyUid}`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function resolveCompanyUid(
|
|
101
|
+
client: VaultClient,
|
|
102
|
+
companyRef?: string,
|
|
103
|
+
): Promise<string> {
|
|
104
|
+
if (!companyRef) {
|
|
105
|
+
throw new Error(
|
|
106
|
+
"No company specified. Use --company <slug> or set up .hq/config.json",
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (companyRef.startsWith("cmp_")) {
|
|
111
|
+
return companyRef;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
const entity = await client.entity.findBySlug("company", companyRef);
|
|
116
|
+
return entity.uid;
|
|
117
|
+
} catch (err) {
|
|
118
|
+
if (err instanceof VaultNotFoundError) {
|
|
119
|
+
throw new Error(`Company "${companyRef}" not found in the vault registry`);
|
|
120
|
+
}
|
|
121
|
+
throw err;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for hq share command (VLT-5 US-002).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
6
|
+
import * as fs from "fs";
|
|
7
|
+
import * as path from "path";
|
|
8
|
+
import * as os from "os";
|
|
9
|
+
import { clearContextCache } from "../context.js";
|
|
10
|
+
import type { VaultServiceConfig } from "../types.js";
|
|
11
|
+
|
|
12
|
+
// Mock s3 module at the top level
|
|
13
|
+
vi.mock("../s3.js", () => ({
|
|
14
|
+
uploadFile: vi.fn().mockResolvedValue(undefined),
|
|
15
|
+
downloadFile: vi.fn().mockResolvedValue(undefined),
|
|
16
|
+
listRemoteFiles: vi.fn().mockResolvedValue([]),
|
|
17
|
+
deleteRemoteFile: vi.fn().mockResolvedValue(undefined),
|
|
18
|
+
headRemoteFile: vi.fn().mockResolvedValue(null),
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
import { share } from "./share.js";
|
|
22
|
+
import { headRemoteFile } from "../s3.js";
|
|
23
|
+
|
|
24
|
+
const mockConfig: VaultServiceConfig = {
|
|
25
|
+
apiUrl: "https://vault-api.test",
|
|
26
|
+
authToken: "test-jwt-token",
|
|
27
|
+
region: "us-east-1",
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const mockEntity = {
|
|
31
|
+
uid: "cmp_01ABCDEF",
|
|
32
|
+
slug: "acme",
|
|
33
|
+
bucketName: "hq-vault-acme-123",
|
|
34
|
+
status: "active",
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const mockVendResponse = {
|
|
38
|
+
credentials: {
|
|
39
|
+
accessKeyId: "ASIA_TEST_KEY",
|
|
40
|
+
secretAccessKey: "test-secret",
|
|
41
|
+
sessionToken: "test-session-token",
|
|
42
|
+
expiration: new Date(Date.now() + 15 * 60 * 1000).toISOString(),
|
|
43
|
+
},
|
|
44
|
+
expiresAt: new Date(Date.now() + 15 * 60 * 1000).toISOString(),
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
function setupFetchMock() {
|
|
48
|
+
const fetchMock = vi.fn().mockImplementation(async (url: string) => {
|
|
49
|
+
const urlStr = String(url);
|
|
50
|
+
if (urlStr.includes("/entity/by-slug/")) {
|
|
51
|
+
return { ok: true, status: 200, json: async () => ({ entity: mockEntity }), text: async () => "" };
|
|
52
|
+
}
|
|
53
|
+
if (urlStr.includes("/sts/vend")) {
|
|
54
|
+
return { ok: true, status: 200, json: async () => mockVendResponse, text: async () => "" };
|
|
55
|
+
}
|
|
56
|
+
return { ok: false, status: 404, text: async () => "Not found" };
|
|
57
|
+
});
|
|
58
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
59
|
+
return fetchMock;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
describe("share", () => {
|
|
63
|
+
let tmpDir: string;
|
|
64
|
+
let stateDir: string;
|
|
65
|
+
|
|
66
|
+
beforeEach(() => {
|
|
67
|
+
clearContextCache();
|
|
68
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "hq-share-test-"));
|
|
69
|
+
// Redirect per-company journal into tmp so share() doesn't write to the
|
|
70
|
+
// real ~/.hq during tests (ADR-0001 Phase 5).
|
|
71
|
+
stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "hq-state-test-"));
|
|
72
|
+
process.env.HQ_STATE_DIR = stateDir;
|
|
73
|
+
setupFetchMock();
|
|
74
|
+
vi.mocked(headRemoteFile).mockResolvedValue(null);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
afterEach(() => {
|
|
78
|
+
vi.unstubAllGlobals();
|
|
79
|
+
vi.clearAllMocks();
|
|
80
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
81
|
+
fs.rmSync(stateDir, { recursive: true, force: true });
|
|
82
|
+
delete process.env.HQ_STATE_DIR;
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("shares a single file", async () => {
|
|
86
|
+
const testFile = path.join(tmpDir, "test.md");
|
|
87
|
+
fs.writeFileSync(testFile, "# Hello World");
|
|
88
|
+
|
|
89
|
+
const result = await share({
|
|
90
|
+
paths: [testFile],
|
|
91
|
+
company: "acme",
|
|
92
|
+
vaultConfig: mockConfig,
|
|
93
|
+
hqRoot: tmpDir,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
expect(result.filesUploaded).toBe(1);
|
|
97
|
+
expect(result.aborted).toBe(false);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("respects ignore rules", async () => {
|
|
101
|
+
fs.mkdirSync(path.join(tmpDir, ".git"));
|
|
102
|
+
fs.writeFileSync(path.join(tmpDir, ".git", "config"), "git config");
|
|
103
|
+
fs.writeFileSync(path.join(tmpDir, "readme.md"), "readme");
|
|
104
|
+
|
|
105
|
+
const result = await share({
|
|
106
|
+
paths: [tmpDir],
|
|
107
|
+
company: "acme",
|
|
108
|
+
vaultConfig: mockConfig,
|
|
109
|
+
hqRoot: tmpDir,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
expect(result.filesUploaded).toBe(1);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("shares a directory of files", async () => {
|
|
116
|
+
fs.mkdirSync(path.join(tmpDir, "docs"));
|
|
117
|
+
fs.writeFileSync(path.join(tmpDir, "docs", "a.md"), "doc a");
|
|
118
|
+
fs.writeFileSync(path.join(tmpDir, "docs", "b.md"), "doc b");
|
|
119
|
+
|
|
120
|
+
const result = await share({
|
|
121
|
+
paths: [path.join(tmpDir, "docs")],
|
|
122
|
+
company: "acme",
|
|
123
|
+
vaultConfig: mockConfig,
|
|
124
|
+
hqRoot: tmpDir,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
expect(result.filesUploaded).toBe(2);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("throws when no company specified and no active company", async () => {
|
|
131
|
+
fs.writeFileSync(path.join(tmpDir, "test.md"), "test");
|
|
132
|
+
|
|
133
|
+
await expect(
|
|
134
|
+
share({
|
|
135
|
+
paths: [path.join(tmpDir, "test.md")],
|
|
136
|
+
vaultConfig: mockConfig,
|
|
137
|
+
hqRoot: tmpDir,
|
|
138
|
+
}),
|
|
139
|
+
).rejects.toThrow(/No company specified/);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("resolves active company from .hq/config.json", async () => {
|
|
143
|
+
fs.mkdirSync(path.join(tmpDir, ".hq"), { recursive: true });
|
|
144
|
+
fs.writeFileSync(path.join(tmpDir, ".hq", "config.json"), JSON.stringify({ activeCompany: "acme" }));
|
|
145
|
+
fs.writeFileSync(path.join(tmpDir, "test.md"), "test");
|
|
146
|
+
|
|
147
|
+
const result = await share({
|
|
148
|
+
paths: [path.join(tmpDir, "test.md")],
|
|
149
|
+
vaultConfig: mockConfig,
|
|
150
|
+
hqRoot: tmpDir,
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
expect(result.filesUploaded).toBe(1);
|
|
154
|
+
});
|
|
155
|
+
});
|