Skip to content

Commit 984985c

Browse files
committed
docs(vcr): improve VCR documentation with @VCRModel annotation, env var override
- Updated Basic Usage section to show declarative @VCRModel annotation approach - Added VCR_MODE environment variable override documentation - Fixed outdated references (VCRCassetteStore now implemented, not 'coming soon') - Changed -Dvcr.mode system property reference to VCR_MODE env var - Added IMPORTANT note about model initialization timing requirement
1 parent 87668c1 commit 984985c

File tree

1 file changed

+60
-6
lines changed

1 file changed

+60
-6
lines changed

docs/content/modules/ROOT/pages/vcr-testing.adoc

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,63 @@ testImplementation 'org.testcontainers:junit-jupiter:1.19.7'
5656

5757
=== Basic Usage
5858

59-
Annotate your test class with `@VCRTest` to enable VCR functionality:
59+
The recommended approach uses `@VCRTest` on the test class and `@VCRModel` on model fields for automatic wrapping:
6060

6161
[source,java]
6262
----
6363
import com.redis.vl.test.vcr.VCRTest;
6464
import com.redis.vl.test.vcr.VCRMode;
65+
import com.redis.vl.test.vcr.VCRModel;
66+
import dev.langchain4j.model.embedding.EmbeddingModel;
67+
import dev.langchain4j.model.chat.ChatLanguageModel;
6568
import org.junit.jupiter.api.Test;
6669
6770
@VCRTest(mode = VCRMode.PLAYBACK_OR_RECORD)
6871
public class MyLLMTest {
6972
73+
// Models are automatically wrapped by VCR - initialize at field declaration
74+
@VCRModel(modelName = "text-embedding-3-small")
75+
private EmbeddingModel embeddingModel = createEmbeddingModel();
76+
77+
@VCRModel
78+
private ChatLanguageModel chatModel = createChatModel();
79+
7080
@Test
71-
void testLLMResponse() {
81+
void testEmbedding() {
7282
// First run: Records API response to Redis
7383
// Subsequent runs: Replays from Redis cassette
74-
String response = myLLMService.generate("What is Redis?");
84+
Response<Embedding> response = embeddingModel.embed("What is Redis?");
85+
assertNotNull(response.content());
86+
}
87+
88+
@Test
89+
void testChat() {
90+
String response = chatModel.generate("Explain Redis in one sentence.");
7591
assertNotNull(response);
7692
}
93+
94+
private static EmbeddingModel createEmbeddingModel() {
95+
String key = System.getenv("OPENAI_API_KEY");
96+
if (key == null) key = "vcr-playback-mode"; // Dummy key for playback
97+
return OpenAiEmbeddingModel.builder()
98+
.apiKey(key)
99+
.modelName("text-embedding-3-small")
100+
.build();
101+
}
102+
103+
private static ChatLanguageModel createChatModel() {
104+
String key = System.getenv("OPENAI_API_KEY");
105+
if (key == null) key = "vcr-playback-mode";
106+
return OpenAiChatModel.builder()
107+
.apiKey(key)
108+
.modelName("gpt-4o-mini")
109+
.build();
110+
}
77111
}
78112
----
79113

114+
IMPORTANT: Models must be initialized at field declaration time, not in `@BeforeEach`. The VCR extension wraps `@VCRModel` fields before `@BeforeEach` methods run.
115+
80116
== VCR Modes
81117

82118
The VCR system supports six modes to control recording and playback behavior:
@@ -110,6 +146,24 @@ The VCR system supports six modes to control recording and playback behavior:
110146
* **CI/CD**: Use `PLAYBACK` to ensure tests are deterministic
111147
* **Initial setup**: Use `RECORD` to capture all cassettes
112148

149+
=== Environment Variable Override
150+
151+
Override the VCR mode at runtime using the `VCR_MODE` environment variable. This takes precedence over the annotation mode:
152+
153+
[source,bash]
154+
----
155+
# Force RECORD mode to capture new cassettes
156+
VCR_MODE=RECORD OPENAI_API_KEY=your-key ./gradlew test
157+
158+
# Force PLAYBACK mode (no API key required)
159+
VCR_MODE=PLAYBACK ./gradlew test
160+
161+
# Force OFF mode to bypass VCR
162+
VCR_MODE=OFF OPENAI_API_KEY=your-key ./gradlew test
163+
----
164+
165+
Valid values: `PLAYBACK`, `PLAYBACK_OR_RECORD`, `RECORD`, `RECORD_NEW`, `RECORD_FAILED`, `OFF`
166+
113167
== Configuration
114168

115169
=== Data Directory
@@ -215,7 +269,7 @@ The VCR system consists of several components:
215269
* **VCRExtension** - JUnit 5 extension that manages the VCR lifecycle
216270
* **VCRContext** - Manages Redis container, call counters, and statistics
217271
* **VCRRegistry** - Tracks recording status for each test
218-
* **VCRCassetteStore** - Stores/retrieves cassettes in Redis (coming soon)
272+
* **VCRCassetteStore** - Stores/retrieves cassettes in Redis using JSON format
219273

220274
=== Cassette Key Format
221275

@@ -266,12 +320,12 @@ public class CITest {
266320

267321
=== Updating Cassettes
268322

269-
When API responses change, re-record cassettes:
323+
When API responses change, re-record cassettes using the `VCR_MODE` environment variable:
270324

271325
[source,bash]
272326
----
273327
# Run tests in RECORD mode to update all cassettes
274-
./gradlew test -Dvcr.mode=RECORD
328+
VCR_MODE=RECORD ./gradlew test
275329
----
276330

277331
=== Sensitive Data

0 commit comments

Comments
 (0)