@calmlens/sdk 0.9.1 → 0.10.0

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/esm/testing.d.ts CHANGED
@@ -1,5 +1,13 @@
1
- import { A as GetAssetResponse, C as RetryAssetOptions, D as UploadAssetPartOptions, E as UpdateFolderOptions, F as AssetBinaryPayloadInput, I as Lens, K as Asset, L as Folder, M as GetEventsResponse, N as PostAssetSignedUrlResponse, S as RetrieveAssetOptions, T as UpdateAssetOptions, _ as MoveAssetOptions, a as CreateAssetSignedUrlOptions, c as DeleteAssetContentOptions, d as EnsureLensesOptions, f as FolderIdOptions, g as ModerateTextOptions, h as ListEventsOptions, i as CdnServeOptions, j as GetAssetsPageResponse, k as GetAssetChildrenResponse, l as DeleteAssetOptions, m as ListAssetsOptions, n as CalmLensClientOptions, o as CreateFolderOptions, p as ListAssetChildrenOptions, q as AssetStatus, r as CdnAssetUrlOptions, u as EnsureLensOptions, v as MoveFolderOptions, w as SubmitAssetOptions } from "./CalmLensTypes-CZqyni4p.js";
1
+ import { A as ModerateTextOptions, B as UpdateFolderOptions, C as IterateAssetChildrenOptions, D as ListAssetChildrenOptions, G as AssetSearchQuery, I as RetrieveAssetOptions, J as PostAssetSignedUrlResponse, K as WebhookEventPayload, L as RetryAssetOptions, M as MoveFolderOptions, O as ListAssetsOptions, Q as TypedEvent, R as SubmitAssetOptions, T as Lens, V as UploadAssetPartOptions, X as Folder, Y as AssetBinaryPayloadInput, Z as Event, _ as DeleteAssetOptions, a as AssetSearchResponse, at as ClassificationCategory, b as FolderIdOptions, d as CdnAssetUrlOptions, f as CdnServeOptions, g as DeleteAssetContentOptions, i as AssetPage, j as MoveAssetOptions, k as ListEventsOptions, m as CreateFolderOptions, n as AssetChildrenPage, p as CreateAssetSignedUrlOptions, q as GetEventsResponse, s as AssetStatus, t as Asset, u as CalmLensClientOptions, v as EnsureLensOptions, w as IterateAssetsOptions, y as EnsureLensesOptions, z as UpdateAssetOptions } from "./CalmLensTypes-BfweJTMS.js";
2
2
  import * as zod from "zod";
3
+ //#region ../shared/src/TestTriggers.d.ts
4
+ type ClassificationTestSignal = ClassificationCategory | "sexual.involving-minors" | "violence.graphic";
5
+ //#endregion
6
+ //#region ../shared/src/MediaTestTriggers.d.ts
7
+ export declare const MEDIA_TEST_SIGNALS: readonly ["sexual", "violence", "self-harm", "harassment", "hate", "criminal-activity", "violence.graphic"];
8
+ /** Read this aloud, or render it as large, high-contrast text in a video frame. */
9
+ export declare function generateMediaTestCode(signal: ClassificationTestSignal, projectId: string): string;
10
+ //#endregion
3
11
  //#region ../shared/src/Permissions.d.ts
4
12
  declare const GRANT_SCHEMA: zod.ZodObject<{
5
13
  actions: zod.ZodArray<zod.ZodEnum<{
@@ -101,13 +109,15 @@ interface MockCalmLensClientOptions extends CalmLensClientOptions {
101
109
  imageStorage?: MockImageStorage;
102
110
  createId?: () => string;
103
111
  now?: () => number;
112
+ /** Status `assets.create` returns. Defaults to `pending`. */
104
113
  queuedStatus?: AssetStatus;
114
+ /** Status `assets.moderate` and `text.moderate` return. Defaults to `approved`. */
105
115
  analyzedStatus?: AssetStatus;
106
116
  signedUrlBaseUrl?: string;
107
117
  };
108
118
  }
109
119
  /** A `MockCalmLensClient` method, named like its path on the client. */
110
- type MockCalmLensOperation = "assets.list" | "assets.retrieve" | "assets.update" | "assets.listChildren" | "assets.create" | "assets.moderate" | "assets.uploadPart" | "assets.createSignedUrl" | "assets.move" | "assets.retry" | "assets.deleteContent" | "assets.delete" | "text.moderate" | "cdn.serve" | "events.list" | "folders.list" | "folders.retrieve" | "folders.create" | "folders.update" | "folders.delete" | "folders.move" | "ensureLens" | "ensureLenses";
120
+ type MockCalmLensOperation = "assets.list" | "assets.search" | "assets.retrieve" | "assets.update" | "assets.listChildren" | "assets.create" | "assets.moderate" | "assets.uploadPart" | "assets.createSignedUrl" | "assets.move" | "assets.retry" | "assets.deleteContent" | "assets.delete" | "text.moderate" | "cdn.serve" | "events.list" | "folders.list" | "folders.retrieve" | "folders.create" | "folders.update" | "folders.delete" | "folders.move" | "ensureLens" | "ensureLenses";
111
121
  /** One recorded call: the method and the options object it was given, exactly as passed. */
112
122
  interface MockCalmLensCall {
113
123
  operation: MockCalmLensOperation;
@@ -119,8 +129,16 @@ interface MockCalmLensCall {
119
129
  * deadline (`sdk/timeout`); any `Error` is thrown as given.
120
130
  */
121
131
  type MockRequestFailure = "transport-error" | "timeout" | Error;
132
+ /** An event to add to the mock feed; omitted fields get defaults. */
133
+ type MockEventInput = Partial<Omit<TypedEvent, "type">> & {
134
+ type: TypedEvent["type"];
135
+ };
136
+ /**
137
+ * An in-memory stand-in for `CalmLensClient` with the same methods, validation and error
138
+ * codes. API errors carry the HTTP status, a `requestId` and response headers like the real
139
+ * client's, so error handling can be tested without a network.
140
+ */
122
141
  export declare class MockCalmLensClient {
123
- private readonly options;
124
142
  readonly databaseStorage: MockDatabaseStorage;
125
143
  readonly imageStorage: MockImageStorage;
126
144
  private readonly createId;
@@ -130,7 +148,13 @@ export declare class MockCalmLensClient {
130
148
  private readonly apiKeyGrants;
131
149
  private readonly signedUrlBaseUrl;
132
150
  private readonly cdnBaseUrl;
151
+ private readonly projectId;
133
152
  private readonly lenses;
153
+ private readonly eventFeed;
154
+ /** The queue mode each open multipart upload was started with, as the API saves it. */
155
+ private readonly uploadQueues;
156
+ private readonly idempotentResults;
157
+ private requestCount;
134
158
  private readonly pendingFailures;
135
159
  /** Every call made through this client, oldest first. */
136
160
  readonly calls: MockCalmLensCall[];
@@ -145,26 +169,61 @@ export declare class MockCalmLensClient {
145
169
  }): void;
146
170
  /** Calls recorded for one operation, oldest first. */
147
171
  callsTo(operation: MockCalmLensOperation): MockCalmLensCall[];
172
+ /**
173
+ * Adds an event to the feed `events.list` reads, as if CalmLens had recorded it. Returns the
174
+ * stored event; pass it to `toWebhookEventPayload` to build a matching webhook body.
175
+ */
176
+ enqueueEvent(input: MockEventInput): TypedEvent;
177
+ /** An API error as the real client throws it: status, default message and request ID. */
178
+ private apiError;
179
+ private assertValidUploadMimeType;
180
+ private invalidPayload;
148
181
  private findOwnAsset;
149
182
  private begin;
183
+ private jsonResponse;
150
184
  get cdn(): {
151
185
  getAssetUrl: ({ assetIdentifier, ...options }: CdnAssetUrlOptions) => string;
186
+ /**
187
+ * Serves stored content like the CDN: `200` with `Content-Type`, `Content-Length`,
188
+ * `ETag` and `X-Request-Id`, or a JSON error body with `404`/`403`. Never throws for
189
+ * HTTP errors, like the real client.
190
+ */
152
191
  serve: (serveOptions: CdnServeOptions) => Promise<Response>;
153
192
  };
154
193
  get events(): {
155
- /** The mock does not record events; it always returns an empty page at "now". */
194
+ /**
195
+ * Reads the mock feed like the API: without `after`, no events and a cursor for "now";
196
+ * with `after`, up to `limit` events added since, oldest first. Add events with
197
+ * `enqueueEvent`.
198
+ */
156
199
  list: (options?: ListEventsOptions) => Promise<GetEventsResponse>;
200
+ pages: (options?: ListEventsOptions) => AsyncGenerator<GetEventsResponse, void, undefined>;
157
201
  };
202
+ private listEvents;
203
+ private iterateEventPages;
204
+ private iteratePages;
158
205
  get text(): {
159
206
  moderate: (options: ModerateTextOptions) => Promise<Asset>;
160
207
  };
161
208
  get assets(): {
162
- list: (options?: ListAssetsOptions) => Promise<GetAssetsPageResponse>;
163
- retrieve: (options: RetrieveAssetOptions) => Promise<GetAssetResponse>;
164
- listChildren: (childrenOptions?: ListAssetChildrenOptions) => Promise<GetAssetChildrenResponse>;
209
+ /**
210
+ * Matches `query` (case-insensitively) against each asset's name, ID, external ID,
211
+ * description and tags, best match first. Mock scores are 1 for an exact match and 0.5
212
+ * otherwise; `mode` is accepted but there is no semantic search.
213
+ */
214
+ search: (options: Partial<AssetSearchQuery> & {
215
+ query: string;
216
+ }) => Promise<AssetSearchResponse>;
217
+ /** Lists assets with the real client's paging, `ordering`, `folderId` and `filter`. */
218
+ list: (options?: ListAssetsOptions) => Promise<AssetPage>;
219
+ iterate: (options?: IterateAssetsOptions) => AsyncGenerator<Asset, void, undefined>;
220
+ retrieve: (options: RetrieveAssetOptions) => Promise<Asset>;
221
+ listChildren: (childrenOptions?: ListAssetChildrenOptions) => Promise<AssetChildrenPage>;
222
+ iterateChildren: (options: IterateAssetChildrenOptions) => AsyncGenerator<Asset, void, undefined>;
165
223
  update: (options: UpdateAssetOptions) => Promise<Asset>;
166
224
  create: (options: SubmitAssetOptions) => Promise<Asset>;
167
225
  moderate: (options: SubmitAssetOptions) => Promise<Asset>;
226
+ /** Continues a multipart upload: parts must arrive in order while it is `uploading`. */
168
227
  uploadPart: (options: UploadAssetPartOptions) => Promise<Asset>;
169
228
  createSignedUrl: (signedUrlOptions: CreateAssetSignedUrlOptions) => Promise<PostAssetSignedUrlResponse>;
170
229
  move: (options: MoveAssetOptions) => Promise<Asset>;
@@ -183,11 +242,27 @@ export declare class MockCalmLensClient {
183
242
  ensureLens(options: EnsureLensOptions): Promise<Lens>;
184
243
  private ensureLensDefinition;
185
244
  ensureLenses(options: EnsureLensesOptions): Promise<Lens[]>;
245
+ /** Replays the first result for a reused idempotency key, like the API. */
246
+ private withIdempotency;
186
247
  private moderateAsset;
187
248
  private createAsset;
188
249
  private resolveFolder;
250
+ private findOwnFolder;
189
251
  private assertUniqueFolderName;
190
252
  private getSizeInBytes;
191
253
  }
192
254
  //#endregion
193
- export type { MockCalmLensCall, MockCalmLensClientOptions, MockCalmLensOperation, MockDatabaseStorage, MockImageStorage, MockRequestFailure, MockStoredContent };
255
+ //#region src/testing/MockWebhooks.d.ts
256
+ /** The JSON body of a webhook delivery for a stored event (or an event from `events.list`). */
257
+ export declare const toWebhookEventPayload: (event: Event) => WebhookEventPayload;
258
+ /** `v1=<hex HMAC-SHA256 of "<timestamp>.<body>">`, the `x-calmlens-signature` header value. */
259
+ export declare const createWebhookSignatureHeader: (secret: string, timestamp: number | string, body: string) => Promise<string>;
260
+ /** Headers for one signed delivery of `body`, as CalmLens sends them. */
261
+ export declare const createWebhookHeaders: (options: {
262
+ secret: string;
263
+ body: string;
264
+ deliveryId: string;
265
+ timestampSeconds?: number;
266
+ }) => Promise<Record<string, string>>;
267
+ //#endregion
268
+ export type { MockCalmLensCall, MockCalmLensClientOptions, MockCalmLensOperation, MockDatabaseStorage, MockEventInput, MockImageStorage, MockRequestFailure, MockStoredContent };