@specific.dev/spectest 0.11.0 → 0.12.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@specific.dev/spectest",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "Spectest SDK for defining test environments in TypeScript.",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -11,6 +11,11 @@ export {
11
11
  type PostgresOptions,
12
12
  type PostgresHelpers,
13
13
  } from "./postgres.js";
14
+ export {
15
+ s3,
16
+ type S3Options,
17
+ type S3Helpers,
18
+ } from "./s3.js";
14
19
  // `SQL` / `SqlClient` now live in the core SDK (`@specific.dev/spectest`),
15
20
  // next to `expect` and `fetch` — re-exported here so existing
16
21
  // `from ".../components"` imports keep resolving.
@@ -0,0 +1,132 @@
1
+ import type { ServiceDefinition } from "../index.js";
2
+ import { S3Client, type S3ClientLike } from "../s3.js";
3
+
4
+ export interface S3Options {
5
+ /** HTTP port the S3 API is served on. Default `9090`. */
6
+ port?: number;
7
+ /**
8
+ * Buckets to create on first boot. The backing server's own `initialBuckets`
9
+ * env is unreliable across versions, so the component creates them in `setup`
10
+ * (a plain CreateBucket `PUT`) — captured into the warm snapshot, never
11
+ * re-run on warm starts/forks.
12
+ */
13
+ buckets?: string[];
14
+ /**
15
+ * Default bucket the `helpers.client` targets (so `client.file("k")` resolves
16
+ * without a per-call `{ bucket }`). Defaults to the first entry of `buckets`.
17
+ */
18
+ bucket?: string;
19
+ /**
20
+ * Hostnames to additionally serve the store at over **HTTPS** (CA-trusted),
21
+ * via the daemon's TLS-terminating reverse proxy. Optional — defaults to none;
22
+ * the store is always reachable at `http://<key>:<port>` regardless. Set this
23
+ * only when the app under test hardcodes a specific prod S3 endpoint it can't
24
+ * be told to override (`https://<bucket>.s3.amazonaws.com`, a provider's
25
+ * storage host): the daemon mints a cert for each host and proxies to the
26
+ * store's plain HTTP. The store ignores SigV4, so the Host rewrite is harmless.
27
+ */
28
+ hosts?: string[];
29
+ /** Access key id the helper client signs with. The store ignores SigV4, so
30
+ * any non-empty value works; default `"s3"`. */
31
+ accessKeyId?: string;
32
+ /** Secret key the helper client signs with (ignored by the store). Default
33
+ * `"s3"`. */
34
+ secretAccessKey?: string;
35
+ /** Region for the helper client. Default `"us-east-1"`. */
36
+ region?: string;
37
+ /** Extra environment variables forwarded to the container. */
38
+ env?: Record<string, string>;
39
+ }
40
+
41
+ /** Helpers an `s3(...)` service exposes on `ctx.svc.<name>`. */
42
+ export interface S3Helpers {
43
+ /** An instrumented {@link S3Client} pointed at this store (path-style) — each
44
+ * object op lands on the test event log and its result comes back
45
+ * provenance-wrapped, so `expect(...)` on a read links to the op. */
46
+ client: S3ClientLike;
47
+ }
48
+
49
+ // Backing image, pinned internally (not user-configurable). adobe/s3mock 5.1.0
50
+ // is the current release; avoid exactly 5.0.0, which crashed at startup on
51
+ // Java 25 (`NoClassDefFoundError: KotlinBuiltIns$2`) — fixed in 5.1.0.
52
+ const S3_IMAGE = "adobe/s3mock:5.1.0";
53
+
54
+ /**
55
+ * A ready-to-use, S3-compatible object store (backed by
56
+ * [`adobe/s3mock`](https://github.com/adobe/S3Mock)), with an instrumented S3
57
+ * client on `ctx.svc.<key>.client`. Drop into `environment.services`:
58
+ *
59
+ * ```ts
60
+ * import { s3 } from "@specific.dev/spectest/components";
61
+ *
62
+ * services: {
63
+ * storage: s3({ buckets: ["uploads"] }),
64
+ * },
65
+ * ```
66
+ *
67
+ * Tests get an instrumented S3 client at `ctx.svc.<key>.client`:
68
+ *
69
+ * ```ts
70
+ * await ctx.svc.storage.client.write("hello.txt", "hi there");
71
+ * const body = await ctx.svc.storage.client.file("hello.txt").text();
72
+ * expect(body).toBe("hi there"); // nests under the s3 read step
73
+ * ```
74
+ *
75
+ * The store skips SigV4 validation and is path-style only (what S3-compatible
76
+ * clients use), so it stands in for AWS S3, R2, Spaces, etc. without credential
77
+ * plumbing. If — and only if — the app under test hardcodes a specific prod S3
78
+ * endpoint, pass `hosts` to also serve the store there over a CA-trusted cert:
79
+ *
80
+ * ```ts
81
+ * // app hardcodes https://files.example.com — serve it there too:
82
+ * storage: s3({ hosts: ["files.example.com"] }),
83
+ * ```
84
+ */
85
+ export function s3(opts: S3Options = {}) {
86
+ const port = opts.port ?? 9090;
87
+ const buckets = opts.buckets ?? [];
88
+ const defaultBucket = opts.bucket ?? buckets[0];
89
+ const accessKeyId = opts.accessKeyId ?? "s3";
90
+ const secretAccessKey = opts.secretAccessKey ?? "s3";
91
+ const region = opts.region ?? "us-east-1";
92
+
93
+ return {
94
+ image: { type: "registry", reference: S3_IMAGE },
95
+ ports: [port],
96
+ ...(opts.env ? { env: opts.env } : {}),
97
+ ...(opts.hosts
98
+ ? { tls: opts.hosts.map((hostname) => ({ hostname, port })) }
99
+ : {}),
100
+ // GET / returns the ListBuckets XML with 200 once the store is up.
101
+ readyCheck: { type: "http" as const, port, path: "/", timeoutSecs: 60 },
102
+ helpers: ({ name }: { name: string }): S3Helpers => ({
103
+ client: new S3Client({
104
+ endpoint: `http://${name}:${port}`,
105
+ region,
106
+ accessKeyId,
107
+ secretAccessKey,
108
+ ...(defaultBucket ? { bucket: defaultBucket } : {}),
109
+ // Path-style (Bun's default) — the store is path-style only.
110
+ }),
111
+ }),
112
+ ...(buckets.length > 0
113
+ ? {
114
+ setup: async ({ name }: { name: string }) => {
115
+ for (const bucket of buckets) {
116
+ // CreateBucket is a plain `PUT` on the bucket root; the store
117
+ // returns 200 (or 409 if it already exists — idempotent enough to
118
+ // ignore).
119
+ const res = await fetch(`http://${name}:${port}/${bucket}`, {
120
+ method: "PUT",
121
+ });
122
+ if (!res.ok && res.status !== 409) {
123
+ throw new Error(
124
+ `s3: failed to create bucket ${bucket}: ${res.status}`,
125
+ );
126
+ }
127
+ }
128
+ },
129
+ }
130
+ : {}),
131
+ } satisfies ServiceDefinition<S3Helpers>;
132
+ }
package/src/index.ts CHANGED
@@ -42,7 +42,7 @@ export { field } from "./inspect.js";
42
42
  // assertions stay tracked. See `sql.ts` / `redis.ts` / `s3.ts`.
43
43
  export { SQL, type SqlClient, type SqlOptions } from "./sql.js";
44
44
  export { RedisClient, type RedisClientLike, type RedisOptions } from "./redis.js";
45
- export { S3Client, type S3ClientLike, type S3File, type S3Options } from "./s3.js";
45
+ export { S3Client, type S3ClientLike, type S3File, type S3ClientOptions } from "./s3.js";
46
46
 
47
47
  export type {
48
48
  Browser,
package/src/s3.ts CHANGED
@@ -79,9 +79,9 @@ export interface S3ClientLike {
79
79
  [m: string]: (...args: any[]) => any;
80
80
  }
81
81
 
82
- /** Options accepted by the {@link S3Client} constructor — Bun's own plus a
83
- * recording `label`. Passed through to `Bun.S3Client` verbatim. */
84
- export interface S3Options {
82
+ /** Options accepted by the {@link S3Client} constructor — Bun's `S3Options`,
83
+ * passed through to `Bun.S3Client` verbatim. */
84
+ export interface S3ClientOptions {
85
85
  accessKeyId?: string;
86
86
  secretAccessKey?: string;
87
87
  sessionToken?: string;
@@ -92,12 +92,12 @@ export interface S3Options {
92
92
  }
93
93
 
94
94
  interface BunGlobal {
95
- S3Client: new (options?: S3Options) => RawS3Client;
95
+ S3Client: new (options?: S3ClientOptions) => RawS3Client;
96
96
  }
97
97
 
98
98
  interface S3Constructor {
99
- new (options?: S3Options): S3ClientLike;
100
- (options?: S3Options): S3ClientLike;
99
+ new (options?: S3ClientOptions): S3ClientLike;
100
+ (options?: S3ClientOptions): S3ClientLike;
101
101
  }
102
102
 
103
103
  const MAX_PREVIEW = 512;
@@ -106,7 +106,7 @@ const MAX_PREVIEW = 512;
106
106
  * Open an instrumented S3 client. Usable with or without `new`. Requires the
107
107
  * Bun runtime — it runs inside the spectest daemon.
108
108
  */
109
- export const S3Client = function S3Client(options?: S3Options): S3ClientLike {
109
+ export const S3Client = function S3Client(options?: S3ClientOptions): S3ClientLike {
110
110
  const bun = (globalThis as unknown as { Bun?: BunGlobal }).Bun;
111
111
  if (!bun?.S3Client) {
112
112
  throw new Error(