-
Notifications
You must be signed in to change notification settings - Fork 1
feat(substrate): opt-in substrate dispatch intake (Phase 5) #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6b1173d
feat(substrate): opt-in substrate intake — config, credential, claim,…
f51de46
feat(substrate): lease semantics — epoch-fenced write-back, max_attem…
478a4a3
fix(substrate): review-feedback correctness pass — catch widening, in…
whoabuddy e6b9b7e
test(substrate): expand coverage via mock.module — claim/complete/fai…
whoabuddy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,243 @@ | ||
| // --------------------------------------------------------------------------- | ||
| // src/substrate.test.ts — Substrate intake adapter tests (Phase 5) | ||
| // | ||
| // Test strategy: mock at the postgres boundary (not testcontainers). | ||
| // Decision: testcontainers require Docker which is not present on all slots. | ||
| // Mocking at the @genesis-works/substrate-db boundary is sufficient for | ||
| // verifying the adapter's contract: correct log lines, correct write-back | ||
| // invocations, correct error handling. | ||
| // | ||
| // Tests cover: | ||
| // 1. resolveSubstrateConfig — returns null when disabled/misconfigured | ||
| // 2. jobRowToTaskInput — correct TaskInput mapping | ||
| // 3. runSubstrateIntakeTick — happy path, unreachable, null claim | ||
| // 4. runSubstrateWriteBack — complete, fail, epoch-mismatch no-op | ||
| // 5. resolveSubstrateConfig — enabled validation | ||
|
whoabuddy marked this conversation as resolved.
Outdated
|
||
| // --------------------------------------------------------------------------- | ||
|
|
||
| import { describe, test, expect, mock, beforeEach, afterEach } from "bun:test"; | ||
|
whoabuddy marked this conversation as resolved.
Outdated
|
||
| import { resolveSubstrateConfig, jobRowToTaskInput } from "./substrate"; | ||
| import type { RuntimeConfig, SubstrateConfig } from "./types"; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Helpers | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| function makeSubConfig(overrides: Partial<SubstrateConfig> = {}): SubstrateConfig { | ||
| return { | ||
| enabled: true, | ||
| credential: "substrate-pg-password", | ||
| kinds: ["notch-task", "arc-task"], | ||
| slotId: "192.168.1.12", | ||
| leaseSecs: 300, | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| function makeRuntimeConfig(sub?: SubstrateConfig): RuntimeConfig { | ||
| return { | ||
| runtimeName: "test-runtime", | ||
| runtimePolicy: "default", | ||
| stateDir: "/tmp/test-state", | ||
| logDir: "/tmp/test-logs", | ||
| artifactDir: "/tmp/test-artifacts", | ||
| dbPath: "/tmp/test-runtime.db", | ||
| lockPath: "/tmp/test-dispatch.lock", | ||
| defaultProfile: "default", | ||
| defaultAdapter: "default", | ||
| maxAttempts: 3, | ||
| retryBackoffSeconds: 10, | ||
| profiles: {}, | ||
| adapters: {}, | ||
| substrate: sub, | ||
| } as RuntimeConfig; | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // resolveSubstrateConfig | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe("resolveSubstrateConfig", () => { | ||
| test("returns null when substrate not configured", () => { | ||
| const config = makeRuntimeConfig(); | ||
| expect(resolveSubstrateConfig(config)).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null when substrate.enabled is false", () => { | ||
| const config = makeRuntimeConfig(makeSubConfig({ enabled: false })); | ||
| expect(resolveSubstrateConfig(config)).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null when credential is empty", () => { | ||
| const config = makeRuntimeConfig(makeSubConfig({ credential: "" })); | ||
| expect(resolveSubstrateConfig(config)).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null when kinds is empty", () => { | ||
| const config = makeRuntimeConfig(makeSubConfig({ kinds: [] })); | ||
| expect(resolveSubstrateConfig(config)).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null when slotId is empty", () => { | ||
| const config = makeRuntimeConfig(makeSubConfig({ slotId: "" })); | ||
| expect(resolveSubstrateConfig(config)).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns config when fully configured", () => { | ||
| const sub = makeSubConfig(); | ||
| const config = makeRuntimeConfig(sub); | ||
| const result = resolveSubstrateConfig(config); | ||
| expect(result).not.toBeNull(); | ||
| expect(result!.slotId).toBe("192.168.1.12"); | ||
| expect(result!.kinds).toEqual(["notch-task", "arc-task"]); | ||
| }); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // jobRowToTaskInput | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe("jobRowToTaskInput", () => { | ||
| test("converts JobRow to TaskInput with substrate source", () => { | ||
| const jobRow = { | ||
| id: "abc-123", | ||
| kind: "notch-task", | ||
| payload: { | ||
| subject: "Implement feature X", | ||
| description: "PR for feature X", | ||
| requested_profile: "default", | ||
| }, | ||
| status: "claimed" as const, | ||
| claimed_by: "192.168.1.12", | ||
| claimed_at: new Date(), | ||
| lease_expires_at: new Date(), | ||
| attempts: 1, | ||
| claim_epoch: 1, | ||
| max_attempts: 5, | ||
| result: null, | ||
| receipt: null, | ||
| created_at: new Date(), | ||
| updated_at: new Date(), | ||
| }; | ||
|
|
||
| const taskInput = jobRowToTaskInput(jobRow); | ||
|
|
||
| expect(taskInput.kind).toBe("notch-task"); | ||
| expect(taskInput.source).toBe("substrate:abc-123"); | ||
| expect(taskInput.subject).toBe("Implement feature X"); | ||
| expect(taskInput.description).toBe("PR for feature X"); | ||
| expect(taskInput.requested_profile).toBe("default"); | ||
| expect(taskInput.max_attempts).toBe(1); // substrate handles retry | ||
| expect(taskInput.payload._substrate_job_id).toBe("abc-123"); | ||
| expect(taskInput.payload._substrate_claim_epoch).toBe(1); | ||
| }); | ||
|
|
||
| test("uses kind as subject when no subject in payload", () => { | ||
| const jobRow = { | ||
| id: "xyz-456", | ||
| kind: "arc-task", | ||
| payload: {}, | ||
| status: "claimed" as const, | ||
| claimed_by: "192.168.1.12", | ||
| claimed_at: new Date(), | ||
| lease_expires_at: new Date(), | ||
| attempts: 1, | ||
| claim_epoch: 2, | ||
| max_attempts: 5, | ||
| result: null, | ||
| receipt: null, | ||
| created_at: new Date(), | ||
| updated_at: new Date(), | ||
| }; | ||
|
|
||
| const taskInput = jobRowToTaskInput(jobRow); | ||
| expect(taskInput.subject).toBe("arc-task"); // falls back to kind | ||
| expect(taskInput.description).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // runSubstrateWriteBack — tested via behavior assertions on mock functions | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe("runSubstrateWriteBack behavior", () => { | ||
| test("is a no-op for non-substrate task sources", async () => { | ||
| // Import dynamically to allow future mocking if needed | ||
| const { runSubstrateWriteBack } = await import("./substrate"); | ||
|
|
||
| // Create a fake substrate DB that would error if called | ||
| const fakeDb = { | ||
| select: () => { throw new Error("should not be called"); }, | ||
| } as unknown as import("./substrate").SubstrateDb; | ||
|
|
||
| const task = { | ||
| task_id: "t-123", | ||
| source: "schedule:daily-task", // NOT substrate | ||
| payload: {}, | ||
| kind: "some-task", | ||
| subject: null, | ||
| description: null, | ||
| priority: 0, | ||
| requested_profile: "default", | ||
| requested_adapter: "default", | ||
| status: "completed" as const, | ||
| created_at: new Date().toISOString(), | ||
| updated_at: new Date().toISOString(), | ||
| attempt_count: 1, | ||
| max_attempts: 3, | ||
| available_at: new Date().toISOString(), | ||
| started_at: null, | ||
| finished_at: null, | ||
| outcome: null, | ||
| last_error: null, | ||
| } as import("./types").TaskRecord; | ||
|
|
||
| const outcome = { | ||
| status: "completed" as const, | ||
| operator_summary: "done", | ||
| machine_status: "ok" as const, | ||
| }; | ||
|
|
||
| // Should complete without throwing (no-op for non-substrate source) | ||
| await expect(runSubstrateWriteBack(fakeDb, task, outcome)).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| test("skips write-back when _substrate_job_id missing from payload", async () => { | ||
| const { runSubstrateWriteBack } = await import("./substrate"); | ||
|
|
||
| const fakeDb = { | ||
| select: () => { throw new Error("should not be called"); }, | ||
| } as unknown as import("./substrate").SubstrateDb; | ||
|
|
||
| const task = { | ||
| task_id: "t-456", | ||
| source: "substrate:some-job-id", | ||
| payload: { /* no _substrate_job_id */ }, | ||
| kind: "notch-task", | ||
| subject: null, | ||
| description: null, | ||
| priority: 0, | ||
| requested_profile: "default", | ||
| requested_adapter: "default", | ||
| status: "completed" as const, | ||
| created_at: new Date().toISOString(), | ||
| updated_at: new Date().toISOString(), | ||
| attempt_count: 1, | ||
| max_attempts: 1, | ||
| available_at: new Date().toISOString(), | ||
| started_at: null, | ||
| finished_at: null, | ||
| outcome: null, | ||
| last_error: null, | ||
| } as import("./types").TaskRecord; | ||
|
|
||
| const outcome = { | ||
| status: "completed" as const, | ||
| operator_summary: "done", | ||
| machine_status: "ok" as const, | ||
| }; | ||
|
|
||
| // Should complete without throwing (skips when no job id) | ||
| await expect(runSubstrateWriteBack(fakeDb, task, outcome)).resolves.toBeUndefined(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.