Chapter 6b: Generating a Bedtime Story with ChatGPT
Overview
Coming soon…
Training
Pre-requisites
Concepts Covered in This Chapter
Tools Used in This Chapter
Commands Used in This Chapter
Assertions
assert.is.true
- Validates that a condition is true.
assert.is.equal
- Checks if two values are equal.
assert.is.deep.equal
- Confirms deep equality between objects, including nested structures.
assert.is.truthy
- Ensures that a value is not null, undefined, or false.
assert.throws
- Verifies that a function throws an error.
assert.does.throw.async
- Asserts that an async method throws an expected error.
assert.error.code
- Validates that a thrown error has the correct error code.
assert.did.not.call.chat.completions.create
- Ensures OpenAI’s
chat.completions.create
method was not called.
- Ensures OpenAI’s
assert.create.completion.called
- Confirms
chat.completions.create
was called.
- Confirms
assert.chat.completion.call.model
- Validates the model name in a ChatGPT request.
assert.first.completion.message.equals
- Checks if the first message sent to ChatGPT matches expectations.
assert.second.message.equals
- Checks if the second message sent to ChatGPT was properly structured.
assert.is.equal.actual.body
- Confirms that the actual body matches the expected body after creation.
assert.is.truthy.story
- Confirms that a story record was created and is not null or undefined.
Event Handling & Mocking
eventFaker.fake.getStory
- Fakes the
get.story
event in tests.
- Fakes the
generate.id
- Generates unique values for mocking input or API keys.
spy.OpenAI
- Spy class for intercepting OpenAI calls.
mock.OpenAI
- Mock implementation of OpenAI client.
spy.OpenAI.instance
- Singleton instance for asserting mock client behavior.
mock.story.generator.errorMessage
- Used to trigger and test error conditions from within mocks.
ChatGPT/OpenAI Integration
OpenAI.chat.completions.create
- Sends chat messages to OpenAI and gets a completion.
chat.completions.create
- Alias used across assertions and mocks for OpenAI interaction.
model: "gpt-4-0125-preview"
- Chat model used in the tests.
messages: [{ role, content }]
- Structure of prompts sent to OpenAI’s chat completions API.
create.options.model
- Part of assert chain confirming the model name used.
create.options.messages
- Asserts that the prompt messages were formed correctly.
Story Generator Methods
story.generator.builder
- Factory method that sets up a generator with mock dependencies.
story.generator.generate.story
- Main function for story generation.
story.generator.generate.second.message
- Builds the second prompt message for ChatGPT.
this.chat.GPT.response.text
- Stores the fake chat response for reuse in test assertions.
await.this.generate.story.with.all.members
- Utility to generate a story with complete inputs.
this.get.first.family.member
- Helper method to fetch seed data in tests.
await.this.stories.create.one
- Creates a new story record in the store.
await.stories.find.one
- Finds and retrieves a story by ID from the store.
Testing Utilities
test.protected.static.async
- Defines reusable async test cases.
before.each
- Prepares a fresh context for each test run.
before.all
- Runs setup logic once before all test cases.
await.this.load()
- Initializes SkillView or test context manually.
testRouter.setShouldThrowWhenRedirectingToBadSkillViewController(false)
- Disables redirect validation during external redirect tests.
assert.does.throw.async(this.bootSkill)
- Ensures that booting the skill fails as expected if misconfigured.
Schema & Error Handling
schema.error.code.invalid.parameters
- Error thrown for unexpected or malformed input.
schema.error.code.missing.parameters
- Error thrown when required parameters are not provided.
throw.invalid.family.member.ids
- Throws when one or more family member IDs are invalid.
throw.invalid.family.id
- Throws when the specified family ID doesn’t exist.
throw.missing.openAI.key
- Throws when OpenAI key is missing in environment variables.
schema.fields.story.body.source
- Field declarations used in schema creation.
sync.schemas()
- Applies updated schemas to the store system.
Form & Skill Context Management
generate.prompt({ family, familyMembers, storyElements, currentChallenge })
- Passes structured options to generate prompt content.
spy.story.generator.implements.type
- Used to typecast and verify properties on the generator.
extends.abstract.8bit.test
- Class extension for standardized test configuration.
merge.skill.context.types
- Used to patch typing for extended Skill context in TypeScript.