mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-05 00:28:51 +01:00
* feat(studio): approved partners keep their profile with OrangeCat The owner chose: applying stays link-only, no account; once approved, a partner connects their OrangeCat account so a lost link is not a lost profile. - studio_requests.orangecat_actor_id (migration 0117), set only on an approved, open partner application, never to a second identity. - POST /api/studio-portal/[id]/connect checks the private key once and returns a fifteen-minute signed link to /studio/connect; the key never travels to Loki's sign-in pages. - /studio/connect signs the partner in with OrangeCat (one button) and ties the application to that identity, then sends them back to the portal. /studio/partner lists a signed-in partner's connected applications and reopens one with a fresh private link (the old one stops working, as with email recovery). - The portal view says whether OrangeCat is connected. - Also: an approved partner who skipped the course now sees their assigned briefs; the portal still asked for a course pass there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HyfPCAeTFi2sjpVY44Y1JJ * style(drizzle): format the generated migration meta Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HyfPCAeTFi2sjpVY44Y1JJ * test(responsive): measure the studio partner's OrangeCat pages Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HyfPCAeTFi2sjpVY44Y1JJ --------- Co-authored-by: Claude <noreply@anthropic.com>
343 lines
13 KiB
TypeScript
343 lines
13 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { randomBytes, randomUUID } from "node:crypto";
|
|
import { and, eq, inArray } from "drizzle-orm";
|
|
import { COMMISSION } from "../../src/config/commission";
|
|
import { StudioContract } from "../../src/lib/studio-commission";
|
|
import { StudioIntake, StudioGuestAction, StudioStaffAction } from "../../src/config/studio";
|
|
|
|
/** Runs against the migrated CI/scratch database, creates only UUID-scoped test
|
|
* tenants, and removes their records in finally. No live customer identifiers. */
|
|
async function main() {
|
|
if (!process.env.DATABASE_URL)
|
|
throw new Error("DATABASE_URL must be a migrated scratch/CI database");
|
|
const { db } = await import("../../src/db");
|
|
const { users, entities, userProjects, widgetTokens, studioRequests, studioRequestEvents } =
|
|
await import("../../src/db/schema");
|
|
const {
|
|
createStudioRequest,
|
|
getStudioPortal,
|
|
mutateStudioPortal,
|
|
getStudioReview,
|
|
mutateStudioReview,
|
|
studioPartnerDirectory,
|
|
studioConnectableApplication,
|
|
connectStudioOrangecat,
|
|
studioApplicationsForActor,
|
|
reopenStudioApplication,
|
|
} = await import("../../src/db/queries/studio-requests");
|
|
const owners = [randomUUID(), randomUUID()];
|
|
const key = () => `spt_${randomBytes(32).toString("base64url")}`;
|
|
const contract = StudioContract.parse({
|
|
version: 1,
|
|
origin: COMMISSION.studioOrigin,
|
|
availability: { state: "closed", line: "Test capacity is closed" },
|
|
offer: {
|
|
id: "rescue",
|
|
name: "Rescue",
|
|
price: "CHF 6,500",
|
|
shape: "Two weeks",
|
|
what: "Assessment scope",
|
|
},
|
|
feedbackToken: "fcw_test",
|
|
course: {
|
|
version: "pilot-v1",
|
|
title: "Pilot",
|
|
pilot: true,
|
|
modules: [{ id: "problem-constraints", title: "Problem" }],
|
|
},
|
|
});
|
|
try {
|
|
await db.insert(users).values(owners.map((id) => ({ id, name: "Studio portal test" })));
|
|
const projects = await db
|
|
.insert(entities)
|
|
.values(
|
|
owners.map((userId) => ({ userId, name: "studio-portal-test", type: "project" as const })),
|
|
)
|
|
.returning();
|
|
for (const project of projects)
|
|
await db.insert(userProjects).values({
|
|
userId: project.userId,
|
|
name: "studio-portal-test",
|
|
entityProjectId: project.id,
|
|
});
|
|
const tokens = await db
|
|
.insert(widgetTokens)
|
|
.values(
|
|
projects.map((p) => ({
|
|
projectId: p.id,
|
|
userId: p.userId,
|
|
token: `fcw_${randomBytes(16).toString("hex")}`,
|
|
origins: [COMMISSION.studioOrigin],
|
|
})),
|
|
)
|
|
.returning();
|
|
const token = tokens.find((t) => t.userId === owners[0])!;
|
|
const ownedProject = projects.find((p) => p.userId === owners[0])!;
|
|
const foreignProject = projects.find((p) => p.userId === owners[1])!;
|
|
const access = key();
|
|
const input = StudioIntake.parse({
|
|
kind: "website",
|
|
target: "studio",
|
|
website: "bitbaum.orangecat.ch",
|
|
changes: "A test request for a mobile booking flow",
|
|
requestId: randomUUID(),
|
|
accessKey: access,
|
|
offerId: "rescue",
|
|
});
|
|
const [receipt, retry] = await Promise.all([
|
|
createStudioRequest(token, contract, input),
|
|
createStudioRequest(token, contract, input),
|
|
]);
|
|
assert.equal(receipt.id, retry.id);
|
|
assert.equal(receipt.status, "waitlisted");
|
|
const initialEvents = await db
|
|
.select()
|
|
.from(studioRequestEvents)
|
|
.where(eq(studioRequestEvents.requestId, receipt.id));
|
|
assert.equal(initialEvents.length, 1);
|
|
assert.equal(await getStudioPortal(receipt.id, key()), null);
|
|
assert.equal(await getStudioReview(owners[1], receipt.id), null);
|
|
const guest = (id: string, access: string, action: Record<string, unknown>) =>
|
|
mutateStudioPortal(
|
|
id,
|
|
access,
|
|
StudioGuestAction.parse({ mutationId: randomUUID(), ...action }),
|
|
contract,
|
|
);
|
|
const staff = (id: string, action: Record<string, unknown>, userId = owners[0]) =>
|
|
mutateStudioReview(
|
|
userId,
|
|
id,
|
|
StudioStaffAction.parse({ mutationId: randomUUID(), ...action }),
|
|
contract,
|
|
);
|
|
assert.equal(
|
|
await staff(receipt.id, { action: "message", body: "Cannot cross tenants" }, owners[1]),
|
|
false,
|
|
);
|
|
await assert.rejects(
|
|
staff(receipt.id, { action: "link_project", projectId: foreignProject.id }),
|
|
);
|
|
await staff(receipt.id, { action: "link_project", projectId: ownedProject.id });
|
|
const privateView = JSON.stringify(await getStudioPortal(receipt.id, access));
|
|
for (const sensitive of [owners[0], token.token, token.id, ownedProject.id, access])
|
|
assert.equal(privateView.includes(sensitive), false);
|
|
await staff(receipt.id, {
|
|
action: "publish_preview",
|
|
expectedVersion: 0,
|
|
previewUrl: "https://bitbaum.orangecat.ch",
|
|
scope: "Test booking flow on mobile",
|
|
summary: "The journey and its error recovery were checked.",
|
|
});
|
|
await assert.rejects(guest(receipt.id, access, { action: "accept_preview", version: 0 }));
|
|
const acceptance = StudioGuestAction.parse({
|
|
mutationId: randomUUID(),
|
|
action: "accept_preview",
|
|
version: 1,
|
|
});
|
|
await mutateStudioPortal(receipt.id, access, acceptance);
|
|
await mutateStudioPortal(receipt.id, access, acceptance);
|
|
assert.equal((await getStudioPortal(receipt.id, access))!.request.delivery!.accepted, true);
|
|
await assert.rejects(
|
|
mutateStudioPortal(
|
|
receipt.id,
|
|
access,
|
|
StudioGuestAction.parse({
|
|
mutationId: acceptance.mutationId,
|
|
action: "message",
|
|
body: "Different action with reused id",
|
|
}),
|
|
),
|
|
);
|
|
await staff(receipt.id, {
|
|
action: "publish_preview",
|
|
expectedVersion: 1,
|
|
previewUrl: "https://bitbaum.orangecat.ch/revision",
|
|
scope: "Revised booking scope on mobile",
|
|
summary: "A second revision with new verification evidence.",
|
|
});
|
|
await assert.rejects(guest(receipt.id, access, { action: "accept_preview", version: 1 }));
|
|
await mutateStudioPortal(receipt.id, access, acceptance); // replay is acknowledged but never accepts v2
|
|
assert.equal((await getStudioPortal(receipt.id, access))!.request.delivery!.accepted, false);
|
|
assert.equal((await getStudioPortal(receipt.id, access))!.request.delivery!.version, 2);
|
|
let partnerKey = key();
|
|
const application = await createStudioRequest(
|
|
token,
|
|
contract,
|
|
StudioIntake.parse({
|
|
kind: "partner",
|
|
requestId: randomUUID(),
|
|
accessKey: partnerKey,
|
|
changes: "A builder applying with a transferable practical capstone",
|
|
}),
|
|
);
|
|
// The course is optional: there is nothing to review until evidence
|
|
// exists, and approval rests on the applicant's work alone.
|
|
await assert.rejects(
|
|
staff(application.id, {
|
|
action: "review_course",
|
|
passed: true,
|
|
body: "No evidence has been submitted to review.",
|
|
}),
|
|
);
|
|
const profile = {
|
|
name: "Test partner",
|
|
headline: "Test evidence only",
|
|
url: "https://github.com/bitbaum",
|
|
rate: "Direct quote",
|
|
availability: "available",
|
|
};
|
|
await guest(application.id, partnerKey, { action: "propose_profile", profile, consent: true });
|
|
await assert.rejects(staff(application.id, { action: "publish_profile" }));
|
|
await staff(application.id, {
|
|
action: "approve_partner",
|
|
body: "Studio review approved this test partner on their portfolio, without the course.",
|
|
});
|
|
assert.equal(
|
|
(await getStudioPortal(application.id, partnerKey))!.request.partner!.approved,
|
|
true,
|
|
);
|
|
await assert.rejects(
|
|
staff(application.id, {
|
|
action: "set_status",
|
|
status: "closed",
|
|
body: "Generic closure must not bypass the explicit partner decision.",
|
|
}),
|
|
);
|
|
assert.equal((await studioPartnerDirectory(owners[0])).length, 0);
|
|
await staff(application.id, { action: "publish_profile" });
|
|
assert.equal((await studioPartnerDirectory(owners[0])).length, 1);
|
|
assert.equal((await studioPartnerDirectory(owners[0]))[0]!.coursePassed, false);
|
|
assert.equal((await studioPartnerDirectory(owners[1])).length, 0);
|
|
// An approved partner may still take the course; it adds the credential
|
|
// and leaves approval and the published profile standing.
|
|
const assessment = {
|
|
version: "pilot-v1",
|
|
answers: {
|
|
"problem-constraints":
|
|
"The concrete user journey and testable constraints are recorded here.",
|
|
},
|
|
projectUrl: "https://bitbaum.orangecat.ch",
|
|
sourceUrl: "https://github.com/bitbaum/bitbaum",
|
|
};
|
|
await guest(application.id, partnerKey, { action: "submit_assessment", assessment });
|
|
assert.equal((await studioPartnerDirectory(owners[0])).length, 1);
|
|
await staff(application.id, {
|
|
action: "review_course",
|
|
passed: true,
|
|
body: "The evidence meets all dimensions in the pilot rubric.",
|
|
});
|
|
assert.equal((await studioPartnerDirectory(owners[0]))[0]!.coursePassed, true);
|
|
// OrangeCat: an approved partner ties the application to their identity,
|
|
// and signing in with it reopens the application with a fresh link.
|
|
const actor = randomUUID();
|
|
assert.ok(await studioConnectableApplication(application.id, partnerKey));
|
|
await connectStudioOrangecat(application.id, actor);
|
|
await connectStudioOrangecat(application.id, actor); // idempotent
|
|
await assert.rejects(connectStudioOrangecat(application.id, randomUUID()));
|
|
assert.deepEqual(
|
|
(await studioApplicationsForActor(actor)).map((a) => a.id),
|
|
[application.id],
|
|
);
|
|
assert.equal(await reopenStudioApplication(application.id, randomUUID()), null);
|
|
const reopened = (await reopenStudioApplication(application.id, actor))!;
|
|
assert.equal(await getStudioPortal(application.id, partnerKey), null); // old link dies
|
|
assert.equal(
|
|
(await getStudioPortal(application.id, reopened.accessKey))!.request.partner!
|
|
.orangecatConnected,
|
|
true,
|
|
);
|
|
partnerKey = reopened.accessKey;
|
|
assert.equal(
|
|
(await getStudioPortal(application.id, partnerKey))!.request.status.id,
|
|
"approved",
|
|
);
|
|
await guest(application.id, partnerKey, {
|
|
action: "set_availability",
|
|
availability: "unavailable",
|
|
});
|
|
assert.equal((await studioPartnerDirectory(owners[0])).length, 0);
|
|
await guest(application.id, partnerKey, {
|
|
action: "set_availability",
|
|
availability: "limited",
|
|
});
|
|
await assert.rejects(
|
|
staff(receipt.id, { action: "assign_partner", partnerId: application.id }),
|
|
);
|
|
const assignmentKey = key();
|
|
const assignment = await createStudioRequest(
|
|
token,
|
|
contract,
|
|
StudioIntake.parse({
|
|
...input,
|
|
requestId: randomUUID(),
|
|
accessKey: assignmentKey,
|
|
target: "partner",
|
|
}),
|
|
);
|
|
await staff(assignment.id, { action: "assign_partner", partnerId: application.id });
|
|
const assignments = (await getStudioPortal(application.id, partnerKey))!.assignments;
|
|
assert.deepEqual(
|
|
assignments.map((r) => r.id),
|
|
[assignment.id],
|
|
);
|
|
assert.equal(JSON.stringify(assignments).includes(ownedProject.id), false);
|
|
await assert.rejects(
|
|
guest(application.id, partnerKey, {
|
|
action: "deliver_assignment",
|
|
requestId: receipt.id,
|
|
expectedVersion: 2,
|
|
previewUrl: "https://bitbaum.orangecat.ch",
|
|
scope: "Attempt at unrelated request",
|
|
summary: "This unrelated request must remain inaccessible.",
|
|
}),
|
|
);
|
|
const delivery = StudioGuestAction.parse({
|
|
action: "deliver_assignment",
|
|
mutationId: randomUUID(),
|
|
requestId: assignment.id,
|
|
expectedVersion: 0,
|
|
previewUrl: "https://bitbaum.orangecat.ch",
|
|
scope: "The assigned customer journey",
|
|
summary: "Verified assigned delivery and handover evidence.",
|
|
});
|
|
await mutateStudioPortal(application.id, partnerKey, delivery);
|
|
await mutateStudioPortal(application.id, partnerKey, delivery);
|
|
assert.equal(
|
|
(await getStudioPortal(assignment.id, assignmentKey))!.request.delivery!.version,
|
|
1,
|
|
);
|
|
await staff(application.id, {
|
|
action: "decline_partner",
|
|
body: "Approval withdrawn for test review reasons.",
|
|
});
|
|
assert.equal((await getStudioPortal(application.id, partnerKey))!.assignments.length, 0);
|
|
assert.equal((await studioPartnerDirectory(owners[0])).length, 0);
|
|
await guest(application.id, partnerKey, { action: "submit_assessment", assessment }); // revision after decline is possible
|
|
await staff(receipt.id, { action: "revoke_access" });
|
|
assert.equal(await getStudioPortal(receipt.id, access), null);
|
|
assert.equal(
|
|
await guest(receipt.id, access, { action: "message", body: "Revoked capability" }),
|
|
false,
|
|
);
|
|
await assert.rejects(createStudioRequest(token, contract, input));
|
|
const sameTenant = await db
|
|
.select()
|
|
.from(studioRequests)
|
|
.where(and(eq(studioRequests.userId, owners[0]), eq(studioRequests.id, receipt.id)));
|
|
assert.equal(sameTenant.length, 1);
|
|
console.log(
|
|
"✓ studio-portal-e2e: real database dedupe, tenant isolation, exact-version review, course/approval separation, availability, assigned delivery and revocation",
|
|
);
|
|
} finally {
|
|
await db.delete(entities).where(inArray(entities.userId, owners));
|
|
await db.delete(users).where(inArray(users.id, owners));
|
|
}
|
|
}
|
|
main()
|
|
.then(() => process.exit(0))
|
|
.catch((e: unknown) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|