|
| 1 | +/** |
| 2 | + * Test file for layer analyzer |
| 3 | + * |
| 4 | + * Tests: |
| 5 | + * 1. Detection of missing service requirements |
| 6 | + * 2. Resolution of available layer candidates |
| 7 | + * 3. Transitive dependency resolution (CacheLive depends on ConfigService) |
| 8 | + * 4. Multiple candidates per service for user selection |
| 9 | + */ |
| 10 | +import { Effect, Context, Layer } from "effect"; |
| 11 | + |
| 12 | +// ============================================================================ |
| 13 | +// SERVICE DEFINITIONS |
| 14 | +// ============================================================================ |
| 15 | + |
| 16 | +class DatabaseService extends Context.Tag("DatabaseService")< |
| 17 | + DatabaseService, |
| 18 | + { readonly query: (sql: string) => Effect.Effect<unknown[]> } |
| 19 | +>() {} |
| 20 | + |
| 21 | +class LoggingService extends Context.Tag("LoggingService")< |
| 22 | + LoggingService, |
| 23 | + { readonly log: (msg: string) => Effect.Effect<void> } |
| 24 | +>() {} |
| 25 | + |
| 26 | +class ConfigService extends Context.Tag("ConfigService")< |
| 27 | + ConfigService, |
| 28 | + { readonly get: (key: string) => Effect.Effect<string> } |
| 29 | +>() {} |
| 30 | + |
| 31 | +class CacheService extends Context.Tag("CacheService")< |
| 32 | + CacheService, |
| 33 | + { readonly get: (key: string) => Effect.Effect<string | null> } |
| 34 | +>() {} |
| 35 | + |
| 36 | +// ============================================================================ |
| 37 | +// LAYER IMPLEMENTATIONS |
| 38 | +// ============================================================================ |
| 39 | + |
| 40 | +// Database layers - no dependencies |
| 41 | +const DatabaseLive = Layer.succeed(DatabaseService, { |
| 42 | + query: (sql) => Effect.succeed([{ id: 1, name: "Alice" }]), |
| 43 | +}); |
| 44 | + |
| 45 | +const DatabaseTest = Layer.succeed(DatabaseService, { |
| 46 | + query: (sql) => Effect.succeed([{ id: 999, test: true }]), |
| 47 | +}); |
| 48 | + |
| 49 | +// Logging layers - no dependencies |
| 50 | +const LoggingLive = Layer.succeed(LoggingService, { |
| 51 | + log: (msg) => Effect.log(msg), |
| 52 | +}); |
| 53 | + |
| 54 | +const LoggingTest = Layer.succeed(LoggingService, { |
| 55 | + log: (msg) => Effect.sync(() => console.log("[TEST]", msg)), |
| 56 | +}); |
| 57 | + |
| 58 | +// Config layers - no dependencies |
| 59 | +const ConfigLive = Layer.succeed(ConfigService, { |
| 60 | + get: (key) => Effect.succeed(process.env[key] ?? ""), |
| 61 | +}); |
| 62 | + |
| 63 | +const ConfigTest = Layer.succeed(ConfigService, { |
| 64 | + get: (key) => Effect.succeed("test-value"), |
| 65 | +}); |
| 66 | + |
| 67 | +// Cache layers - DEPENDS ON ConfigService (tests transitive resolution) |
| 68 | +const CacheLive = Layer.effect( |
| 69 | + CacheService, |
| 70 | + Effect.gen(function* () { |
| 71 | + const config = yield* ConfigService; |
| 72 | + const ttl = yield* config.get("CACHE_TTL"); |
| 73 | + return { |
| 74 | + get: (key: string) => Effect.succeed(null as string | null), |
| 75 | + }; |
| 76 | + }), |
| 77 | +); |
| 78 | + |
| 79 | +const CacheTest = Layer.effect( |
| 80 | + CacheService, |
| 81 | + Effect.gen(function* () { |
| 82 | + const config = yield* ConfigService; |
| 83 | + return { |
| 84 | + get: (key: string) => Effect.succeed(("cached-" + key) as string | null), |
| 85 | + }; |
| 86 | + }), |
| 87 | +); |
| 88 | + |
| 89 | +// ============================================================================ |
| 90 | +// PROGRAM THAT REQUIRES SERVICES |
| 91 | +// ============================================================================ |
| 92 | + |
| 93 | +const myProgram = Effect.gen(function* () { |
| 94 | + const db = yield* DatabaseService; |
| 95 | + const logger = yield* LoggingService; |
| 96 | + const cache = yield* CacheService; |
| 97 | + |
| 98 | + yield* logger.log("Starting query"); |
| 99 | + |
| 100 | + // Check cache first |
| 101 | + const cached = yield* cache.get("users"); |
| 102 | + if (cached) { |
| 103 | + yield* logger.log("Cache hit"); |
| 104 | + return JSON.parse(cached); |
| 105 | + } |
| 106 | + |
| 107 | + // Query database |
| 108 | + const results = yield* db.query("SELECT * FROM users"); |
| 109 | + yield* logger.log(`Found ${results.length} users`); |
| 110 | + |
| 111 | + return results; |
| 112 | +}); |
| 113 | + |
| 114 | +// ============================================================================ |
| 115 | +// This line should trigger the layer analyzer |
| 116 | +// The program requires: DatabaseService, LoggingService, CacheService |
| 117 | +// CacheLive transitively requires: ConfigService |
| 118 | +// ============================================================================ |
| 119 | + |
| 120 | +// This will fail type checking - no layers provided! |
| 121 | +const runnable = Effect.runPromise(myProgram); |
0 commit comments