@principles/pd-cli 1.142.3 → 1.143.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.
Files changed (57) hide show
  1. package/dist/commands/__tests__/telemetry-flag-wiring.test.d.ts +9 -0
  2. package/dist/commands/__tests__/telemetry-flag-wiring.test.d.ts.map +1 -0
  3. package/dist/commands/__tests__/telemetry-flag-wiring.test.js +76 -0
  4. package/dist/commands/__tests__/telemetry-flag-wiring.test.js.map +1 -0
  5. package/dist/commands/__tests__/version.test.d.ts +2 -0
  6. package/dist/commands/__tests__/version.test.d.ts.map +1 -0
  7. package/dist/commands/__tests__/version.test.js +134 -0
  8. package/dist/commands/__tests__/version.test.js.map +1 -0
  9. package/dist/commands/console.d.ts.map +1 -1
  10. package/dist/commands/console.js +29 -10
  11. package/dist/commands/console.js.map +1 -1
  12. package/dist/commands/pain-record.d.ts.map +1 -1
  13. package/dist/commands/pain-record.js +6 -15
  14. package/dist/commands/pain-record.js.map +1 -1
  15. package/dist/commands/runtime-activation.d.ts +29 -0
  16. package/dist/commands/runtime-activation.d.ts.map +1 -1
  17. package/dist/commands/runtime-activation.js +70 -31
  18. package/dist/commands/runtime-activation.js.map +1 -1
  19. package/dist/commands/runtime-internalization-enqueue-successors.d.ts.map +1 -1
  20. package/dist/commands/runtime-internalization-enqueue-successors.js +2 -2
  21. package/dist/commands/runtime-internalization-enqueue-successors.js.map +1 -1
  22. package/dist/commands/runtime-internalization-retry.d.ts.map +1 -1
  23. package/dist/commands/runtime-internalization-retry.js +14 -0
  24. package/dist/commands/runtime-internalization-retry.js.map +1 -1
  25. package/dist/commands/telemetry.d.ts +82 -0
  26. package/dist/commands/telemetry.d.ts.map +1 -0
  27. package/dist/commands/telemetry.js +268 -0
  28. package/dist/commands/telemetry.js.map +1 -0
  29. package/dist/commands/version.d.ts +11 -0
  30. package/dist/commands/version.d.ts.map +1 -0
  31. package/dist/commands/version.js +48 -0
  32. package/dist/commands/version.js.map +1 -0
  33. package/dist/index.js +35 -2
  34. package/dist/index.js.map +1 -1
  35. package/dist/services/version-report.d.ts +44 -0
  36. package/dist/services/version-report.d.ts.map +1 -0
  37. package/dist/services/version-report.js +152 -0
  38. package/dist/services/version-report.js.map +1 -0
  39. package/package.json +2 -1
  40. package/src/commands/__tests__/telemetry-flag-wiring.test.ts +84 -0
  41. package/src/commands/__tests__/version.test.ts +141 -0
  42. package/src/commands/console.ts +34 -11
  43. package/src/commands/pain-record.ts +6 -13
  44. package/src/commands/runtime-activation.ts +74 -22
  45. package/src/commands/runtime-internalization-enqueue-successors.ts +3 -2
  46. package/src/commands/runtime-internalization-retry.ts +14 -0
  47. package/src/commands/telemetry.ts +349 -0
  48. package/src/commands/version.ts +49 -0
  49. package/src/index.ts +37 -2
  50. package/src/services/version-report.ts +203 -0
  51. package/tests/commands/console-open.test.ts +55 -5
  52. package/tests/commands/pain-record.test.ts +26 -0
  53. package/tests/commands/runtime-activation-shadow-telemetry.test.ts +130 -0
  54. package/tests/commands/runtime-activation.test.ts +67 -1
  55. package/tests/commands/runtime-internalization-enqueue-successors.test.ts +6 -1
  56. package/tests/commands/telemetry.test.ts +190 -0
  57. package/tests/e2e/cli-full-flow.test.ts +38 -3
@@ -0,0 +1,190 @@
1
+ /**
2
+ * pd telemetry — behavior tests (PRI-597/598 acceptance at the CLI surface).
3
+ *
4
+ * Covers: JSON purity (cli-1), dry-run default + --confirm (cli-4),
5
+ * enable/disable/reset consent transitions against a temp HOME, status
6
+ * never exposing the secret, preview showing the exact payload with the
7
+ * "Preview only. Nothing was sent." banner.
8
+ */
9
+
10
+ import fs from 'node:fs';
11
+ import os from 'node:os';
12
+ import path from 'node:path';
13
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
14
+ import {
15
+ handleTelemetryMutation,
16
+ handleTelemetryPreview,
17
+ handleTelemetryStatus,
18
+ } from '../../src/commands/telemetry.js';
19
+ import { getProductTelemetryStatePath, readProductTelemetryControlState } from '@principles/host-runtime';
20
+
21
+ // Toggleable workspace-resolution failure for the preview fail-loud test.
22
+ // Defaults to delegating to the real resolver so other tests are unaffected.
23
+ const wsMock = vi.hoisted(() => ({ shouldThrow: false }));
24
+ vi.mock('../../src/resolve-workspace.js', async (importOriginal) => {
25
+ const actual = await importOriginal<typeof import('../../src/resolve-workspace.js')>();
26
+ return {
27
+ ...actual,
28
+ resolveWorkspaceDir: (...args: Parameters<typeof actual.resolveWorkspaceDir>) => {
29
+ if (wsMock.shouldThrow) {
30
+ throw new Error('No workspace directory configured.');
31
+ }
32
+ return actual.resolveWorkspaceDir(...args);
33
+ },
34
+ };
35
+ });
36
+
37
+ let tmpHome: string;
38
+ let tmpWorkspace: string;
39
+ let originalUserprofile: string | undefined;
40
+ let originalHome: string | undefined;
41
+ let stdoutSpy: ReturnType<typeof vi.spyOn>;
42
+
43
+ function capturedOutput(): { text: string; json: Record<string, unknown> } {
44
+ expect(stdoutSpy).toHaveBeenCalled();
45
+ const text = stdoutSpy.mock.calls[0][0] as string;
46
+ return { text, json: JSON.parse(text) };
47
+ }
48
+
49
+ beforeEach(() => {
50
+ tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), 'pd-tel-cli-home-'));
51
+ tmpWorkspace = fs.mkdtempSync(path.join(os.tmpdir(), 'pd-tel-cli-ws-'));
52
+ // os.homedir() reads USERPROFILE on Windows and HOME on POSIX — set both so
53
+ // the machine-scope consent store is isolated on every CI runner.
54
+ originalUserprofile = process.env.USERPROFILE;
55
+ originalHome = process.env.HOME;
56
+ process.env.USERPROFILE = tmpHome;
57
+ process.env.HOME = tmpHome;
58
+ stdoutSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
59
+ });
60
+
61
+ afterEach(() => {
62
+ stdoutSpy.mockRestore();
63
+ if (originalUserprofile === undefined) {
64
+ delete process.env.USERPROFILE;
65
+ } else {
66
+ process.env.USERPROFILE = originalUserprofile;
67
+ }
68
+ if (originalHome === undefined) {
69
+ delete process.env.HOME;
70
+ } else {
71
+ process.env.HOME = originalHome;
72
+ }
73
+ fs.rmSync(tmpHome, { recursive: true, force: true });
74
+ fs.rmSync(tmpWorkspace, { recursive: true, force: true });
75
+ });
76
+
77
+ describe('pd telemetry status', () => {
78
+ it('--json outputs exactly one parseable JSON object with consent gates and no secret', async () => {
79
+ await handleTelemetryMutation('enable', { confirm: true, json: true, workspace: tmpWorkspace });
80
+ stdoutSpy.mockClear();
81
+ await handleTelemetryStatus({ json: true, workspace: tmpWorkspace });
82
+ const { text, json } = capturedOutput();
83
+ expect(stdoutSpy).toHaveBeenCalledTimes(1);
84
+ expect(json.status).toBe('ok');
85
+ expect(json.command).toBe('telemetry:status');
86
+ expect(json.consent).toBe('granted');
87
+ expect(json.canExport).toBe(false); // flag off + repo-checkout suppression in tests — expected
88
+ const stateRead = readProductTelemetryControlState(tmpHome);
89
+ const secret = stateRead.ok ? stateRead.state.telemetrySecret : undefined;
90
+ expect(secret).toMatch(/^[0-9a-f]{64}$/);
91
+ expect(text).not.toContain(secret as string);
92
+ expect(JSON.stringify(json)).not.toContain(secret as string);
93
+ expect(json.blockers).toEqual(expect.arrayContaining(['feature_flag_disabled']));
94
+ });
95
+
96
+ it('works with no resolvable workspace passed explicitly (machine-scope status)', async () => {
97
+ // No --workspace: resolution falls back to discovery; machine-scope
98
+ // fields must render regardless of what discovery finds on this machine.
99
+ await handleTelemetryStatus({ json: true });
100
+ const { json } = capturedOutput();
101
+ expect(json.status).toBe('ok');
102
+ expect(json.command).toBe('telemetry:status');
103
+ expect(json.consent).toBe('unset');
104
+ expect(json.hasSecret).toBe(false);
105
+ expect(json.flagEnabled).toBeDefined();
106
+ });
107
+ });
108
+
109
+ describe('pd telemetry enable/disable/reset (cli-4 dry-run default)', () => {
110
+ it('enable without --confirm is a dry-run: nothing written', async () => {
111
+ await handleTelemetryMutation('enable', { json: true });
112
+ const { json } = capturedOutput();
113
+ expect(json.applied).toBe(false);
114
+ expect(json.dryRun).toBe(true);
115
+ expect(fs.existsSync(getProductTelemetryStatePath(tmpHome))).toBe(false);
116
+ });
117
+
118
+ it('enable --confirm writes granted consent with a secret', async () => {
119
+ await handleTelemetryMutation('enable', { confirm: true, json: true });
120
+ const { json } = capturedOutput();
121
+ expect(json.applied).toBe(true);
122
+ expect(json.consent).toBe('granted');
123
+ const state = readProductTelemetryControlState(tmpHome);
124
+ expect(state.ok && state.state.consent).toBe('granted');
125
+ expect(state.ok && state.state.telemetrySecret).toMatch(/^[0-9a-f]{64}$/);
126
+ });
127
+
128
+ it('--dry-run and --confirm together are rejected (cli-4 mutex)', async () => {
129
+ await handleTelemetryMutation('enable', { dryRun: true, confirm: true, json: true });
130
+ const { json } = capturedOutput();
131
+ expect(json.ok).toBe(false);
132
+ expect(process.exitCode).toBe(1);
133
+ process.exitCode = 0;
134
+ });
135
+
136
+ it('disable --confirm deletes identity and preserves the denied choice', async () => {
137
+ await handleTelemetryMutation('enable', { confirm: true, json: true, workspace: tmpWorkspace });
138
+ await handleTelemetryMutation('disable', { confirm: true, json: true });
139
+ const state = readProductTelemetryControlState(tmpHome);
140
+ expect(state.ok && state.state.consent).toBe('denied');
141
+ expect(state.ok && state.state.telemetrySecret).toBeUndefined();
142
+ });
143
+
144
+ it('reset --confirm rotates the secret while consent stays granted', async () => {
145
+ await handleTelemetryMutation('enable', { confirm: true, json: true, workspace: tmpWorkspace });
146
+ const before = readProductTelemetryControlState(tmpHome);
147
+ const secretBefore = before.ok ? before.state.telemetrySecret : undefined;
148
+ await handleTelemetryMutation('reset', { confirm: true, json: true });
149
+ const after = readProductTelemetryControlState(tmpHome);
150
+ expect(after.ok && after.state.consent).toBe('granted');
151
+ expect(after.ok && after.state.telemetrySecret).not.toBe(secretBefore);
152
+ });
153
+ });
154
+
155
+ describe('pd telemetry preview', () => {
156
+ it('shows the exact snapshot with the never-sent banner and zero network activity', async () => {
157
+ await handleTelemetryMutation('enable', { confirm: true, json: true, workspace: tmpWorkspace });
158
+ stdoutSpy.mockClear();
159
+ await handleTelemetryPreview({ json: true, workspace: tmpWorkspace });
160
+ const { text, json } = capturedOutput();
161
+ expect(json.banner).toBe('Preview only. Nothing was sent.');
162
+ expect(text).toContain('Preview only. Nothing was sent.');
163
+ const snapshot = json.snapshot as Record<string, unknown>;
164
+ expect(snapshot.schemaVersion).toBe('1');
165
+ expect(snapshot.milestones).toBeDefined();
166
+ // Conservative booleans for an empty workspace fixture.
167
+ const milestones = snapshot.milestones as Record<string, unknown>;
168
+ expect(milestones.initialized).toBe(false);
169
+ expect(milestones.painObserved).toBe(false);
170
+ // The stored secret never appears in the preview output.
171
+ const state = readProductTelemetryControlState(tmpHome);
172
+ const secret = state.ok ? state.state.telemetrySecret : undefined;
173
+ expect(text).not.toContain(secret ?? 'no-secret');
174
+ });
175
+
176
+ it('fails loud with nextAction when workspace resolution throws', async () => {
177
+ wsMock.shouldThrow = true;
178
+ try {
179
+ await handleTelemetryPreview({ json: true });
180
+ const { json } = capturedOutput();
181
+ expect(json.ok).toBe(false);
182
+ expect(json.reason).toContain('workspace_unresolvable');
183
+ expect(json.nextAction).toBeDefined();
184
+ expect(process.exitCode).toBe(1);
185
+ process.exitCode = 0;
186
+ } finally {
187
+ wsMock.shouldThrow = false;
188
+ }
189
+ });
190
+ });
@@ -106,12 +106,13 @@ function readExitCode(obj: unknown): number {
106
106
 
107
107
  async function runPd(
108
108
  args: string[],
109
- options: { timeout?: number } = {},
109
+ options: { timeout?: number; env?: NodeJS.ProcessEnv } = {},
110
110
  ): Promise<RunResult> {
111
111
  try {
112
112
  const { stdout, stderr } = await execFileAsync('node', [PD_BIN, ...args], {
113
113
  timeout: options.timeout ?? 30_000,
114
114
  maxBuffer: 10 * 1024 * 1024,
115
+ env: options.env,
115
116
  });
116
117
  return { stdout, stderr, exitCode: 0 };
117
118
  } catch (err: unknown) {
@@ -136,9 +137,43 @@ describe('CLI full flow', () => {
136
137
  });
137
138
 
138
139
  it('pd --version exits 0 and prints version', async () => {
139
- const { stdout, exitCode } = await runPd(['--version']);
140
+ const home = makeWorkspace();
141
+ const { stdout, exitCode } = await runPd(['--version'], {
142
+ env: { ...process.env, HOME: home, USERPROFILE: home },
143
+ });
140
144
  expect(exitCode).toBe(0);
141
- expect(stdout.trim()).toMatch(/^\d+\.\d+\.\d+/);
145
+ // SPEC §12 stable short-text contract. On a machine without a supported
146
+ // installation the CLI falls back to its own package version and marks
147
+ // itself as a development checkout instead of impersonating a release.
148
+ expect(stdout.trim()).toMatch(/^Principles Disciple \d+\.\d+\.\d+ \([a-f0-9]{12}|^Principles Disciple \d+\.\d+\.\d+ \(development-checkout\)$/);
149
+ });
150
+
151
+ it('pd --version refuses a corrupt installed state instead of impersonating a development checkout', async () => {
152
+ const home = makeWorkspace();
153
+ fs.mkdirSync(path.join(home, '.pd'), { recursive: true });
154
+ fs.writeFileSync(path.join(home, '.pd', 'active.json'), '{');
155
+ const { stdout, stderr, exitCode } = await runPd(['--version'], {
156
+ env: { ...process.env, HOME: home, USERPROFILE: home },
157
+ });
158
+ expect(exitCode).toBe(1);
159
+ expect(stdout).toBe('');
160
+ expect(stderr).toMatch(/not valid JSON/i);
161
+ expect(stderr).toMatch(/Next:/);
162
+ expect(stderr).not.toContain('development-checkout');
163
+ });
164
+
165
+ it('pd version --json returns exactly one structured refusal for corrupt JSON state', async () => {
166
+ const home = makeWorkspace();
167
+ fs.mkdirSync(path.join(home, '.pd'), { recursive: true });
168
+ fs.writeFileSync(path.join(home, '.pd', 'active.json'), '{');
169
+ const { stdout, stderr, exitCode } = await runPd(['version', '--json'], {
170
+ env: { ...process.env, HOME: home, USERPROFILE: home },
171
+ });
172
+ expect(exitCode).toBe(1);
173
+ expect(stderr).toBe('');
174
+ const result = requireRecord(JSON.parse(stdout), 'corrupt version response');
175
+ expect(result).toMatchObject({ ok: false, reason: 'state_corrupt' });
176
+ expect(typeof result.nextAction).toBe('string');
142
177
  });
143
178
 
144
179
  it('pd runtime features --json on fresh workspace returns valid JSON with defaults', async () => {