Skip to content

Commit 2026260

Browse files
committed
Wrap into one test (required)
1 parent 9774e2f commit 2026260

File tree

2 files changed

+112
-108
lines changed

2 files changed

+112
-108
lines changed

src/client/index.test.ts

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,67 +6,69 @@ import { RequestSchema, NotificationSchema, ResultSchema } from "../types.js";
66
/*
77
Test that custom request/notification/result schemas can be used with the Client class.
88
*/
9-
const GetWeatherRequestSchema = RequestSchema.extend({
10-
method: z.literal("weather/get"),
11-
params: z.object({
12-
city: z.string(),
13-
}),
14-
});
9+
test("should typecheck", () => {
10+
const GetWeatherRequestSchema = RequestSchema.extend({
11+
method: z.literal("weather/get"),
12+
params: z.object({
13+
city: z.string(),
14+
}),
15+
});
1516

16-
const GetForecastRequestSchema = RequestSchema.extend({
17-
method: z.literal("weather/forecast"),
18-
params: z.object({
19-
city: z.string(),
20-
days: z.number(),
21-
}),
22-
});
17+
const GetForecastRequestSchema = RequestSchema.extend({
18+
method: z.literal("weather/forecast"),
19+
params: z.object({
20+
city: z.string(),
21+
days: z.number(),
22+
}),
23+
});
2324

24-
const WeatherForecastNotificationSchema = NotificationSchema.extend({
25-
method: z.literal("weather/alert"),
26-
params: z.object({
27-
severity: z.enum(["warning", "watch"]),
28-
message: z.string(),
29-
}),
30-
});
25+
const WeatherForecastNotificationSchema = NotificationSchema.extend({
26+
method: z.literal("weather/alert"),
27+
params: z.object({
28+
severity: z.enum(["warning", "watch"]),
29+
message: z.string(),
30+
}),
31+
});
3132

32-
const WeatherRequestSchema = GetWeatherRequestSchema.or(
33-
GetForecastRequestSchema,
34-
);
35-
const WeatherNotificationSchema = WeatherForecastNotificationSchema;
36-
const WeatherResultSchema = ResultSchema.extend({
37-
temperature: z.number(),
38-
conditions: z.string(),
39-
});
33+
const WeatherRequestSchema = GetWeatherRequestSchema.or(
34+
GetForecastRequestSchema,
35+
);
36+
const WeatherNotificationSchema = WeatherForecastNotificationSchema;
37+
const WeatherResultSchema = ResultSchema.extend({
38+
temperature: z.number(),
39+
conditions: z.string(),
40+
});
4041

41-
type WeatherRequest = z.infer<typeof WeatherRequestSchema>;
42-
type WeatherNotification = z.infer<typeof WeatherNotificationSchema>;
43-
type WeatherResult = z.infer<typeof WeatherResultSchema>;
42+
type WeatherRequest = z.infer<typeof WeatherRequestSchema>;
43+
type WeatherNotification = z.infer<typeof WeatherNotificationSchema>;
44+
type WeatherResult = z.infer<typeof WeatherResultSchema>;
4445

45-
// Create a typed Client for weather data
46-
const weatherClient = new Client<
47-
WeatherRequest,
48-
WeatherNotification,
49-
WeatherResult
50-
>({
51-
name: "WeatherClient",
52-
version: "1.0.0",
53-
});
46+
// Create a typed Client for weather data
47+
const weatherClient = new Client<
48+
WeatherRequest,
49+
WeatherNotification,
50+
WeatherResult
51+
>({
52+
name: "WeatherClient",
53+
version: "1.0.0",
54+
});
5455

55-
// Typecheck that only valid weather requests/notifications/results are allowed
56-
weatherClient.request(
57-
{
58-
method: "weather/get",
59-
params: {
60-
city: "Seattle",
56+
// Typecheck that only valid weather requests/notifications/results are allowed
57+
weatherClient.request(
58+
{
59+
method: "weather/get",
60+
params: {
61+
city: "Seattle",
62+
},
6163
},
62-
},
63-
WeatherResultSchema,
64-
);
64+
WeatherResultSchema,
65+
);
6566

66-
weatherClient.notification({
67-
method: "weather/alert",
68-
params: {
69-
severity: "warning",
70-
message: "Storm approaching",
71-
},
67+
weatherClient.notification({
68+
method: "weather/alert",
69+
params: {
70+
severity: "warning",
71+
message: "Storm approaching",
72+
},
73+
});
7274
});

src/server/index.test.ts

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,65 @@ import { RequestSchema, NotificationSchema, ResultSchema } from "../types.js";
66
/*
77
Test that custom request/notification/result schemas can be used with the Server class.
88
*/
9-
const GetWeatherRequestSchema = RequestSchema.extend({
10-
method: z.literal("weather/get"),
11-
params: z.object({
12-
city: z.string(),
13-
}),
14-
});
9+
test("should typecheck", () => {
10+
const GetWeatherRequestSchema = RequestSchema.extend({
11+
method: z.literal("weather/get"),
12+
params: z.object({
13+
city: z.string(),
14+
}),
15+
});
1516

16-
const GetForecastRequestSchema = RequestSchema.extend({
17-
method: z.literal("weather/forecast"),
18-
params: z.object({
19-
city: z.string(),
20-
days: z.number(),
21-
}),
22-
});
17+
const GetForecastRequestSchema = RequestSchema.extend({
18+
method: z.literal("weather/forecast"),
19+
params: z.object({
20+
city: z.string(),
21+
days: z.number(),
22+
}),
23+
});
2324

24-
const WeatherForecastNotificationSchema = NotificationSchema.extend({
25-
method: z.literal("weather/alert"),
26-
params: z.object({
27-
severity: z.enum(["warning", "watch"]),
28-
message: z.string(),
29-
}),
30-
});
25+
const WeatherForecastNotificationSchema = NotificationSchema.extend({
26+
method: z.literal("weather/alert"),
27+
params: z.object({
28+
severity: z.enum(["warning", "watch"]),
29+
message: z.string(),
30+
}),
31+
});
3132

32-
const WeatherRequestSchema = GetWeatherRequestSchema.or(
33-
GetForecastRequestSchema,
34-
);
35-
const WeatherNotificationSchema = WeatherForecastNotificationSchema;
36-
const WeatherResultSchema = ResultSchema.extend({
37-
temperature: z.number(),
38-
conditions: z.string(),
39-
});
33+
const WeatherRequestSchema = GetWeatherRequestSchema.or(
34+
GetForecastRequestSchema,
35+
);
36+
const WeatherNotificationSchema = WeatherForecastNotificationSchema;
37+
const WeatherResultSchema = ResultSchema.extend({
38+
temperature: z.number(),
39+
conditions: z.string(),
40+
});
4041

41-
type WeatherRequest = z.infer<typeof WeatherRequestSchema>;
42-
type WeatherNotification = z.infer<typeof WeatherNotificationSchema>;
43-
type WeatherResult = z.infer<typeof WeatherResultSchema>;
42+
type WeatherRequest = z.infer<typeof WeatherRequestSchema>;
43+
type WeatherNotification = z.infer<typeof WeatherNotificationSchema>;
44+
type WeatherResult = z.infer<typeof WeatherResultSchema>;
4445

45-
// Create a typed Server for weather data
46-
const weatherServer = new Server<
47-
WeatherRequest,
48-
WeatherNotification,
49-
WeatherResult
50-
>({
51-
name: "WeatherServer",
52-
version: "1.0.0",
53-
});
46+
// Create a typed Server for weather data
47+
const weatherServer = new Server<
48+
WeatherRequest,
49+
WeatherNotification,
50+
WeatherResult
51+
>({
52+
name: "WeatherServer",
53+
version: "1.0.0",
54+
});
5455

55-
// Typecheck that only valid weather requests/notifications/results are allowed
56-
weatherServer.setRequestHandler(GetWeatherRequestSchema, (request) => {
57-
return {
58-
temperature: 72,
59-
conditions: "sunny",
60-
};
61-
});
56+
// Typecheck that only valid weather requests/notifications/results are allowed
57+
weatherServer.setRequestHandler(GetWeatherRequestSchema, (request) => {
58+
return {
59+
temperature: 72,
60+
conditions: "sunny",
61+
};
62+
});
6263

63-
weatherServer.setNotificationHandler(
64-
WeatherForecastNotificationSchema,
65-
(notification) => {
66-
console.log(`Weather alert: ${notification.params.message}`);
67-
},
68-
);
64+
weatherServer.setNotificationHandler(
65+
WeatherForecastNotificationSchema,
66+
(notification) => {
67+
console.log(`Weather alert: ${notification.params.message}`);
68+
},
69+
);
70+
});

0 commit comments

Comments
 (0)