@somewhere-cloud/cli 0.38.152 → 0.38.154

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/src/testing.ts CHANGED
@@ -1,85 +1,21 @@
1
- import { ApiClient } from './lib/client.js';
2
- import { getToken, loadProjectConfig } from './lib/config.js';
3
- import { fixtureTestTarget, type TestTarget } from './lib/test-target.js';
1
+ export { UserSession, signIn, createUser, expectStatus, expectRefusal, type DataRequest } from './lib/test-session.js';
4
2
 
5
- // Node 22.19+: native fetch, cookie storage and TypeScript; no test dependencies.
6
- export class UserSession {
7
- readonly origin: string;
8
- private cookies = new Map<string, string>();
9
- private target?: Promise<TestTarget>;
10
- constructor(baseUrl: string) { this.origin = new URL(baseUrl).origin; }
11
- async request(path: string, init: RequestInit = {}): Promise<Response> {
12
- const url = new URL(path, this.origin);
13
- if (url.origin !== this.origin) throw new Error('Fixture requests must stay on the app origin');
14
- const headers = new Headers(init.headers);
15
- headers.set('Origin', this.origin);
16
- headers.set('Cookie', [...this.cookies].map(([k, v]) => k + '=' + v).join('; '));
17
- const response = await fetch(url, { ...init, headers, redirect: 'error' });
18
- for (const cookie of response.headers.getSetCookie()) {
19
- const pair = cookie.split(';')[0];
20
- const split = pair.indexOf('=');
21
- this.cookies.set(pair.slice(0, split), pair.slice(split + 1));
22
- }
23
- return response;
24
- }
25
- post(path: string, body: unknown): Promise<Response> {
26
- return this.request(path, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
27
- }
28
- /** Use this session's cookies and the linked app's declared data contract. */
29
- data(table: string, request: DataRequest): Promise<Response>;
30
- data(table: string, contract: string, request: DataRequest): Promise<Response>;
31
- async data(table: string, requestOrContract: DataRequest | string, request?: DataRequest): Promise<Response> {
32
- if (typeof requestOrContract === 'string') {
33
- return this.post('/__sw/data', { ...request, table, contract: requestOrContract });
34
- }
35
- const projectId = loadProjectConfig()?.project_id;
36
- if (!projectId) throw new Error('Use the data helper from a linked project folder.');
37
- const target = await (this.target ??= fixtureTestTarget(new ApiClient(process.env.SW_TEST_TARGET ? null : getToken()), projectId));
38
- if (target.origin !== this.origin) throw new Error('This session belongs to a different app than the linked test target.');
39
- return this.post('/__sw/data', { ...requestOrContract, table, contract: target.contractDigest ?? '' });
40
- }
3
+ // Structurally compatible with Playwright without loading its browser.
4
+ interface ReadyLocator {
5
+ first?(): ReadyLocator;
6
+ waitFor(options: { state: 'visible' }): Promise<void>;
41
7
  }
42
- export type DataRequest =
43
- | { operation: 'list'; limit?: number; after?: string | number; where?: Record<string, unknown> }
44
- | { operation: 'get' | 'delete'; id: string | number }
45
- | { operation: 'create'; values: Record<string, unknown> }
46
- | { operation: 'update'; id: string | number; values: Record<string, unknown> };
47
-
48
- export async function expectStatus(response: Response, expected: number): Promise<void> {
49
- if (response.status !== expected) throw new Error('Expected HTTP ' + expected + ', received ' + response.status);
50
- }
51
- export async function expectRefusal(response: Response, status: 401 | 403, code: string): Promise<void> {
52
- await expectStatus(response, status);
53
- const body: unknown = await response.json();
54
- if (!body || typeof body !== 'object' || !('error' in body) || body.error !== code) {
55
- throw new Error('Refusal did not have the exact expected error code: ' + code);
56
- }
57
- }
58
- export async function signIn(baseUrl: string, email: string, password: string): Promise<UserSession> {
59
- const session = new UserSession(baseUrl);
60
- const response = await session.post('/api/auth/login', { email, password });
61
- await expectStatus(response, 200);
62
- const body = await response.json() as { cookie_session?: boolean };
63
- if (!body.cookie_session) throw new Error('Sign-in did not establish a cookie session');
64
- return session;
65
- }
66
- export async function createUser(baseUrl: string): Promise<UserSession> {
67
- const session = new UserSession(baseUrl);
68
- // Reserved platform test domain: no real recipient, no admin credential.
69
- const email = 'starter-' + crypto.randomUUID() + '@fixture.test.somewhere.site';
70
- const password = 'Fixture!9-' + crypto.randomUUID();
71
- const response = await session.post('/api/auth/signup', { email, password });
72
- await expectStatus(response, 200);
73
- const body = await response.json() as { cookie_session?: boolean };
74
- if (!body.cookie_session) throw new Error('Signup did not establish a cookie session');
75
- return session;
76
- }
77
- // Structurally compatible with Playwright Page; no browser dependency required.
78
- interface BrowserPage { locator(selector: string): { waitFor(options: { state: 'visible' }): Promise<void> } }
8
+ interface BrowserPage { locator(selector: string): ReadyLocator }
79
9
  export function waitForApp(page: BrowserPage, state: 'ready' | 'signed-in' | 'signed-out' = 'ready'): Promise<void> {
80
- const selector = state === 'ready' ? '[data-app-state="signed-in"], [data-app-state="signed-out"]' : '[data-app-state="' + state + '"]';
81
- return page.locator(selector).waitFor({ state: 'visible' });
10
+ const values = state === 'ready' ? ['signed-in', 'signed-out'] : [state];
11
+ const matches = values.flatMap(value => [`html[data-sw-auth="${value}"]`, `[data-app-state="${value}"]`]);
12
+ const locator = page.locator(matches.join(', '));
13
+ return (locator.first?.() ?? locator).waitFor({ state: 'visible' });
82
14
  }
83
15
 
84
16
  // Lazy project-local Playwright resolution keeps UserSession dependency-free.
85
17
  export { test, expect, expectRefused, data, ready, type RefusalOptions, type TestUser, type NewUserOptions, type Person, type SignUpOptions, type SignInOptions, type FormSteps, type CodeSteps } from './lib/playwright-fixtures.js';
18
+
19
+ export { openTestApp, type TestApp, type TestActor } from './lib/testing-app.js';
20
+ export { assertNoLeak, captureResponses } from './lib/test-response-capture.js';
21
+ export type { TestJob, TestJobOptions, TestJobTarget } from './lib/testing-jobs.js';