|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "bun:test" |
| 2 | +import { initializeMcbFromConfig } from "./config-gate" |
| 3 | +import { getMcbAvailability, resetMcbAvailability } from "./availability" |
| 4 | +import type { McbConfig } from "../../config/schema/mcb" |
| 5 | + |
| 6 | +describe("mcb-integration/config-gate", () => { |
| 7 | + beforeEach(() => { |
| 8 | + resetMcbAvailability() |
| 9 | + }) |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + resetMcbAvailability() |
| 13 | + }) |
| 14 | + |
| 15 | + //#given no mcb config (undefined) |
| 16 | + //#when initializeMcbFromConfig is called |
| 17 | + //#then mcb is completely unavailable and locked |
| 18 | + it("disables MCB when config is missing", () => { |
| 19 | + initializeMcbFromConfig(undefined) |
| 20 | + |
| 21 | + const status = getMcbAvailability() |
| 22 | + expect(status.available).toBe(false) |
| 23 | + |
| 24 | + // Verify it's locked (subsequent calls don't reset it) |
| 25 | + // We can't directly check 'locked' state, but we can check if it remains unavailable |
| 26 | + // even if we try to access it again |
| 27 | + const status2 = getMcbAvailability() |
| 28 | + expect(status2.available).toBe(false) |
| 29 | + }) |
| 30 | + |
| 31 | + //#given mcb config with enabled: false |
| 32 | + //#when initializeMcbFromConfig is called |
| 33 | + //#then mcb is completely unavailable and locked |
| 34 | + it("disables MCB when enabled is false", () => { |
| 35 | + const config: McbConfig = { |
| 36 | + enabled: false, |
| 37 | + } |
| 38 | + initializeMcbFromConfig(config) |
| 39 | + |
| 40 | + const status = getMcbAvailability() |
| 41 | + expect(status.available).toBe(false) |
| 42 | + }) |
| 43 | + |
| 44 | + //#given mcb config with enabled: true |
| 45 | + //#when initializeMcbFromConfig is called |
| 46 | + //#then mcb is available with all tools enabled by default |
| 47 | + it("enables MCB when enabled is true", () => { |
| 48 | + const config: McbConfig = { |
| 49 | + enabled: true, |
| 50 | + url: "http://localhost:3000", |
| 51 | + } |
| 52 | + initializeMcbFromConfig(config) |
| 53 | + |
| 54 | + const status = getMcbAvailability() |
| 55 | + expect(status.available).toBe(true) |
| 56 | + expect(status.tools.search).toBe(true) |
| 57 | + expect(status.tools.memory).toBe(true) |
| 58 | + }) |
| 59 | + |
| 60 | + //#given mcb config with enabled: true and specific tools disabled |
| 61 | + //#when initializeMcbFromConfig is called |
| 62 | + //#then mcb is available but specific tools are disabled |
| 63 | + it("respects per-tool configuration", () => { |
| 64 | + const config: McbConfig = { |
| 65 | + enabled: true, |
| 66 | + tools: { |
| 67 | + memory: false, |
| 68 | + vcs: false, |
| 69 | + search: true, // explicit true |
| 70 | + // index implicit true |
| 71 | + }, |
| 72 | + } |
| 73 | + initializeMcbFromConfig(config) |
| 74 | + |
| 75 | + const status = getMcbAvailability() |
| 76 | + expect(status.available).toBe(true) |
| 77 | + expect(status.tools.memory).toBe(false) |
| 78 | + expect(status.tools.vcs).toBe(false) |
| 79 | + expect(status.tools.search).toBe(true) |
| 80 | + expect(status.tools.index).toBe(true) // default |
| 81 | + }) |
| 82 | + |
| 83 | + //#given mcb is initialized and locked |
| 84 | + //#when time passes (simulated) |
| 85 | + //#then the configuration is NOT overwritten by cache expiration logic |
| 86 | + it("locks configuration against cache expiration", () => { |
| 87 | + // 1. Initialize as disabled |
| 88 | + initializeMcbFromConfig({ enabled: false }) |
| 89 | + |
| 90 | + // 2. Verify disabled |
| 91 | + expect(getMcbAvailability().available).toBe(false) |
| 92 | + |
| 93 | + // 3. Even if we manually reset the internal cache (simulating expiry logic inside availability.ts), |
| 94 | + // the lock should prevent re-enabling if we were able to modify availability.ts to expose it. |
| 95 | + // However, since we can't easily mock the internal state of availability.ts without |
| 96 | + // more complex mocking, we rely on the contract that initializeMcbFromConfig calls lockMcbAvailability. |
| 97 | + |
| 98 | + // Instead, let's verify that calling getMcbAvailability multiple times returns the same result |
| 99 | + for (let i = 0; i < 5; i++) { |
| 100 | + expect(getMcbAvailability().available).toBe(false) |
| 101 | + } |
| 102 | + }) |
| 103 | +}) |
0 commit comments