mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 19:36:04 +00:00
Merge remote-tracking branch 'origin/codex/unified-cli-share' into prerelease/cli-share
This commit is contained in:
@@ -37,7 +37,7 @@ export class AuthError extends Error {
|
||||
export const ErrNotConfigured = () =>
|
||||
new AuthError(
|
||||
"NOT_CONFIGURED",
|
||||
"No HeyGen credentials found",
|
||||
"No HeyGen credentials found.",
|
||||
"Run `hyperframes auth login` to sign in.",
|
||||
);
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { ReviewFailed } from "@hyperframes/share";
|
||||
import { afterEach, beforeEach, expect, it, vi } from "vitest";
|
||||
import { runCommand } from "citty";
|
||||
import { consumeCommandResult } from "../utils/commandResult.js";
|
||||
@@ -96,3 +97,39 @@ it("deduplicates progress and terminates each message before the friendly error"
|
||||
]);
|
||||
expect(friendly).toHaveBeenCalledWith("Sharing blocked.");
|
||||
});
|
||||
|
||||
it.each(["owner@example.com", "another@example.com"])(
|
||||
"keeps the owner error code stable for %s",
|
||||
async (owner) => {
|
||||
mocks.run.mockRejectedValue(new ReviewFailed(403, `owner_signed_out:${owner}`));
|
||||
const out = vi.spyOn(console, "log").mockImplementation(() => {});
|
||||
await runCommand(command, { rawArgs: ["--status", "--json"] });
|
||||
expect(consumeCommandResult().exitCode).toBe(1);
|
||||
expect(out).toHaveBeenCalledOnce();
|
||||
expect(JSON.parse(String(out.mock.calls[0]?.[0]))).toEqual({
|
||||
ok: false,
|
||||
error: {
|
||||
code: "owner_signed_out",
|
||||
owner,
|
||||
message: `This review belongs to ${owner}. Switch to that account to manage it.`,
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("reports an unchanged update in human and JSON output", async () => {
|
||||
mocks.run.mockResolvedValue({
|
||||
state: "ready",
|
||||
ready: true,
|
||||
upToDate: true,
|
||||
source: { at: "ready" },
|
||||
});
|
||||
const out = vi.spyOn(console, "log").mockImplementation(() => {});
|
||||
await runCommand(command, { rawArgs: ["--update"] });
|
||||
expect(out).toHaveBeenCalledWith("Already up to date.");
|
||||
await runCommand(command, { rawArgs: ["--update", "--json"] });
|
||||
expect(JSON.parse(String(out.mock.calls.at(-1)?.[0]))).toMatchObject({
|
||||
ok: true,
|
||||
upToDate: true,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -73,7 +73,10 @@ export default defineCommand({
|
||||
await presentResult(result, input, Boolean(args.json));
|
||||
} catch (error) {
|
||||
const failure = shareError(error);
|
||||
if (args.json) console.log(JSON.stringify(jsonFailure(failure.code, failure.message)));
|
||||
if (args.json)
|
||||
console.log(
|
||||
JSON.stringify({ ...jsonFailure(failure.code, failure.message), error: failure }),
|
||||
);
|
||||
else console.error(failure.message);
|
||||
setCommandExitCode(error instanceof CliUsageError ? 2 : 1);
|
||||
}
|
||||
@@ -114,7 +117,7 @@ function validateActionOptions(
|
||||
) {
|
||||
if (action !== "share" && audience)
|
||||
throw new CliUsageError(
|
||||
"Audience flags are only valid for a first share. Use --manage to change access.",
|
||||
"Use audience flags with hyperframes share, without --update, --status or --manage.",
|
||||
);
|
||||
if ((action === "status" || action === "manage") && (args.space || args["video-only"]))
|
||||
throw new CliUsageError("--space and --video-only cannot be used with --status or --manage.");
|
||||
@@ -126,7 +129,7 @@ function requestedAudience(args: ShareArgs) {
|
||||
if (args.team) audiences.push("team");
|
||||
if (args["invite-only"]) audiences.push("invite");
|
||||
if (args.private) audiences.push("private");
|
||||
if (audiences.length > 1) throw new CliUsageError("Choose one first-share audience.");
|
||||
if (audiences.length > 1) throw new CliUsageError("Choose one audience.");
|
||||
return audiences[0];
|
||||
}
|
||||
|
||||
@@ -172,6 +175,7 @@ async function presentResult(result: ShareState, input: ShareAction, json: boole
|
||||
|
||||
async function presentSuccessfulResult(result: ShareState, input: ShareAction, json: boolean) {
|
||||
if (json) console.log(JSON.stringify({ ok: true, ...result }));
|
||||
else if (result.upToDate) console.log("Already up to date.");
|
||||
else if (input.action === "manage") {
|
||||
console.log(result.manageUrl);
|
||||
if (!isUnattended()) await (await import("open")).default(result.manageUrl);
|
||||
|
||||
@@ -10,6 +10,7 @@ afterEach(() => {
|
||||
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
const state: ShareState = {
|
||||
upToDate: false,
|
||||
share: {
|
||||
projectId: "project",
|
||||
url: "https://hyperframes.dev/r/test",
|
||||
|
||||
@@ -8,6 +8,7 @@ export function presentShare(value: ShareState): LocalShareState {
|
||||
value.share?.takedown ||
|
||||
value.share?.sharingState?.state === "subscription_required";
|
||||
return {
|
||||
upToDate: value.upToDate,
|
||||
state: presentedState(value, Boolean(held)),
|
||||
url: held ? null : (value.url ?? value.previousUrl),
|
||||
failure: value.state === "failed" ? value.version.failure : null,
|
||||
|
||||
@@ -7,6 +7,7 @@ import { AuthError } from "../auth/errors.js";
|
||||
import { createShareCsrf, localShareRequestAllowed } from "./shareBoundary.js";
|
||||
|
||||
const state: ShareState = {
|
||||
upToDate: false,
|
||||
source: { at: "not_checked" },
|
||||
share: null,
|
||||
version: { last: null, source: "none", exporting: null, failure: null },
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ReviewFailed, reviewSentence } from "@hyperframes/share";
|
||||
import { ownerOf, ReviewFailed, reviewSentence } from "@hyperframes/share";
|
||||
import { ShareSourceError } from "./sources.js";
|
||||
import { isAuthError } from "../auth/errors.js";
|
||||
import { CliUsageError } from "../utils/commandResult.js";
|
||||
@@ -19,7 +19,7 @@ const HINTS: Record<string, string> = {
|
||||
not_shared:
|
||||
"This project has no shared review yet. Run hyperframes share with an explicit audience.",
|
||||
};
|
||||
export function shareError(error: unknown): { code: string; message: string } {
|
||||
export function shareError(error: unknown): { code: string; message: string; owner?: string } {
|
||||
if (error instanceof CliUsageError) return { code: error.code, message: error.message };
|
||||
if (error instanceof ShareSourceError)
|
||||
return { code: "source_unavailable", message: error.message };
|
||||
@@ -28,11 +28,14 @@ export function shareError(error: unknown): { code: string; message: string } {
|
||||
code: error.code.toLowerCase(),
|
||||
message: `${error.message} ${error.hint ?? ""}`.trim(),
|
||||
};
|
||||
if (error instanceof ReviewFailed)
|
||||
if (error instanceof ReviewFailed) {
|
||||
const owner = error.status === 401 ? null : ownerOf(error.reason);
|
||||
if (owner !== null) return { code: "owner_signed_out", owner, message: reviewSentence(error) };
|
||||
return {
|
||||
code: error.status === 401 ? "login_expired" : error.reason || "share_failed",
|
||||
message: HINTS[error.reason] ?? reviewSentence(error),
|
||||
};
|
||||
}
|
||||
return {
|
||||
code: "share_failed",
|
||||
message: error instanceof Error ? error.message : "Sharing failed. Try again.",
|
||||
|
||||
@@ -13,6 +13,7 @@ const mocks = vi.hoisted(() => ({
|
||||
refresh: vi.fn(),
|
||||
event: vi.fn(),
|
||||
access: vi.fn(),
|
||||
setAccess: vi.fn(),
|
||||
}));
|
||||
vi.mock("@hyperframes/share", () => ({
|
||||
reviewClient: () => ({ project: mocks.project, spaces: mocks.spaces, reviewFlags: mocks.flags }),
|
||||
@@ -23,7 +24,7 @@ vi.mock("@hyperframes/share", () => ({
|
||||
addVersion: mocks.add,
|
||||
pendingReviewExport: mocks.pending,
|
||||
refreshReviewExport: mocks.refresh,
|
||||
setReviewAccess: vi.fn(),
|
||||
setReviewAccess: mocks.setAccess,
|
||||
turnOffReviewLink: vi.fn(),
|
||||
ReviewFailed: class extends Error {
|
||||
constructor(
|
||||
@@ -104,15 +105,20 @@ describe("share operation consent and identity", () => {
|
||||
).rejects.toMatchObject({ reason: "review_project_limit_reached" });
|
||||
expect(mocks.render).not.toHaveBeenCalled();
|
||||
});
|
||||
it("preserves existing workspace and audience", async () => {
|
||||
it("preserves existing workspace and changes an explicitly requested audience", async () => {
|
||||
mocks.status.mockResolvedValue(shared);
|
||||
const service = createShareService("/project");
|
||||
await expect(service.run({ action: "update", space: "other" })).rejects.toMatchObject({
|
||||
reason: "workspace_mismatch",
|
||||
});
|
||||
await expect(service.run({ action: "share", audience: "private" })).rejects.toMatchObject({
|
||||
reason: "manage_existing_audience",
|
||||
});
|
||||
mocks.setAccess.mockImplementation(async () =>
|
||||
mocks.status.mockResolvedValue({ ...shared, access: "private" }),
|
||||
);
|
||||
await service.run({ action: "share", audience: "private" });
|
||||
expect(mocks.setAccess).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ change: { access: "private" } }),
|
||||
);
|
||||
expect(mocks.add).not.toHaveBeenCalled();
|
||||
expect(mocks.render).not.toHaveBeenCalled();
|
||||
});
|
||||
it("refuses a public share opened while a private first share was rendering", async () => {
|
||||
@@ -423,3 +429,36 @@ it("retries an uncreated first version after refusal even when local files chang
|
||||
expect(mocks.add).toHaveBeenCalledOnce();
|
||||
expect(mocks.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("reports an unchanged completed update without claiming a new version", async () => {
|
||||
mocks.status.mockResolvedValue(shared);
|
||||
mocks.version.mockResolvedValue({ last: { current: true }, source: "ready" });
|
||||
mocks.add.mockResolvedValue(null);
|
||||
const result = await createShareService("/project").run({ action: "update" });
|
||||
expect(result.upToDate).toBe(true);
|
||||
});
|
||||
it("propagates a refused audience change without rendering", async () => {
|
||||
mocks.status.mockResolvedValue(shared);
|
||||
mocks.setAccess.mockRejectedValue(new Error("Restricted sharing requires a subscription"));
|
||||
await expect(
|
||||
createShareService("/project").run({ action: "share", audience: "team" }),
|
||||
).rejects.toThrow("subscription");
|
||||
expect(mocks.render).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not label a newly published version as already up to date", async () => {
|
||||
mocks.status.mockResolvedValue(shared);
|
||||
mocks.version.mockResolvedValue({ last: { current: true }, source: "ready" });
|
||||
mocks.add.mockImplementation(async ({ onAdded }) => {
|
||||
onAdded();
|
||||
return null;
|
||||
});
|
||||
expect((await createShareService("/project").run({ action: "update" })).upToDate).toBe(false);
|
||||
});
|
||||
it("does not label a still-processing update as already up to date", async () => {
|
||||
mocks.status.mockResolvedValue(shared);
|
||||
mocks.version.mockResolvedValue({ last: { current: true }, source: "ready" });
|
||||
mocks.pending.mockResolvedValue({ projectId: "project" });
|
||||
mocks.add.mockResolvedValue(null);
|
||||
expect((await createShareService("/project").run({ action: "update" })).upToDate).toBe(false);
|
||||
});
|
||||
|
||||
@@ -64,6 +64,7 @@ async function readStatus(
|
||||
const failed = hasFailedVersion(version);
|
||||
const ready = Boolean(activeUrl && latest && !processing && !failed);
|
||||
return {
|
||||
upToDate: false,
|
||||
share,
|
||||
version,
|
||||
pending,
|
||||
@@ -216,7 +217,10 @@ export function createShareService(
|
||||
return { ...result, state: "processing", ready: false, url: null };
|
||||
}
|
||||
attempt.complete(result.ready, added);
|
||||
return result;
|
||||
return {
|
||||
...result,
|
||||
upToDate: isUnchangedUpdate(input, added, result),
|
||||
};
|
||||
};
|
||||
const uploadTarget = async (current: ReviewShare | null, input: ShareAction, space?: string) => {
|
||||
const share = current
|
||||
@@ -250,7 +254,16 @@ export function createShareService(
|
||||
const mutate = async (input: ShareAction, attempt: ShareAttempt) => {
|
||||
await auth.access();
|
||||
attempt.authenticated();
|
||||
const current = await readStatus(dir, client, undefined, attempt.recover);
|
||||
let current = await readStatus(dir, client, undefined, attempt.recover);
|
||||
if (current.share && changesAudience(input, current.share)) {
|
||||
assertExistingIntent(current.share, { ...input, audience: undefined });
|
||||
const projectId = current.share.projectId;
|
||||
await setReviewAccess({ projectPath: dir, client, change: { access: input.audience } });
|
||||
current = await readStatus(dir, client, projectId);
|
||||
assertExistingIntent(current.share, input);
|
||||
attempt.retrieved();
|
||||
return current;
|
||||
}
|
||||
assertExistingIntent(current.share, input);
|
||||
if (retrievesExisting(current, input)) {
|
||||
attempt.retrieved();
|
||||
@@ -304,3 +317,12 @@ export type ShareState = Awaited<ReturnType<typeof readStatus>>;
|
||||
function hasFailedVersion(version: Awaited<ReturnType<typeof reviewVersion>>): boolean {
|
||||
return Boolean(version.failure && !version.held);
|
||||
}
|
||||
|
||||
function changesAudience(input: ShareAction, share: ReviewShare) {
|
||||
return input.action === "share" && Boolean(input.audience) && share.access !== input.audience;
|
||||
}
|
||||
function isUnchangedUpdate(input: ShareAction, added: boolean, result: ShareState) {
|
||||
return (
|
||||
input.action === "update" && !added && Boolean(result.version.last?.current) && !result.pending
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user