Skip to content

Commit a300fbd

Browse files
committed
refactor(cli): eagerly resolve TelemetryClient distinctId via async factory
Made-with: Cursor
1 parent fd138e2 commit a300fbd

15 files changed

Lines changed: 137 additions & 118 deletions

packages/cli/cli-v2/src/__test__/ApiChecker.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe("ApiChecker", () => {
2626
}
2727

2828
const checker = new ApiChecker({
29-
context: createTestContext({ cwd }),
29+
context: await createTestContext({ cwd }),
3030
cliVersion: "0.0.0"
3131
});
3232

@@ -53,7 +53,7 @@ describe("ApiChecker", () => {
5353
}
5454

5555
const checker = new ApiChecker({
56-
context: createTestContext({ cwd }),
56+
context: await createTestContext({ cwd }),
5757
cliVersion: "0.0.0"
5858
});
5959

@@ -71,7 +71,7 @@ describe("ApiChecker", () => {
7171
const workspace = await loadWorkspace("simple-api");
7272

7373
const checker = new ApiChecker({
74-
context: createTestContext({ cwd }),
74+
context: await createTestContext({ cwd }),
7575
cliVersion: "0.0.0"
7676
});
7777

@@ -89,7 +89,7 @@ describe("ApiChecker", () => {
8989
const workspace = await loadWorkspace("simple-api");
9090

9191
const checker = new ApiChecker({
92-
context: createTestContext({ cwd }),
92+
context: await createTestContext({ cwd }),
9393
cliVersion: "0.0.0"
9494
});
9595

@@ -110,7 +110,7 @@ describe("ApiChecker", () => {
110110
const workspace = await loadWorkspace("simple-api");
111111

112112
const checker = new ApiChecker({
113-
context: createTestContext({ cwd }),
113+
context: await createTestContext({ cwd }),
114114
cliVersion: "0.0.0"
115115
});
116116

@@ -128,7 +128,7 @@ describe("ApiChecker", () => {
128128
const workspace = await loadWorkspace("simple-api");
129129

130130
const checker = new ApiChecker({
131-
context: createTestContext({ cwd }),
131+
context: await createTestContext({ cwd }),
132132
cliVersion: "0.0.0"
133133
});
134134

@@ -146,7 +146,7 @@ describe("ApiChecker", () => {
146146
const workspace = await loadWorkspace("simple-api");
147147

148148
const checker = new ApiChecker({
149-
context: createTestContext({ cwd }),
149+
context: await createTestContext({ cwd }),
150150
cliVersion: "0.0.0"
151151
});
152152

packages/cli/cli-v2/src/__test__/DocsChecker.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe("DocsChecker", () => {
2929
const cwd = AbsoluteFilePath.of(join(__dirname, "fixtures/simple-api"));
3030

3131
const checker = new DocsChecker({
32-
context: createTestContext({ cwd })
32+
context: await createTestContext({ cwd })
3333
});
3434

3535
const result = await checker.check({ workspace });
@@ -49,7 +49,7 @@ describe("DocsChecker", () => {
4949

5050
const workspace = await loadTempWorkspace(testDir);
5151
const checker = new DocsChecker({
52-
context: createTestContext({ cwd: testDir })
52+
context: await createTestContext({ cwd: testDir })
5353
});
5454

5555
const result = await checker.check({ workspace });
@@ -69,7 +69,7 @@ describe("DocsChecker", () => {
6969

7070
const workspace = await loadTempWorkspace(testDir);
7171
const checker = new DocsChecker({
72-
context: createTestContext({ cwd: testDir })
72+
context: await createTestContext({ cwd: testDir })
7373
});
7474

7575
const result = await checker.check({ workspace });
@@ -84,7 +84,7 @@ describe("DocsChecker", () => {
8484

8585
const workspace = await loadTempWorkspace(testDir);
8686
const checker = new DocsChecker({
87-
context: createTestContext({ cwd: testDir })
87+
context: await createTestContext({ cwd: testDir })
8888
});
8989

9090
const resultStrict = await checker.check({ workspace, strict: true });
@@ -133,7 +133,7 @@ docs:
133133

134134
const workspace = await loadTempWorkspace(testDir);
135135
const checker = new DocsChecker({
136-
context: createTestContext({ cwd: testDir })
136+
context: await createTestContext({ cwd: testDir })
137137
});
138138

139139
const result = await checker.check({ workspace, strict: true });

packages/cli/cli-v2/src/__test__/LegacyApiSpecAdapter.test.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import type { OpenAPISpec, OpenRPCSpec } from "@fern-api/api-workspace-commons";
22
import { generatorsYml } from "@fern-api/configuration";
33
import { AbsoluteFilePath } from "@fern-api/fs-utils";
4-
import { describe, expect, it } from "vitest";
4+
import { beforeEach, describe, expect, it } from "vitest";
55
import { LegacyApiSpecAdapter } from "../api/adapter/LegacyApiSpecAdapter.js";
66
import type { AsyncApiSpec } from "../api/config/AsyncApiSpec.js";
77
import type { OpenApiSpec } from "../api/config/OpenApiSpec.js";
88
import type { OpenRpcSpec } from "../api/config/OpenRpcSpec.js";
9+
import type { Context } from "../context/Context.js";
910
import { createTestContext } from "./utils/createTestContext.js";
1011

1112
describe("LegacyApiSpecAdapter", () => {
1213
const cwd = AbsoluteFilePath.of("/test/path");
13-
const context = createTestContext({ cwd });
14-
const adapter = new LegacyApiSpecAdapter({ context });
14+
let context: Context;
15+
let adapter: LegacyApiSpecAdapter;
16+
17+
beforeEach(async () => {
18+
context = await createTestContext({ cwd });
19+
adapter = new LegacyApiSpecAdapter({ context });
20+
});
1521

1622
describe("convertOpenApiSettings", () => {
1723
it("returns undefined when settings are not provided", () => {

packages/cli/cli-v2/src/__test__/LegacyFernWorkspaceAdapter.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe("LegacyFernWorkspaceAdapter", () => {
2727

2828
it("adapts Fern spec to FernWorkspace using LazyFernWorkspace", async () => {
2929
const { cwd, apiDefinition } = await loadApiDefinition("fern-definition");
30-
const adapter = createAdapter(cwd);
30+
const adapter = await createAdapter(cwd);
3131

3232
const fernWorkspace = await adapter.adapt(apiDefinition);
3333
expect(fernWorkspace).toBeDefined();
@@ -44,7 +44,7 @@ describe("LegacyFernWorkspaceAdapter", () => {
4444

4545
it("loads types and services from Fern definition files", async () => {
4646
const { cwd, apiDefinition } = await loadApiDefinition("fern-definition");
47-
const adapter = createAdapter(cwd);
47+
const adapter = await createAdapter(cwd);
4848

4949
const fernWorkspace = await adapter.adapt(apiDefinition);
5050
const definitionFiles = fernWorkspace.definition.namedDefinitionFiles;
@@ -82,7 +82,7 @@ describe("LegacyFernWorkspaceAdapter", () => {
8282
expect(isOpenApiSpec(firstSpec)).toBe(true);
8383
}
8484

85-
const adapter = createAdapter(cwd);
85+
const adapter = await createAdapter(cwd);
8686
const fernWorkspace = await adapter.adapt(apiDefinition);
8787
expect(fernWorkspace).toBeDefined();
8888
expect(fernWorkspace.definition).toBeDefined();
@@ -103,7 +103,7 @@ describe("LegacyFernWorkspaceAdapter", () => {
103103

104104
it("adapts Conjure spec to FernWorkspace using ConjureWorkspace", async () => {
105105
const { cwd, apiDefinition } = await loadApiDefinition("conjure-definition");
106-
const adapter = createAdapter(cwd);
106+
const adapter = await createAdapter(cwd);
107107

108108
const fernWorkspace = await adapter.adapt(apiDefinition);
109109
expect(fernWorkspace).toBeDefined();
@@ -120,7 +120,7 @@ describe("LegacyFernWorkspaceAdapter", () => {
120120
expect(spec.conjure.toString()).toContain("conjure-definition/conjure");
121121
}
122122

123-
const adapter = createAdapter(cwd);
123+
const adapter = await createAdapter(cwd);
124124
const fernWorkspace = await adapter.adapt(apiDefinition);
125125
expect(fernWorkspace).toBeDefined();
126126
expect(fernWorkspace.absoluteFilePath.toString()).toBe(cwd.toString());
@@ -130,7 +130,7 @@ describe("LegacyFernWorkspaceAdapter", () => {
130130
describe("Multiple OpenAPI Specs", () => {
131131
it("allows multiple OpenAPI specs to be mixed together", async () => {
132132
const cwd = AbsoluteFilePath.of(join(FIXTURES_DIR, "simple-api"));
133-
const adapter = createAdapter(cwd);
133+
const adapter = await createAdapter(cwd);
134134

135135
const openApiSpec1: OpenApiSpec = {
136136
openapi: AbsoluteFilePath.of(join(FIXTURES_DIR, "simple-api", "openapi.yml"))
@@ -173,7 +173,7 @@ describe("LegacyFernWorkspaceAdapter", () => {
173173

174174
it("override auth scheme", async () => {
175175
const { cwd, apiDefinition } = await loadApiDefinition("api-config-override");
176-
const adapter = createAdapter(cwd);
176+
const adapter = await createAdapter(cwd);
177177

178178
const fernWorkspace = await adapter.adapt(apiDefinition);
179179
expect(fernWorkspace).toBeDefined();
@@ -186,7 +186,7 @@ describe("LegacyFernWorkspaceAdapter", () => {
186186

187187
it("override environments", async () => {
188188
const { cwd, apiDefinition } = await loadApiDefinition("api-config-override");
189-
const adapter = createAdapter(cwd);
189+
const adapter = await createAdapter(cwd);
190190

191191
const fernWorkspace = await adapter.adapt(apiDefinition);
192192
expect(fernWorkspace).toBeDefined();
@@ -207,8 +207,8 @@ describe("LegacyFernWorkspaceAdapter", () => {
207207
/**
208208
* Creates an adapter for testing with the given cwd.
209209
*/
210-
function createAdapter(cwd: AbsoluteFilePath): LegacyFernWorkspaceAdapter {
211-
const context = createTestContext({ cwd });
210+
async function createAdapter(cwd: AbsoluteFilePath): Promise<LegacyFernWorkspaceAdapter> {
211+
const context = await createTestContext({ cwd });
212212
const task = createMockTask();
213213
return new LegacyFernWorkspaceAdapter({
214214
context,

packages/cli/cli-v2/src/__test__/SdkChecker.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("SdkChecker", () => {
3434
const workspace = await loadWorkspace("simple-api");
3535

3636
const checker = new SdkChecker({
37-
context: createTestContext({ cwd }),
37+
context: await createTestContext({ cwd }),
3838
versionChecker: NOOP_VERSION_CHECKER
3939
});
4040

@@ -61,7 +61,7 @@ api:
6161
const workspace = await loadTempWorkspace(testDir);
6262

6363
const checker = new SdkChecker({
64-
context: createTestContext({ cwd: testDir }),
64+
context: await createTestContext({ cwd: testDir }),
6565
versionChecker: NOOP_VERSION_CHECKER
6666
});
6767

@@ -98,7 +98,7 @@ sdks:
9898

9999
const workspace = await loadTempWorkspace(testDir);
100100
const checker = new SdkChecker({
101-
context: createTestContext({ cwd: testDir }),
101+
context: await createTestContext({ cwd: testDir }),
102102
versionChecker: NOOP_VERSION_CHECKER
103103
});
104104

@@ -113,7 +113,7 @@ sdks:
113113
const workspace = await loadWorkspace("simple-api");
114114

115115
const checker = new SdkChecker({
116-
context: createTestContext({ cwd }),
116+
context: await createTestContext({ cwd }),
117117
versionChecker: NOOP_VERSION_CHECKER
118118
});
119119

@@ -146,7 +146,7 @@ sdks:
146146

147147
const workspace = await loadTempWorkspace(testDir);
148148
const checker = new SdkChecker({
149-
context: createTestContext({ cwd: testDir }),
149+
context: await createTestContext({ cwd: testDir }),
150150
versionChecker: NOOP_VERSION_CHECKER
151151
});
152152

@@ -163,7 +163,7 @@ sdks:
163163
const workspace = await loadWorkspace("simple-api");
164164

165165
const checker = new SdkChecker({
166-
context: createTestContext({ cwd }),
166+
context: await createTestContext({ cwd }),
167167
versionChecker: NOOP_VERSION_CHECKER
168168
});
169169

@@ -198,7 +198,7 @@ sdks:
198198

199199
const workspace = await loadTempWorkspace(testDir);
200200
const checker = new SdkChecker({
201-
context: createTestContext({ cwd: testDir }),
201+
context: await createTestContext({ cwd: testDir }),
202202
versionChecker: NOOP_VERSION_CHECKER
203203
});
204204

@@ -213,7 +213,7 @@ sdks:
213213
const workspace = await loadWorkspace("simple-api");
214214

215215
const checker = new SdkChecker({
216-
context: createTestContext({ cwd }),
216+
context: await createTestContext({ cwd }),
217217
versionChecker: NOOP_VERSION_CHECKER
218218
});
219219

@@ -229,7 +229,7 @@ sdks:
229229
const workspace = await loadWorkspace("simple-api");
230230

231231
const checker = new SdkChecker({
232-
context: createTestContext({ cwd }),
232+
context: await createTestContext({ cwd }),
233233
versionChecker: async ({ target }) => ({
234234
violation: {
235235
severity: "warning",

packages/cli/cli-v2/src/__test__/check.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ paths: {}
5151
`
5252
);
5353

54-
const { context, getStdout, getStderr } = createTestContextWithCapture({ cwd: testDir });
54+
const { context, getStdout, getStderr } = await createTestContextWithCapture({ cwd: testDir });
5555
const cmd = new CheckCommand();
5656

5757
// The sdk checker will find the defaultGroup violation, which causes an error exit.
@@ -129,7 +129,7 @@ paths: {}
129129
`
130130
);
131131

132-
const { context, getStdout, getStderr } = createTestContextWithCapture({ cwd: testDir });
132+
const { context, getStdout, getStderr } = await createTestContextWithCapture({ cwd: testDir });
133133
const cmd = new SdkCheckCommand();
134134

135135
try {

packages/cli/cli-v2/src/__test__/compile.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe("fern api compile", () => {
3939
});
4040

4141
it("writes valid IR JSON to the specified file", async () => {
42-
const { context } = createTestContextWithCapture({ cwd: SIMPLE_API_DIR });
42+
const { context } = await createTestContextWithCapture({ cwd: SIMPLE_API_DIR });
4343
await cmd.handle(context, baseArgs({ output: outputPath }));
4444

4545
expect(await doesPathExist(AbsoluteFilePath.of(outputPath))).toBe(true);
@@ -50,7 +50,7 @@ describe("fern api compile", () => {
5050
});
5151

5252
it("writes no human-readable output to stderr at default log level", async () => {
53-
const { context, getStderr } = createTestContextWithCapture({ cwd: SIMPLE_API_DIR });
53+
const { context, getStderr } = await createTestContextWithCapture({ cwd: SIMPLE_API_DIR });
5454
await cmd.handle(context, baseArgs({ output: outputPath }));
5555

5656
expect(getStderr()).toBe("");
@@ -59,14 +59,14 @@ describe("fern api compile", () => {
5959

6060
describe("no --output (validate only)", () => {
6161
it("produces no stdout output", async () => {
62-
const { context, getStdout } = createTestContextWithCapture({ cwd: SIMPLE_API_DIR });
62+
const { context, getStdout } = await createTestContextWithCapture({ cwd: SIMPLE_API_DIR });
6363
await cmd.handle(context, baseArgs());
6464

6565
expect(getStdout()).toBe("");
6666
});
6767

6868
it("produces no stderr output at default log level", async () => {
69-
const { context, getStderr } = createTestContextWithCapture({ cwd: SIMPLE_API_DIR });
69+
const { context, getStderr } = await createTestContextWithCapture({ cwd: SIMPLE_API_DIR });
7070
await cmd.handle(context, baseArgs());
7171

7272
expect(getStderr()).toBe("");
@@ -89,7 +89,7 @@ describe("fern api compile", () => {
8989
});
9090

9191
it("compiles a specific API by name", async () => {
92-
const { context } = createTestContextWithCapture({ cwd: SIMPLE_API_DIR });
92+
const { context } = await createTestContextWithCapture({ cwd: SIMPLE_API_DIR });
9393
await cmd.handle(context, baseArgs({ api: "api", output: outputPath }));
9494

9595
const content = await readFile(outputPath, "utf-8");
@@ -98,7 +98,7 @@ describe("fern api compile", () => {
9898
});
9999

100100
it("throws CliError for a non-existent API name", async () => {
101-
const { context } = createTestContextWithCapture({ cwd: SIMPLE_API_DIR });
101+
const { context } = await createTestContextWithCapture({ cwd: SIMPLE_API_DIR });
102102
await expect(cmd.handle(context, baseArgs({ api: "nonexistent" }))).rejects.toThrow(
103103
"API 'nonexistent' not found"
104104
);
@@ -107,7 +107,7 @@ describe("fern api compile", () => {
107107

108108
describe("validation errors", () => {
109109
it("throws CliError with error details for an invalid API", async () => {
110-
const { context, getStderr } = createTestContextWithCapture({ cwd: INVALID_API_DIR });
110+
const { context, getStderr } = await createTestContextWithCapture({ cwd: INVALID_API_DIR });
111111
await expect(cmd.handle(context, baseArgs())).rejects.toThrow(CliError);
112112

113113
const stderr = getStderr();

0 commit comments

Comments
 (0)