Skip to content

Commit 9798fe0

Browse files
committed
fix: tests
1 parent 456ed10 commit 9798fe0

File tree

3 files changed

+39
-118
lines changed

3 files changed

+39
-118
lines changed
Lines changed: 35 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { describe, expect, it } from 'vitest';
22

3-
import { transformCodeSamplesToGuideMethods } from '../snippets.js';
4-
import { SnippetSamples } from '../types.js';
3+
import { parseCodeSamples } from '../snippets.js';
4+
import { CodeSamples } from '../types.js';
55

66
describe('init', () => {
77
it('parses a multi line import', () => {
88
expect(
9-
JSON.parse(
10-
transformCodeSamplesToGuideMethods({
11-
csharp: {
12-
foo: {
13-
default: `
9+
JSON.stringify(
10+
parseCodeSamples({
11+
foo: {
12+
default: `
1413
// Initialize the client
1514
var client = new QuerySuggestionsClient(
1615
new QuerySuggestionsConfig("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION")
@@ -40,53 +39,26 @@ var response = await client.CreateConfigAsync(
4039
);
4140
// >LOG
4241
`,
43-
},
4442
},
45-
} as unknown as SnippetSamples),
43+
} as unknown as CodeSamples),
44+
null,
45+
2,
4646
),
4747
).toMatchInlineSnapshot(`
48-
{
49-
"csharp": {
50-
"foo": {
51-
"default": "var response = await client.CreateConfigAsync(
52-
new ConfigurationWithIndex
53-
{
54-
IndexName = "<YOUR_INDEX_NAME>",
55-
SourceIndices = new List<SourceIndex>
56-
{
57-
new SourceIndex
58-
{
59-
IndexName = "<YOUR_INDEX_NAME>",
60-
Facets = new List<Facet> { new Facet { Attribute = "test" } },
61-
Generate = new List<List<string>>
62-
{
63-
new List<string> { "facetA", "facetB" },
64-
new List<string> { "facetC" },
65-
},
66-
},
67-
},
68-
Languages = new Languages(new List<string> { "french" }),
69-
Exclude = new List<string> { "test" },
48+
"{
49+
"foo": {
50+
"default": "\\n // Initialize the client\\nvar client = new QuerySuggestionsClient(\\n new QuerySuggestionsConfig(\\"YOUR_APP_ID\\", \\"YOUR_API_KEY\\", \\"YOUR_APP_ID_REGION\\")\\n);\\n\\n// Call the API\\nvar response = await client.CreateConfigAsync(\\n new ConfigurationWithIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n SourceIndices = new List<SourceIndex>\\n {\\n new SourceIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n Facets = new List<Facet> { new Facet { Attribute = \\"test\\" } },\\n Generate = new List<List<string>>\\n {\\n new List<string> { \\"facetA\\", \\"facetB\\" },\\n new List<string> { \\"facetC\\" },\\n },\\n },\\n },\\n Languages = new Languages(new List<string> { \\"french\\" }),\\n Exclude = new List<string> { \\"test\\" },\\n }\\n);\\n// >LOG\\n "
7051
}
71-
);",
72-
},
73-
"init": {
74-
"default": "var client = new QuerySuggestionsClient(
75-
new QuerySuggestionsConfig("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION")
76-
);",
77-
},
78-
},
79-
}
52+
}"
8053
`);
8154
});
8255

8356
it('parses a single line import', () => {
8457
expect(
85-
JSON.parse(
86-
transformCodeSamplesToGuideMethods({
87-
csharp: {
88-
foo: {
89-
default: `
58+
JSON.stringify(
59+
parseCodeSamples({
60+
foo: {
61+
default: `
9062
// Initialize the client
9163
var client = new QuerySuggestionsClient(new Client("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION"));
9264
@@ -114,53 +86,28 @@ var response = await client.CreateConfigAsync(
11486
);
11587
// >LOG
11688
`,
117-
},
11889
},
119-
} as unknown as SnippetSamples),
90+
} as unknown as CodeSamples),
91+
null,
92+
2,
12093
),
12194
).toMatchInlineSnapshot(`
122-
{
123-
"csharp": {
124-
"foo": {
125-
"default": "var response = await client.CreateConfigAsync(
126-
new ConfigurationWithIndex
127-
{
128-
IndexName = "<YOUR_INDEX_NAME>",
129-
SourceIndices = new List<SourceIndex>
130-
{
131-
new SourceIndex
132-
{
133-
IndexName = "<YOUR_INDEX_NAME>",
134-
Facets = new List<Facet> { new Facet { Attribute = "test" } },
135-
Generate = new List<List<string>>
136-
{
137-
new List<string> { "facetA", "facetB" },
138-
new List<string> { "facetC" },
139-
},
140-
},
141-
},
142-
Languages = new Languages(new List<string> { "french" }),
143-
Exclude = new List<string> { "test" },
95+
"{
96+
"foo": {
97+
"default": "\\n // Initialize the client\\nvar client = new QuerySuggestionsClient(new Client(\\"YOUR_APP_ID\\", \\"YOUR_API_KEY\\", \\"YOUR_APP_ID_REGION\\"));\\n\\n// Call the API\\nvar response = await client.CreateConfigAsync(\\n new ConfigurationWithIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n SourceIndices = new List<SourceIndex>\\n {\\n new SourceIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n Facets = new List<Facet> { new Facet { Attribute = \\"test\\" } },\\n Generate = new List<List<string>>\\n {\\n new List<string> { \\"facetA\\", \\"facetB\\" },\\n new List<string> { \\"facetC\\" },\\n },\\n },\\n },\\n Languages = new Languages(new List<string> { \\"french\\" }),\\n Exclude = new List<string> { \\"test\\" },\\n }\\n);\\n// >LOG\\n "
14498
}
145-
);",
146-
},
147-
"init": {
148-
"default": "var client = new QuerySuggestionsClient(new Client("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION"));",
149-
},
150-
},
151-
}
99+
}"
152100
`);
153101
});
154102
});
155103

156104
describe('initialize', () => {
157105
it("doesn't stop at `client`", () => {
158106
expect(
159-
JSON.parse(
160-
transformCodeSamplesToGuideMethods({
161-
csharp: {
162-
foo: {
163-
default: `
107+
JSON.stringify(
108+
parseCodeSamples({
109+
foo: {
110+
default: `
164111
// Initialize the client foo bar BAAAAAAAAAAAAAAAAAAAAAZ
165112
var client = new QuerySuggestionsClient(
166113
new QuerySuggestionsConfig("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION")
@@ -190,43 +137,17 @@ var response = await client.CreateConfigAsync(
190137
);
191138
// >LOG
192139
`,
193-
},
194140
},
195-
} as unknown as SnippetSamples),
141+
} as unknown as CodeSamples),
142+
null,
143+
2,
196144
),
197145
).toMatchInlineSnapshot(`
198-
{
199-
"csharp": {
200-
"foo": {
201-
"default": "var response = await client.CreateConfigAsync(
202-
new ConfigurationWithIndex
203-
{
204-
IndexName = "<YOUR_INDEX_NAME>",
205-
SourceIndices = new List<SourceIndex>
206-
{
207-
new SourceIndex
208-
{
209-
IndexName = "<YOUR_INDEX_NAME>",
210-
Facets = new List<Facet> { new Facet { Attribute = "test" } },
211-
Generate = new List<List<string>>
212-
{
213-
new List<string> { "facetA", "facetB" },
214-
new List<string> { "facetC" },
215-
},
216-
},
217-
},
218-
Languages = new Languages(new List<string> { "french" }),
219-
Exclude = new List<string> { "test" },
146+
"{
147+
"foo": {
148+
"default": "\\n // Initialize the client foo bar BAAAAAAAAAAAAAAAAAAAAAZ\\nvar client = new QuerySuggestionsClient(\\n new QuerySuggestionsConfig(\\"YOUR_APP_ID\\", \\"YOUR_API_KEY\\", \\"YOUR_APP_ID_REGION\\")\\n);\\n\\n// Call the API\\nvar response = await client.CreateConfigAsync(\\n new ConfigurationWithIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n SourceIndices = new List<SourceIndex>\\n {\\n new SourceIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n Facets = new List<Facet> { new Facet { Attribute = \\"test\\" } },\\n Generate = new List<List<string>>\\n {\\n new List<string> { \\"facetA\\", \\"facetB\\" },\\n new List<string> { \\"facetC\\" },\\n },\\n },\\n },\\n Languages = new Languages(new List<string> { \\"french\\" }),\\n Exclude = new List<string> { \\"test\\" },\\n }\\n);\\n// >LOG\\n "
220149
}
221-
);",
222-
},
223-
"init": {
224-
"default": "var client = new QuerySuggestionsClient(
225-
new QuerySuggestionsConfig("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION")
226-
);",
227-
},
228-
},
229-
}
150+
}"
230151
`);
231152
});
232153
});

scripts/specs/format.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { GENERATORS, run, toAbsolutePath } from '../common.js';
99
import { createSpinner } from '../spinners.js';
1010
import type { Spec } from '../types.js';
1111

12-
import { bundleCodeSamplesForDoc, getCodeSampleLabel, transformGeneratedSnippetsToCodeSamples } from './snippets.js';
12+
import { getCodeSampleLabel, parseCodeSamples, transformGeneratedSnippetsToCodeSamples } from './snippets.js';
1313

1414
export async function lintCommon(useCache: boolean): Promise<void> {
1515
const spinner = createSpinner('linting common spec');
@@ -52,7 +52,7 @@ export async function bundleSpecsForDoc(bundledPath: string, clientName: string)
5252
const tagsDefinitions = bundledSpec.tags;
5353
const codeSamples = await transformGeneratedSnippetsToCodeSamples(clientName);
5454

55-
await bundleCodeSamplesForDoc(JSON.parse(JSON.stringify(codeSamples)), clientName);
55+
parseCodeSamples(JSON.parse(JSON.stringify(codeSamples)));
5656

5757
for (const [pathKey, pathMethods] of Object.entries(bundledSpec.paths)) {
5858
for (const [method, specMethod] of Object.entries(pathMethods)) {

scripts/specs/snippets.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function getCodeSampleLabel(language: Language): OpenAPICodeSample['label
1919
}
2020

2121
// Iterates over the result of `transformSnippetsToCodeSamples` in order to generate a JSON file for the doc to consume.
22-
export async function bundleCodeSamplesForDoc(codeSamples: CodeSamples, clientName: string): Promise<void> {
22+
export function parseCodeSamples(codeSamples: CodeSamples): CodeSamples {
2323
for (const [language, operationWithSamples] of Object.entries(codeSamples)) {
2424
for (const [operation, samples] of Object.entries(operationWithSamples)) {
2525
if (operation === 'import') {
@@ -48,7 +48,7 @@ export async function bundleCodeSamplesForDoc(codeSamples: CodeSamples, clientNa
4848
}
4949
}
5050

51-
await fsp.writeFile(toAbsolutePath(`docs/bundled/${clientName}-snippets.json`), JSON.stringify(codeSamples, null, 2));
51+
return codeSamples;
5252
}
5353

5454
// Reads the generated `docs/snippets/` file for every languages of the given `clientName` and builds an hashmap of snippets per operationId per language.

0 commit comments

Comments
 (0)