Skip to content

Commit

Permalink
Merge branch 'main' into specs_client-initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft committed Nov 11, 2024
2 parents 77e8867 + 778bacc commit 559dd48
Show file tree
Hide file tree
Showing 117 changed files with 7,536 additions and 4,543 deletions.
12 changes: 12 additions & 0 deletions packages/cadl-ranch-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# @azure-tools/cadl-ranch-api

## 0.5.0

### Minor Changes

- 46e3022: Fix mockapi in authentication scenarios.

## 0.4.7

### Patch Changes

- 352934c: Backport the changes from typespec repository to migrate the scenarios to new model

## 0.4.6

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/cadl-ranch-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure-tools/cadl-ranch-api",
"version": "0.4.6",
"version": "0.5.0",
"description": "Cadl Ranch set of api to implement mock api",
"main": "dist/index.js",
"type": "module",
Expand All @@ -24,9 +24,9 @@
},
"homepage": "https://github.com/Azure/cadl-ranch#readme",
"dependencies": {
"body-parser": "^1.20.2",
"body-parser": "^1.20.3",
"deep-equal": "^2.2.0",
"express": "^4.19.2",
"express": "^4.20.0",
"express-promise-router": "^4.1.1",
"glob": "^11.0.0",
"morgan": "^1.10.0",
Expand Down
33 changes: 30 additions & 3 deletions packages/cadl-ranch-api/src/scenarios.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { KeyedMockApi, MockApi, PassByKeyScenario, PassOnCodeScenario, PassOnSuccessScenario } from "./types.js";
import {
KeyedMockApi,
KeyedMockApiDefinition,
MockApi,
MockApiDefinition,
PassByKeyScenario,
PassByServiceKeyScenario,
PassOnCodeScenario,
PassOnSuccessScenario,
} from "./types.js";

/**
* Specify that the scenario should be a `pass` if all the endpoints are called and the API response with 2xx exit code.
* @param apis Endpoint or List of endpoints for this scenario
*/
export function passOnSuccess(apis: MockApi | readonly MockApi[]): PassOnSuccessScenario {
export function passOnSuccess(
apis: MockApi | readonly MockApi[] | MockApiDefinition | readonly MockApiDefinition[],
): PassOnSuccessScenario {
return {
passCondition: "response-success",
apis: Array.isArray(apis) ? apis : [apis],
Expand All @@ -15,7 +26,7 @@ export function passOnSuccess(apis: MockApi | readonly MockApi[]): PassOnSuccess
* @param code Status code all endpoint should return
* @param apis Endpoint or List of endpoints for this scenario
*/
export function passOnCode(code: number, apis: MockApi | readonly MockApi[]): PassOnCodeScenario {
export function passOnCode(code: number, apis: MockApi | readonly MockApi[] | MockApiDefinition): PassOnCodeScenario {
return {
passCondition: "status-code",
code,
Expand All @@ -42,3 +53,19 @@ export function withKeys<const K extends string>(keys: K[]): WithKeysScenarioExp
},
};
}

export interface WithServiceKeysScenarioExpect<K extends string> {
pass(api: KeyedMockApiDefinition<K> | KeyedMockApiDefinition<K>[]): PassByServiceKeyScenario<K>;
}

export function withServiceKeys<const K extends string>(keys: K[]): WithServiceKeysScenarioExpect<K> {
return {
pass: (api: KeyedMockApiDefinition<K> | KeyedMockApiDefinition<K>[]) => {
return {
passCondition: "by-key",
keys,
apis: Array.isArray(api) ? api : [api],
};
},
};
}
44 changes: 41 additions & 3 deletions packages/cadl-ranch-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,28 @@ export type ScenarioPassCondition = "response-success" | "status-code";

export interface PassOnSuccessScenario {
passCondition: "response-success";
apis: MockApi[];
apis: MockApi[] | MockApiDefinition[];
}

export interface PassOnCodeScenario {
passCondition: "status-code";
code: number;
apis: MockApi[];
apis: MockApi[] | MockApiDefinition[];
}

export interface PassByKeyScenario<K extends string = string> {
passCondition: "by-key";
keys: K[];
apis: KeyedMockApi<K>[];
}

export type ScenarioMockApi = PassOnSuccessScenario | PassOnCodeScenario | PassByKeyScenario;
export interface PassByServiceKeyScenario<K extends string = string> {
passCondition: "by-key";
keys: K[];
apis: KeyedMockApiDefinition<K>[];
}

export type ScenarioMockApi = PassOnSuccessScenario | PassOnCodeScenario | PassByKeyScenario | PassByServiceKeyScenario;
export type MockRequestHandler = SimpleMockRequestHandler | KeyedMockRequestHandler;
export type SimpleMockRequestHandler = (req: MockRequest) => MockResponse | Promise<MockResponse>;
export type KeyedMockRequestHandler<T extends string = string> = (
Expand All @@ -49,12 +56,43 @@ export interface MockApi {
method: HttpMethod;
uri: string;
handler: MockRequestHandler;
kind: "MockApi";
}

export interface MockApiDefinition {
uri: string;
method: HttpMethod;
request: ServiceRequest;
response: MockResponse;
handler?: MockRequestHandler;
kind: "MockApiDefinition";
}

export interface ServiceRequest {
body?: any;
status?: number;
/**
* Query parameters to match to the request.
*/
params?: Record<string, unknown>;
headers?: Record<string, unknown>;
files?: ServiceRequestFile[];
}

export interface ServiceRequestFile {
fieldname: string;
originalname: string;
buffer: Buffer;
mimetype: string;
}

export const Fail = Symbol.for("Fail");
export interface KeyedMockApi<K extends string> extends MockApi {
handler: KeyedMockRequestHandler<K>;
}
export interface KeyedMockApiDefinition<K extends string> extends MockApiDefinition {
handler: KeyedMockRequestHandler<K>;
}

export interface MockResponse {
status: number;
Expand Down
6 changes: 6 additions & 0 deletions packages/cadl-ranch-coverage-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @azure-tools/cadl-ranch-api

## 0.9.0

### Minor Changes

- afce8be: Add dashboard for Azure Resource Manager.

## 0.8.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/cadl-ranch-coverage-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure-tools/cadl-ranch-coverage-sdk",
"version": "0.8.4",
"version": "0.9.0",
"description": "Cadl Ranch utility to manage the reported coverage",
"main": "dist/index.js",
"type": "module",
Expand Down
5 changes: 4 additions & 1 deletion packages/cadl-ranch-coverage-sdk/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export const GeneratorMode: string[] = ["azure", "standard"];
export enum GeneratorMode {
azure = "azure",
standard = "standard",
}
17 changes: 17 additions & 0 deletions packages/cadl-ranch-dashboard/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# @azure-tools/cadl-ranch-dashboard

## 0.11.0

### Minor Changes

- 9d65984: Added number of scenarios under each row

## 0.9.0

### Minor Changes

- afce8be: Add dashboard for Azure Resource Manager.

### Patch Changes

- Updated dependencies [afce8be]
- @azure-tools/cadl-ranch-coverage-sdk@0.9.0

## 0.8.6

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/cadl-ranch-dashboard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
## Dev

```bash
npm start
npm run start
# or
npm dev
npm run dev
```

## Show the test generator

Add `?showtest=true` query parameter to url https://localhost:5173/?showtest=true
Add `?showtest=true` query parameter to url http://localhost:5173/?showtest=true
4 changes: 2 additions & 2 deletions packages/cadl-ranch-dashboard/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@azure-tools/cadl-ranch-dashboard",
"private": true,
"version": "0.8.6",
"version": "0.11.0",
"description": "Cadl Ranch Dashboard website",
"main": "dist/index.js",
"type": "module",
Expand Down Expand Up @@ -42,6 +42,6 @@
"rimraf": "^6.0.1",
"rollup-plugin-visualizer": "^5.9.0",
"typescript": "~5.5.4",
"vite": "^5.4.0"
"vite": "^5.4.6"
}
}
54 changes: 32 additions & 22 deletions packages/cadl-ranch-dashboard/src/apis.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
CadlRanchCoverageClient,
GeneratorMode,
ResolvedCoverageReport,
ScenarioData,
ScenarioManifest,
GeneratorMode,
} from "@azure-tools/cadl-ranch-coverage-sdk";
import { TableConfig, TableConfigs } from "./constants.js";

const storageAccountName = "azuresdkcadlranch";

Expand Down Expand Up @@ -35,7 +37,7 @@ const generatorNames: GeneratorNames[] = [
export interface CoverageSummary {
manifest: ScenarioManifest;
generatorReports: Record<GeneratorNames, ResolvedCoverageReport | undefined>;
mode: string;
tableConfig: TableConfig;
}

let client: CadlRanchCoverageClient | undefined;
Expand All @@ -57,34 +59,42 @@ export async function getCoverageSummaries(): Promise<CoverageSummary[]> {
coverageClient.manifest.get(),
loadReports(coverageClient, generatorNames),
]);
return GeneratorMode.map((mode) => ({
manifest,
generatorReports: generatorReports[mode],
mode,
}));
return TableConfigs.map((config) => {
const copiedManifest: ScenarioManifest = JSON.parse(JSON.stringify(manifest));
copiedManifest.scenarios = manifest.scenarios.filter((scenarioData: ScenarioData) =>
config.scenarioFilter ? config.scenarioFilter(scenarioData.name) : () => true,
);
return {
manifest: copiedManifest,
generatorReports: generatorReports[config.mode],
tableConfig: config,
};
});
}

async function loadReports(
coverageClient: CadlRanchCoverageClient,
generatorNames: GeneratorNames[],
): Promise<{ [mode: string]: Record<GeneratorNames, ResolvedCoverageReport | undefined> }> {
const results = await Promise.all(
GeneratorMode.map(async (mode): Promise<[string, Record<GeneratorNames, ResolvedCoverageReport | undefined>]> => {
const items = await Promise.all(
generatorNames.map(async (generatorName): Promise<[GeneratorNames, ResolvedCoverageReport | undefined]> => {
try {
const report = await coverageClient.coverage.getLatestCoverageFor(generatorName, mode);
return [generatorName, report];
} catch (error) {
// eslint-disable-next-line no-console
console.error("Error resolving report", error);
Object.keys(GeneratorMode).map(
async (mode): Promise<[string, Record<GeneratorNames, ResolvedCoverageReport | undefined>]> => {
const items = await Promise.all(
generatorNames.map(async (generatorName): Promise<[GeneratorNames, ResolvedCoverageReport | undefined]> => {
try {
const report = await coverageClient.coverage.getLatestCoverageFor(generatorName, mode);
return [generatorName, report];
} catch (error) {
// eslint-disable-next-line no-console
console.error("Error resolving report", error);

return [generatorName, undefined];
}
}),
);
return [mode, Object.fromEntries(items) as any];
}),
return [generatorName, undefined];
}
}),
);
return [mode, Object.fromEntries(items) as any];
},
),
);

return results.reduce<{ [mode: string]: Record<GeneratorNames, ResolvedCoverageReport | undefined> }>(
Expand Down
24 changes: 22 additions & 2 deletions packages/cadl-ranch-dashboard/src/components/dashboard-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,15 @@ const DashboardHeaderRow: FunctionComponent<DashboardHeaderRowProps> = ({ covera
}
return [language, getCompletedRatio(coverageSummary.manifest.scenarios, report), report];
});
let tableHeader;
if (coverageSummary.tableConfig.name) {
tableHeader = <th>Scenario name ({coverageSummary.tableConfig.name})</th>;
} else {
tableHeader = <th>Scenario name ({coverageSummary.tableConfig.mode})</th>;
}
return (
<tr>
<th>Scenario name (mode: {coverageSummary.mode})</th>
{tableHeader}
{data.map(([lang, status, report]) => (
<GeneratorHeaderCell key={lang} status={status} report={report} language={lang} />
))}
Expand Down Expand Up @@ -286,5 +292,19 @@ function createTree(manifest: ScenarioManifest): ManifestTreeNode {

current.scenario = scenario;
}
return root;

return cutTillMultipleChildren(root);
}

function cutTillMultipleChildren(node: ManifestTreeNode): ManifestTreeNode {
let newRoot: ManifestTreeNode = node;
while (newRoot.children) {
if (Object.keys(newRoot.children).length === 1) {
newRoot = Object.values(newRoot.children)[0];
} else {
break;
}
}

return newRoot;
}
Loading

0 comments on commit 559dd48

Please sign in to comment.