@sortlist/cli 0.34.0 → 0.36.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,114 @@
1
+ import { buildSchema } from 'graphql';
2
+ import { describe, expect, it } from 'vitest';
3
+
4
+ import { buildQuery } from './query-builder.ts';
5
+
6
+ const schema = buildSchema(`
7
+ type Query {
8
+ contact(id: ID!): Contact
9
+ contacts(first: Int): ContactConnection
10
+ version: String
11
+ }
12
+
13
+ type Mutation {
14
+ updateContact(input: UpdateContactInput!): UpdateContactPayload
15
+ }
16
+
17
+ type Contact {
18
+ id: ID!
19
+ name: String
20
+ organization: Organization
21
+ avatar(size: Int!): String
22
+ }
23
+
24
+ type Organization {
25
+ id: ID!
26
+ name: String
27
+ contacts: [Contact]
28
+ }
29
+
30
+ type ContactConnection {
31
+ nodes: [Contact]
32
+ pageInfo: PageInfo
33
+ totalCount: Int
34
+ }
35
+
36
+ type PageInfo {
37
+ hasNextPage: Boolean!
38
+ endCursor: String
39
+ }
40
+
41
+ input UpdateContactInput {
42
+ id: ID!
43
+ name: String
44
+ }
45
+
46
+ type UpdateContactPayload {
47
+ id: ID
48
+ contact: Contact
49
+ }
50
+ `);
51
+
52
+ describe('buildQuery', () => {
53
+ it('selects only leaf fields at depth 0 and skips fields with required args', () => {
54
+ const { query } = buildQuery(schema, 'query', 'contact', { id: '1' });
55
+ expect(query).toContain('id');
56
+ expect(query).toContain('name');
57
+ expect(query).not.toContain('organization');
58
+ expect(query).not.toContain('avatar');
59
+ });
60
+
61
+ it('descends into object fields at depth 1 without recursing into cycles', () => {
62
+ const { query } = buildQuery(schema, 'query', 'contact', { id: '1' }, { depth: 2 });
63
+ expect(query).toContain('organization {');
64
+ expect(query).not.toContain('contacts {');
65
+ });
66
+
67
+ it('unwraps Relay connections into nodes/pageInfo/totalCount', () => {
68
+ const { query } = buildQuery(schema, 'query', 'contacts', {});
69
+ expect(query).toContain('nodes {');
70
+ expect(query).toContain('pageInfo {');
71
+ expect(query).toContain('hasNextPage');
72
+ expect(query).toContain('totalCount');
73
+ });
74
+
75
+ it('builds selections from an explicit field list with dot notation', () => {
76
+ const { query } = buildQuery(schema, 'query', 'contact', { id: '1' }, { fields: ['name', 'organization.name'] });
77
+ expect(query).toContain('name');
78
+ expect(query).toContain('organization {');
79
+ expect(query).not.toContain('id\n');
80
+ });
81
+
82
+ it('omits the selection set for scalar return types', () => {
83
+ const { query } = buildQuery(schema, 'query', 'version', {});
84
+ expect(query).toContain('version');
85
+ expect(query).not.toContain('{ __typename');
86
+ });
87
+
88
+ it('declares variables for every field argument', () => {
89
+ const { query } = buildQuery(schema, 'query', 'contact', { id: '1' });
90
+ expect(query).toContain('query Contact($id: ID!)');
91
+ expect(query).toContain('contact(id: $id)');
92
+ });
93
+
94
+ it('falls back to __typename on payload types at depth 0 (id is skipped)', () => {
95
+ const { query } = buildQuery(schema, 'mutation', 'updateContact', { input: { id: '1' } });
96
+ expect(query).toContain('__typename');
97
+ expect(query).not.toMatch(/\{\s*id/);
98
+ });
99
+
100
+ it('selects payload object fields at depth 1, still skipping the payload id', () => {
101
+ const { query } = buildQuery(schema, 'mutation', 'updateContact', { input: { id: '1' } }, { depth: 1 });
102
+ expect(query).toContain('contact {');
103
+ expect(query).not.toMatch(/updateContact\(input: \$input\) \{\s*id/);
104
+ });
105
+
106
+ it('honors exclude paths', () => {
107
+ const { query } = buildQuery(schema, 'query', 'contact', { id: '1' }, { exclude: ['name'] });
108
+ expect(query).not.toContain('name');
109
+ });
110
+
111
+ it('throws on unknown root fields', () => {
112
+ expect(() => buildQuery(schema, 'query', 'nope', {})).toThrow('Unknown query field: nope');
113
+ });
114
+ });
@@ -38,7 +38,7 @@ const PAYLOAD_TYPE_PATTERN = /(?:Response|Payload|MutationPayload)$/;
38
38
  // ── Selection set builder ──
39
39
 
40
40
  export interface QueryOptions {
41
- /** Max nesting depth for sub-objects (default: 1) */
41
+ /** Max nesting depth for sub-objects (default: 0) */
42
42
  depth?: number;
43
43
  /** Only include these fields (dot-notation for nested, e.g. "organization.name") */
44
44
  fields?: string[];
@@ -0,0 +1,30 @@
1
+ import { afterEach, describe, expect, it, vi } from 'vitest';
2
+
3
+ import { isExpired, type Credentials } from './auth.ts';
4
+
5
+ function credsExpiringIn(seconds: number): Credentials {
6
+ return {
7
+ accessToken: 'a',
8
+ refreshToken: 'r',
9
+ idToken: 'i',
10
+ expiresAt: Math.floor(Date.now() / 1000) + seconds,
11
+ };
12
+ }
13
+
14
+ describe('isExpired', () => {
15
+ afterEach(() => {
16
+ vi.useRealTimers();
17
+ });
18
+
19
+ it('is false well before expiry', () => {
20
+ expect(isExpired(credsExpiringIn(300))).toBe(false);
21
+ });
22
+
23
+ it('is true inside the 60s refresh buffer', () => {
24
+ expect(isExpired(credsExpiringIn(30))).toBe(true);
25
+ });
26
+
27
+ it('is true after expiry', () => {
28
+ expect(isExpired(credsExpiringIn(-10))).toBe(true);
29
+ });
30
+ });
@@ -1,6 +1,11 @@
1
- import { exec } from 'node:child_process';
1
+ import { execFile } from 'node:child_process';
2
2
 
3
3
  export function openBrowser(url: string): void {
4
- const cmd = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open';
5
- exec(`${cmd} "${url}"`);
4
+ if (process.platform === 'win32') {
5
+ // `start` is a cmd built-in; the empty string is the window title slot.
6
+ execFile('cmd', ['/c', 'start', '', url], () => {});
7
+ return;
8
+ }
9
+ const cmd = process.platform === 'darwin' ? 'open' : 'xdg-open';
10
+ execFile(cmd, [url], () => {});
6
11
  }
package/src/lib/output.ts CHANGED
@@ -10,7 +10,6 @@ const ansi = {
10
10
  red: '\x1b[31m',
11
11
  green: '\x1b[32m',
12
12
  yellow: '\x1b[33m',
13
- cyan: '\x1b[36m',
14
13
  };
15
14
 
16
15
  function c(code: keyof typeof ansi, text: string): string {
@@ -29,21 +28,6 @@ export function printJson(data: unknown): void {
29
28
  process.stdout.write(JSON.stringify(data, null, 2) + '\n');
30
29
  }
31
30
 
32
- // ── Table output (human-readable, no box-drawing characters) ──
33
-
34
- export function printTable(headers: string[], rows: string[][]): void {
35
- const colWidths = headers.map((h, i) => Math.max(h.length, ...rows.map((r) => (r[i] ?? '').length)));
36
-
37
- const separator = colWidths.map((w) => '-'.repeat(w + 2)).join('+');
38
- const formatRow = (row: string[]) => row.map((cell, i) => ` ${(cell ?? '').padEnd(colWidths[i])} `).join('|');
39
-
40
- process.stdout.write(formatRow(headers) + '\n');
41
- process.stdout.write(separator + '\n');
42
- for (const row of rows) {
43
- process.stdout.write(formatRow(row) + '\n');
44
- }
45
- }
46
-
47
31
  // ── Message helpers ──
48
32
  // Every function uses a text prefix so status is conveyed independent of color.
49
33
  // Color is enhancement only — never the sole status indicator.
@@ -72,13 +56,3 @@ export function error(msg: string): void {
72
56
  export function hint(msg: string): void {
73
57
  process.stderr.write(`${c('dim', ' ' + msg)}\n`);
74
58
  }
75
-
76
- /** Dim text (e.g. secondary details, IDs). */
77
- export function dim(msg: string): void {
78
- process.stderr.write(`${c('dim', msg)}\n`);
79
- }
80
-
81
- /** Section heading for grouping output. */
82
- export function heading(msg: string): void {
83
- process.stderr.write(`\n${c('cyan', msg)}\n`);
84
- }
package/src/lib/sentry.ts CHANGED
@@ -55,13 +55,6 @@ export function captureException(error: unknown, extra?: Record<string, unknown>
55
55
  Sentry.captureException(error, extra ? { extra } : undefined);
56
56
  }
57
57
 
58
- export function setUser(user: { id?: string; email?: string }): void {
59
- if (isLocalDev) {
60
- return;
61
- }
62
- Sentry.setUser(user);
63
- }
64
-
65
58
  export function addBreadcrumb(breadcrumb: Sentry.Breadcrumb): void {
66
59
  if (isLocalDev) {
67
60
  return;
package/src/lib/parse.ts DELETED
@@ -1,8 +0,0 @@
1
- import fs from 'fs';
2
-
3
- export function parseRawData(value: string): unknown {
4
- if (value.startsWith('@')) {
5
- return JSON.parse(fs.readFileSync(value.slice(1), 'utf-8'));
6
- }
7
- return JSON.parse(value);
8
- }
@@ -1,95 +0,0 @@
1
- /**
2
- * Reorders out-of-sequence streaming events before they reach the event handler.
3
- *
4
- * Every subscription event carries an `agentLoopSequence` (monotonic integer starting
5
- * at 1) and an `agentLoopId` (UUID constant for a single outer agent loop run). When
6
- * an event arrives with a sequence higher than expected, it is buffered. If the
7
- * expected event arrives within the timeout, all buffered events are flushed in order.
8
- * If the timeout expires, the gap is marked as unrecoverable and buffered events are
9
- * flushed anyway.
10
- */
11
-
12
- const SEQUENCE_WAIT_MS = 1500;
13
-
14
- export interface SequencedEvent {
15
- agentLoopSequence?: number | null;
16
- agentLoopId?: string | null;
17
- }
18
-
19
- export interface SequenceBufferOptions<T extends SequencedEvent> {
20
- onEvent: (event: T) => void;
21
- onGap: (from: number, to: number) => void;
22
- }
23
-
24
- export class SequenceBuffer<T extends SequencedEvent> {
25
- private buffer = new Map<number, T>();
26
- private expected = 1;
27
- private timer: ReturnType<typeof setTimeout> | null = null;
28
- private onEvent: (event: T) => void;
29
- private onGap: (from: number, to: number) => void;
30
-
31
- constructor({ onEvent, onGap }: SequenceBufferOptions<T>) {
32
- this.onEvent = onEvent;
33
- this.onGap = onGap;
34
- }
35
-
36
- push(event: T): void {
37
- const seq = event.agentLoopSequence;
38
-
39
- // No sequence info — pass through (old server or non-sequenced event)
40
- if (seq == null) {
41
- this.onEvent(event);
42
- return;
43
- }
44
-
45
- if (seq === this.expected) {
46
- this.clearTimer();
47
- this.onEvent(event);
48
- this.expected++;
49
- this.flush();
50
- } else if (seq > this.expected) {
51
- this.buffer.set(seq, event);
52
- this.startTimer();
53
- }
54
- // seq < expected: duplicate, ignore
55
- }
56
-
57
- reset(): void {
58
- this.buffer.clear();
59
- this.expected = 1;
60
- this.clearTimer();
61
- }
62
-
63
- private flush(): void {
64
- while (this.buffer.has(this.expected)) {
65
- const event = this.buffer.get(this.expected)!;
66
- this.buffer.delete(this.expected);
67
- this.onEvent(event);
68
- this.expected++;
69
- }
70
- if (this.buffer.size > 0) {
71
- this.startTimer();
72
- } else {
73
- this.clearTimer();
74
- }
75
- }
76
-
77
- private startTimer(): void {
78
- if (this.timer) return;
79
- this.timer = setTimeout(() => {
80
- this.timer = null;
81
- const missing = this.expected;
82
- const nextAvailable = Math.min(...this.buffer.keys());
83
- this.onGap(missing, nextAvailable - 1);
84
- this.expected = nextAvailable;
85
- this.flush();
86
- }, SEQUENCE_WAIT_MS);
87
- }
88
-
89
- private clearTimer(): void {
90
- if (this.timer) {
91
- clearTimeout(this.timer);
92
- this.timer = null;
93
- }
94
- }
95
- }