Files
memos/web/tests/usage-limit-errors.test.ts
T
johnnyjoygh 9e3a164dff feat(usage): add default limits and explicit rule targets
Move memo content size into usage limits, with 8 KiB content and 32 MiB
upload defaults when settings are absent. Represent stored rule targets
with all_users or a positive user_id, and use that same schema for
deployment configuration.

Rename the memo count metric, remove reserved proto declarations, and
renumber fields and enums for the unreleased schema. Synchronize
enforcement, settings UI, generated clients, and tests.
2026-10-12 22:35:39 +08:00

55 lines
2.7 KiB
TypeScript

import { BinaryWriter } from "@bufbuild/protobuf/wire";
import { Code, ConnectError } from "@connectrpc/connect";
import { describe, expect, it } from "vitest";
import { errorService } from "@/components/MemoEditor/services/errorService";
import { ERROR_REASON_QUOTA_EXCEEDED, getErrorMessage, getRetryAfterSeconds } from "@/lib/error";
function quotaError(metadata: Record<string, string>): ConnectError {
const writer = new BinaryWriter();
writer.tag(1, 2).string(ERROR_REASON_QUOTA_EXCEEDED);
writer.tag(2, 2).string("memos.api");
for (const [key, value] of Object.entries(metadata)) {
const entry = new BinaryWriter();
entry.tag(1, 2).string(key);
entry.tag(2, 2).string(value);
writer.tag(3, 2).bytes(entry.finish());
}
const error = new ConnectError("quota exceeded", Code.ResourceExhausted);
error.details.push({ type: "google.rpc.ErrorInfo", value: writer.finish() });
return error;
}
describe("usage limit refusals", () => {
it("names the memo limit", () => {
const error = quotaError({ metric: "MEMO_COUNT", limit: "1000", usage: "1000", usage_limit: "usageLimits/default-memos" });
expect(getErrorMessage(error)).toBe("You have reached your limit of 1000 memos. Delete memos to create new ones.");
});
it("names the storage limit and the current usage", () => {
const error = quotaError({ metric: "STORAGE_BYTES", limit: String(1024 ** 3), usage: String(1000 * 1024 ** 2) });
expect(getErrorMessage(error)).toBe("This upload would exceed your storage limit of 1.0 GB. You are using 1000.0 MB.");
});
it("names the per-file limit", () => {
const error = quotaError({ metric: "UPLOAD_BYTES", limit: String(10 * 1024 ** 2), usage: String(20 * 1024 ** 2) });
expect(getErrorMessage(error)).toBe("This file is larger than your limit of 10.0 MB per file.");
});
it("names the content limit and reaches the editor's error message", () => {
const error = quotaError({ metric: "MEMO_CONTENT_BYTES", limit: "8192", usage: "8193" });
const message = "This memo is larger than your limit of 8.0 KB per memo. Shorten the content to save it.";
expect(getErrorMessage(error)).toBe(message);
expect(errorService.getErrorMessage(error)).toBe(message);
});
it("is not a rate limit, so it carries no wait", () => {
const error = quotaError({ metric: "MEMO_COUNT", limit: "1", usage: "1" });
expect(getRetryAfterSeconds(error)).toBeUndefined();
});
it("reaches the editor's own error messages", () => {
const error = quotaError({ metric: "UPLOAD_BYTES", limit: String(1024 ** 2), usage: String(2 * 1024 ** 2) });
expect(errorService.getErrorMessage(error)).toBe("This file is larger than your limit of 1.0 MB per file.");
});
});