Reader Pipeline

advanced8 of 8

Learn about reader pipeline in fp-ts reader

Code Editor

08-reader-pipeline.exercise.ts

💻
Loading editor...
Preparing Monaco Editor with TypeScript support

Test Results

Requirements

describe('Reader pipeline', () => {
  const logs: string[] = []
  const cacheStore = new Map<string, string>()

  const deps: AppDependencies = {
    logger: {
      log: (msg) => logs.push(msg),
    },
    cache: {
      get: (key) => cacheStore.get(key),
      set: (key, value) => cacheStore.set(key, value),
    },
    httpClient: {
      fetch: async (url) => `data from ${url}`,
    },
    baseUrl: 'https://api.example.com',
  }

  beforeEach(() => {
    logs.length = 0
    cacheStore.clear()
  })

  it('logs a message', () => {
    logMessage('test message')(deps)
    expect(logs).toContain('test message')
  })

  it('gets cached value', () => {
    cacheStore.set('key1', 'value1')
    const result = getCachedValue('key1')(deps)
    expect(result).toBe('value1')
  })

  it('sets cached value', () => {
    setCachedValue('key2', 'value2')(deps)
    expect(cacheStore.get('key2')).toBe('value2')
  })

  it('builds URL', () => {
    const result = buildUrl('users')(deps)
    expect(result).toBe('https://api.example.com/users')
  })

  it('fetches with cache miss', () => {
    const result = fetchWithCache('users')(deps)
    expect(logs).toContain('Fetching users')
    expect(logs).toContain('Cache miss')
    expect(result).toContain('api.example.com/users')
    expect(cacheStore.get('users')).toBeDefined()
  })

  it('fetches with cache hit', () => {
    cacheStore.set('posts', 'cached posts data')
    const result = fetchWithCache('posts')(deps)
    expect(logs).toContain('Fetching posts')
    expect(logs).toContain('Cache hit')
    expect(result).toBe('cached posts data')
  })
})
🧪

Ready to Test?

Click "Run Tests" to see how your code performs

Pro Tips

💡 Stuck? Here's what to try:

  • • Read the comments in the code carefully
  • • Run tests frequently to get feedback
  • • Check the fp-ts documentation
  • • Use the solution if you need help

🚀 Learning Approach:

  • • Focus on understanding, not just solving
  • • Experiment with different approaches
  • • Think about real-world applications
  • • Build on previous exercises