Skip to content

Commit f9cec14

Browse files
committed
👌 IMPROVE: Tests
1 parent 3a2f75d commit f9cec14

File tree

9 files changed

+96
-189
lines changed

9 files changed

+96
-189
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ public/dist
1616
/playwright-report/
1717
/blob-report/
1818
/playwright/.cache/
19+
.x*

packages/langbase/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
1818
"type-check": "tsc --noEmit",
1919
"prettier-check": "prettier --check \"./**/*.ts*\"",
20-
"test": "pnpm test:node && pnpm test:edge && pnpm test:ui && pnpm test:e2e",
20+
"test": "pnpm test:node && pnpm test:edge",
21+
"#test": "pnpm test:node && pnpm test:edge && pnpm test:ui && pnpm test:e2e",
2122
"test:edge": "vitest --config vitest.edge.config.js --run",
2223
"test:node": "vitest --config vitest.node.config.js --run",
2324
"test:ui": "pnpm test:ui:react",

packages/langbase/readme.md

Lines changed: 7 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ The AI SDK for building declarative and composable AI-powered LLM products.
44

55
## Documentation
66

7-
Check the [Langbase SDK documentation](https://langbase.com/docs/langbase-sdk/overview) for more details.
7+
> [!NOTE]
8+
> Check the [Langbase SDK documentation](https://langbase.com/docs/langbase-sdk/overview) for more details.
89
910
The following examples are for reference only. Prefer docs for the latest information.
1011

11-
## Getting Started with `langbase` SDK
12+
## Getting started with `langbase` SDK
1213

1314
### Installation
1415

@@ -30,91 +31,10 @@ or
3031
yarn add langbase
3132
```
3233

33-
### Usage
34-
35-
You can [`generateText`](https://langbase.com/docs/langbase-sdk/generate-text) or [`streamText`](https://langbase.com/docs/langbase-sdk/stream-text) based on the type of a pipe.
36-
37-
Check our [SDK documentation](https://langbase.com/docs/langbase-sdk/overview) for more details.
38-
39-
### Example projects
40-
41-
Check the following examples:
42-
43-
- [Node: Generate Text](https://github.com/LangbaseInc/langbase-sdk/blob/main/examples/everything/generate-text.ts)
44-
- [Node: Stream Text](https://github.com/LangbaseInc/langbase-sdk/blob/main/examples/everything/stream-text.ts)
45-
- [Next.js Example](https://github.com/LangbaseInc/langbase-sdk/tree/main/examples/nextjs)
46-
- TypeScript code
47-
- [React component](https://github.com/LangbaseInc/langbase-sdk/tree/main/examples/nextjs/components/langbase) to display the response
48-
- [API Route handlers](https://github.com/LangbaseInc/langbase-sdk/tree/main/examples/nextjs/app/api/langbase/pipe) to send requests to ⌘ Langbase
49-
50-
### Node.js Example Code
51-
52-
53-
## Node.js Examples
54-
55-
### Add a `.env` file with your Pipe API key
56-
57-
```bash
58-
# Add your Pipe API key here.
59-
LANGBASE_PIPE_API_KEY="pipe_12345`"
60-
```
61-
62-
---
63-
64-
### Generate text [`generateText()`](https://langbase.com/docs/langbase-sdk/generate-text)
65-
66-
For more check the API reference of [`generateText()`](https://langbase.com/docs/langbase-sdk/generate-text)
67-
68-
```ts
69-
import 'dotenv/config';
70-
import {Pipe} from 'langbase';
71-
72-
// 1. Initiate the Pipe.
73-
const pipe = new Pipe({
74-
// Make sure you have a .env file with any pipe you wanna use.
75-
// As a demo we're using a pipe that has less wordy responses.
76-
apiKey: process.env.LANGBASE_PIPE_API_KEY!,
77-
});
78-
79-
// 3. Generate the text by asking a question.
80-
const result = await pipe.generateText({
81-
messages: [{role: 'user', content: 'Who is an AI Engineer?'}],
82-
});
83-
84-
// 4. Done: You got the generated completion.
85-
console.log(result.completion);
86-
```
87-
88-
---
89-
90-
### Stream text [`streamText()`](https://langbase.com/docs/langbase-sdk/stream-text)
91-
92-
For more check the API reference of [`streamText()`](https://langbase.com/docs/langbase-sdk/stream-text)
93-
94-
```ts
95-
import 'dotenv/config';
96-
import {Pipe} from 'langbase';
97-
98-
// 1. Initiate the Pipe.
99-
const pipe = new Pipe({
100-
// Make sure you have a .env file with any pipe you wanna use.
101-
// As a demo we're using a pipe that has less wordy responses.
102-
apiKey: process.env.LANGBASE_PIPE_API_KEY!,
103-
});
104-
105-
// 2. Generate a stream by asking a question
106-
const stream = await pipe.streamText({
107-
messages: [{role: 'user', content: 'Who is an AI Engineer?'}],
108-
});
34+
## Documentation
10935

110-
// 3. Print the stream
111-
for await (const chunk of stream) {
112-
// Streaming text part — a single word or several.
113-
const textPart = chunk.choices[0]?.delta?.content || '';
36+
Please read the [SDK documentation](https://langbase.com/docs/langbase-sdk/overview)
11437

115-
// Demo: Print the stream — you can use it however.
116-
process.stdout.write(textPart);
117-
}
118-
```
38+
## Examples
11939

120-
Check out [more examples in the docs](https://langbase.com/docs/langbase-sdk/examples) →
40+
Check out [more examples in the docs](https://langbase.com/docs/langbase-sdk/examples)

packages/langbase/src/pipes/example.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

packages/langbase/src/pipes/pipes.test.ts

Lines changed: 86 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import {beforeEach, describe, expect, it, vi} from 'vitest';
2-
import {Request} from '../common/request';
3-
import {
4-
GenerateNonStreamResponse,
5-
GenerateOptions,
6-
GenerateStreamResponse,
7-
Pipe,
8-
} from './pipes';
2+
import {GenerateOptions, GenerateResponse, Pipe, StreamResponse} from './pipes';
93

104
// Mock the Request class
115
vi.mock('../common/request');
@@ -16,14 +10,15 @@ describe('Pipe', () => {
1610

1711
beforeEach(() => {
1812
pipe = new Pipe({apiKey: mockApiKey});
13+
vi.resetAllMocks();
1914
});
2015

2116
describe('generateText', () => {
22-
it('should call request.post with correct parameters', async () => {
17+
it('should call request.post with correct parameters for non-chat generation', async () => {
2318
const mockOptions: GenerateOptions = {
2419
messages: [{role: 'user', content: 'Hello'}],
2520
};
26-
const mockResponse: GenerateNonStreamResponse = {
21+
const mockResponse: GenerateResponse = {
2722
completion: 'Hello, how can I help you?',
2823
raw: {
2924
id: 'test-id',
@@ -37,6 +32,8 @@ describe('Pipe', () => {
3732
role: 'assistant',
3833
content: 'Hello, how can I help you?',
3934
},
35+
logprobs: null,
36+
finish_reason: 'stop',
4037
},
4138
],
4239
usage: {
@@ -48,36 +45,104 @@ describe('Pipe', () => {
4845
},
4946
};
5047

51-
(Request.prototype.post as any).mockResolvedValue(mockResponse);
48+
const mockPost = vi.fn().mockResolvedValue(mockResponse);
49+
(pipe as any).request = {post: mockPost};
5250

5351
const result = await pipe.generateText(mockOptions);
5452

55-
expect(Request.prototype.post).toHaveBeenCalledWith({
53+
expect(mockPost).toHaveBeenCalledWith({
5654
endpoint: '/beta/generate',
57-
body: mockOptions,
55+
body: {...mockOptions, stream: false},
56+
});
57+
expect(result).toEqual(mockResponse);
58+
});
59+
60+
it('should call request.post with correct parameters for chat generation', async () => {
61+
const mockOptions: GenerateOptions = {
62+
messages: [{role: 'user', content: 'Hello'}],
63+
chat: true,
64+
};
65+
const mockResponse: GenerateResponse = {
66+
completion: 'Hello! How can I assist you today?',
67+
threadId: 'chat-thread-123',
68+
raw: {
69+
id: 'chat-id',
70+
object: 'chat-object',
71+
created: 123456789,
72+
model: 'chat-model',
73+
choices: [
74+
{
75+
index: 0,
76+
message: {
77+
role: 'assistant',
78+
content: 'Hello! How can I assist you today?',
79+
},
80+
logprobs: null,
81+
finish_reason: 'stop',
82+
},
83+
],
84+
usage: {
85+
prompt_tokens: 5,
86+
completion_tokens: 12,
87+
total_tokens: 17,
88+
},
89+
system_fingerprint: null,
90+
},
91+
};
92+
93+
const mockPost = vi.fn().mockResolvedValue(mockResponse);
94+
(pipe as any).request = {post: mockPost};
95+
96+
const result = await pipe.generateText(mockOptions);
97+
98+
expect(mockPost).toHaveBeenCalledWith({
99+
endpoint: '/beta/chat',
100+
body: {...mockOptions, stream: false},
58101
});
59102
expect(result).toEqual(mockResponse);
60103
});
61104
});
62105

63106
describe('streamText', () => {
64-
it('should call request.post with correct parameters and stream option', async () => {
107+
it('should call request.post with correct parameters for non-chat streaming', async () => {
65108
const mockOptions: GenerateOptions = {
66109
messages: [{role: 'user', content: 'Hello'}],
67110
};
68-
const mockStreamResponse: GenerateStreamResponse =
69-
{} as GenerateStreamResponse; // You might need to create a more detailed mock
111+
const mockStreamResponse: StreamResponse = {
112+
stream: {} as any,
113+
threadId: null,
114+
};
70115

71-
(Request.prototype.post as any).mockResolvedValue(
72-
mockStreamResponse,
73-
);
116+
const mockPost = vi.fn().mockResolvedValue(mockStreamResponse);
117+
(pipe as any).request = {post: mockPost};
74118

75119
const result = await pipe.streamText(mockOptions);
76120

77-
expect(Request.prototype.post).toHaveBeenCalledWith({
121+
expect(mockPost).toHaveBeenCalledWith({
78122
endpoint: '/beta/generate',
79-
body: mockOptions,
80-
stream: true,
123+
body: {...mockOptions, stream: true},
124+
});
125+
expect(result).toEqual(mockStreamResponse);
126+
});
127+
128+
it('should call request.post with correct parameters for chat streaming', async () => {
129+
const mockOptions: GenerateOptions = {
130+
messages: [{role: 'user', content: 'Hello'}],
131+
chat: true,
132+
};
133+
const mockStreamResponse: StreamResponse = {
134+
stream: {} as any,
135+
threadId: 'chat-thread-123',
136+
};
137+
138+
const mockPost = vi.fn().mockResolvedValue(mockStreamResponse);
139+
(pipe as any).request = {post: mockPost};
140+
141+
const result = await pipe.streamText(mockOptions);
142+
143+
expect(mockPost).toHaveBeenCalledWith({
144+
endpoint: '/beta/chat',
145+
body: {...mockOptions, stream: true},
81146
});
82147
expect(result).toEqual(mockStreamResponse);
83148
});

packages/langbase/tests/example.spec.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/langbase/tests/example.spec.d.ts.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/langbase/tests/example.spec.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/langbase/tests/example.spec.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)