mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-19 08:28:06 +02:00
feat: pass telemetry project id to semantic daemon
This commit is contained in:
parent
1c6c6c853f
commit
2cbafa3df6
7 changed files with 71 additions and 1 deletions
|
|
@ -6,6 +6,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|||
import {
|
||||
computeTelemetryProjectId,
|
||||
loadTelemetryIdentity,
|
||||
readExistingTelemetryProjectId,
|
||||
TELEMETRY_NOTICE,
|
||||
type TelemetryIdentityEnv,
|
||||
} from './identity.js';
|
||||
|
|
@ -156,4 +157,39 @@ describe('telemetry identity', () => {
|
|||
expect(computeTelemetryProjectId('00000000-0000-4000-8000-000000000000', projectDir)).toBe(projectId);
|
||||
expect(computeTelemetryProjectId('11111111-1111-4111-8111-111111111111', projectDir)).not.toBe(projectId);
|
||||
});
|
||||
|
||||
it('reads an existing project id for Python telemetry without creating identity', async () => {
|
||||
await mkdir(join(homeDir, '.ktx'), { recursive: true });
|
||||
await writeFile(
|
||||
join(homeDir, '.ktx', 'telemetry.json'),
|
||||
JSON.stringify(
|
||||
{
|
||||
installId: '00000000-0000-4000-8000-000000000000',
|
||||
enabled: true,
|
||||
noticeShownAt: '2026-05-22T14:33:02.000Z',
|
||||
noticeShownVersion: 1,
|
||||
createdAt: '2026-05-22T14:33:02.000Z',
|
||||
},
|
||||
null,
|
||||
2,
|
||||
) + '\n',
|
||||
'utf-8',
|
||||
);
|
||||
|
||||
await expect(
|
||||
readExistingTelemetryProjectId({
|
||||
homeDir,
|
||||
projectDir: '/tmp/acme-private-project',
|
||||
env: {},
|
||||
}),
|
||||
).resolves.toMatch(/^[a-f0-9]{64}$/);
|
||||
|
||||
await expect(
|
||||
readExistingTelemetryProjectId({
|
||||
homeDir,
|
||||
projectDir: '/tmp/acme-private-project',
|
||||
env: { KTX_TELEMETRY_DISABLED: '1' },
|
||||
}),
|
||||
).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -124,3 +124,21 @@ export async function loadTelemetryIdentity(options: LoadTelemetryIdentityOption
|
|||
export function computeTelemetryProjectId(installId: string, projectDir: string): string {
|
||||
return createHash('sha256').update(`${installId}:${resolve(projectDir)}`).digest('hex');
|
||||
}
|
||||
|
||||
export async function readExistingTelemetryProjectId(options: {
|
||||
projectDir: string;
|
||||
homeDir?: string;
|
||||
env?: Pick<TelemetryIdentityEnv, 'KTX_TELEMETRY_DISABLED' | 'DO_NOT_TRACK'>;
|
||||
}): Promise<string | undefined> {
|
||||
const env = options.env ?? process.env;
|
||||
if (env.KTX_TELEMETRY_DISABLED || env.DO_NOT_TRACK) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const existing = await readTelemetryFile(telemetryPath(options.homeDir ?? homedir()));
|
||||
if (!existing?.enabled) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return computeTelemetryProjectId(existing.installId, options.projectDir);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue