@scalepad/cli 0.1.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.
@@ -0,0 +1,118 @@
1
+ import { describe, expect, it, vi } from "vitest";
2
+ import { createCoreClient, ScalepadApiError } from "@scalepad/sdk-core";
3
+ import { buildSort, flattenRecord, parseFilterArgs, projectRecord } from "../src/filters.js";
4
+ import { extractList, parseFields, resolveOutputFormat } from "../src/format.js";
5
+
6
+ describe("filter helpers", () => {
7
+ it("parses filter arguments into API query expressions", () => {
8
+ expect(parseFilterArgs([
9
+ "name=cont:acme",
10
+ "client_id=eq:2220324"
11
+ ])).toEqual({
12
+ name: "cont:acme",
13
+ client_id: "eq:2220324"
14
+ });
15
+ });
16
+
17
+ it("builds sort expressions", () => {
18
+ expect(buildSort("name", false)).toBe("+name");
19
+ expect(buildSort("name", true)).toBe("-name");
20
+ });
21
+
22
+ it("projects and flattens records", () => {
23
+ const record = {
24
+ id: "123",
25
+ name: "Acme",
26
+ nested: {
27
+ count: 4
28
+ }
29
+ };
30
+
31
+ expect(projectRecord(record, ["id", "nested.count"])).toEqual({
32
+ id: "123",
33
+ "nested.count": 4
34
+ });
35
+
36
+ expect(flattenRecord(record)).toEqual({
37
+ id: "123",
38
+ name: "Acme",
39
+ "nested.count": "4"
40
+ });
41
+ });
42
+ });
43
+
44
+ describe("format helpers", () => {
45
+ it("resolves output flags", () => {
46
+ expect(resolveOutputFormat({ json: true })).toBe("json");
47
+ expect(resolveOutputFormat({ jsonl: true })).toBe("jsonl");
48
+ expect(resolveOutputFormat({ csv: true })).toBe("csv");
49
+ expect(resolveOutputFormat({})).toBe("table");
50
+ });
51
+
52
+ it("extracts paginated list payloads", () => {
53
+ expect(extractList({
54
+ data: [{ id: "1" }],
55
+ total_count: 10,
56
+ next_cursor: "cursor-2"
57
+ })).toEqual({
58
+ items: [{ id: "1" }],
59
+ totalCount: 10,
60
+ nextCursor: "cursor-2"
61
+ });
62
+ });
63
+
64
+ it("parses field selections", () => {
65
+ expect(parseFields("id,name,nested.count")).toEqual(["id", "name", "nested.count"]);
66
+ });
67
+ });
68
+
69
+ describe("core SDK retry behavior", () => {
70
+ it("retries 429 responses before succeeding", async () => {
71
+ const fetchImpl = vi.fn()
72
+ .mockResolvedValueOnce(new Response(JSON.stringify({ error: "slow down" }), {
73
+ status: 429,
74
+ headers: {
75
+ "content-type": "application/json",
76
+ "retry-after": "0"
77
+ }
78
+ }))
79
+ .mockResolvedValueOnce(new Response(JSON.stringify({
80
+ data: [{ id: "1" }],
81
+ total_count: 1,
82
+ next_cursor: null
83
+ }), {
84
+ status: 200,
85
+ headers: {
86
+ "content-type": "application/json"
87
+ }
88
+ }));
89
+
90
+ const client = createCoreClient({
91
+ apiKey: "secret",
92
+ fetchImpl: fetchImpl as typeof fetch,
93
+ maxRetries: 1
94
+ });
95
+
96
+ await expect(client.listClients({ pageSize: 1 })).resolves.toMatchObject({
97
+ data: [{ id: "1" }],
98
+ total_count: 1
99
+ });
100
+ expect(fetchImpl).toHaveBeenCalledTimes(2);
101
+ });
102
+
103
+ it("throws a typed error on non-retriable failures", async () => {
104
+ const fetchImpl = vi.fn().mockResolvedValue(new Response(JSON.stringify({ error: "nope" }), {
105
+ status: 401,
106
+ headers: {
107
+ "content-type": "application/json"
108
+ }
109
+ }));
110
+
111
+ const client = createCoreClient({
112
+ apiKey: "secret",
113
+ fetchImpl: fetchImpl as typeof fetch
114
+ });
115
+
116
+ await expect(client.listClients()).rejects.toBeInstanceOf(ScalepadApiError);
117
+ });
118
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist"
6
+ },
7
+ "include": ["src/**/*.ts"]
8
+ }