import { describe, it, expect } from "vitest";

// Unit tests for workflow metadata record structure and ID conventions

describe("workflow metadata: record ID format", () => {
  it("workflow record ID is prefixed with 'wf-'", () => {
    const workflowId = "abc123";
    const recordId = `wf-${workflowId}`;
    expect(recordId).toBe("wf-abc123");
    expect(recordId.startsWith("wf-")).toBe(true);
  });

  it("record ID is deterministic for the same workflow_id", () => {
    const workflowId = "my-workflow";
    expect(`wf-${workflowId}`).toBe(`wf-${workflowId}`);
  });
});

describe("workflow metadata: required fields", () => {
  it("workflow_metadata record contains required fields", () => {
    const record = {
      workflow_id: "wf-abc",
      name: "My Workflow",
      deployed_at: new Date().toISOString(),
      org_namespace: "org-test",
    };
    expect(record.workflow_id).toBeTruthy();
    expect(record.name).toBeTruthy();
    expect(record.deployed_at).toMatch(/^\d{4}-\d{2}-\d{2}T/);
    expect(record.org_namespace).toBeTruthy();
  });

  it("deployed_at is ISO 8601 format", () => {
    const ts = new Date().toISOString();
    expect(ts).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/);
  });
});

describe("workflow metadata: namespace isolation", () => {
  it("get workflow query URL includes org_namespace", () => {
    const orgNamespace = "org-test";
    const workflowId = "abc123";
    const recordId = `wf-${workflowId}`;
    const url = `http://kbdb/records/${encodeURIComponent(recordId)}?org_namespace=${encodeURIComponent(orgNamespace)}`;
    expect(url).toContain("org-test");
    expect(url).toContain("wf-abc123");
  });
});
