Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/components/modals/add-source-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function AddSourceForm() {
const { close, open: openModal } = useModalStore()
const { budget, setBudget, pubKey, routeHint, isAdmin } = useUserStore()
const refreshBalance = useUserStore((s) => s.refreshBalance)
const activeSkin = useAppStore((s) => s.activeSkin)
const [sourceUrl, setSourceUrl] = useState("")
const [detectedType, setDetectedType] = useState<SourceType | null>(null)
const [detecting, setDetecting] = useState(false)
Expand Down Expand Up @@ -195,15 +196,20 @@ export function AddSourceForm() {
if (!contentType) {
throw new Error(`Unsupported source type: ${sourceType}`)
}
// Legal skin: submit PDFs as LegalDocument instead of generic Document
const effectiveContentType =
activeSkin === "legal" && contentType === "document"
? "legal_document"
: contentType
const body: Record<string, unknown> = {
content_type: contentType,
content_type: effectiveContentType,
source_link: source,
}
if (fullPubkey) body.pubkey = fullPubkey

await api.post("/v2/content", body, headers)
},
[pubKey, routeHint, topics, category, weight, cacheStatus, cachedRefId]
[pubKey, routeHint, topics, category, weight, cacheStatus, cachedRefId, activeSkin]
)

const handleSubmit = useCallback(async () => {
Expand Down
119 changes: 116 additions & 3 deletions src/lib/__tests__/add-content-modal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,26 @@ vi.mock("@/stores/user-store", () => ({
const mockSetMyContentOpen = vi.fn()
const mockBumpMyContentRefresh = vi.fn()

let mockActiveSkinInContent = "default"

vi.mock("@/stores/app-store", () => {
const getState = () => ({
setMyContentOpen: mockSetMyContentOpen,
bumpMyContentRefresh: mockBumpMyContentRefresh,
activeSkin: mockActiveSkinInContent,
})
return {
useAppStore: {
getState,
},
useAppStore: Object.assign(
(sel?: (s: unknown) => unknown) => {
const state = {
setMyContentOpen: mockSetMyContentOpen,
bumpMyContentRefresh: mockBumpMyContentRefresh,
activeSkin: mockActiveSkinInContent,
}
return sel ? sel(state) : state
},
{ getState }
),
}
})

Expand Down Expand Up @@ -458,3 +469,105 @@ describe("AddContentModal — admin category/weight fields", () => {
expect(screen.queryByPlaceholderText(/0\.0 – 1\.0/i)).not.toBeInTheDocument()
})
})

// ---------------------------------------------------------------------------
// AddSourceForm — content_type skin-awareness
// ---------------------------------------------------------------------------

describe("AddSourceForm — content_type overridden by active skin", () => {
beforeEach(() => {
vi.clearAllMocks()
mockActiveSkinInContent = "default"
mockGetL402.mockResolvedValue(null)
mockGetPrice.mockResolvedValue(0)
mockApiPost.mockResolvedValue({})
mockRefreshBalance.mockResolvedValue(undefined)
mockIsSubscriptionSource.mockReturnValue(false)
mockCheckNodeExists.mockResolvedValue({ exists: false, ref_id: null, status: null })
})

it("sends content_type 'legal_document' when legal skin is active and source is a PDF URL", async () => {
vi.useFakeTimers({ shouldAdvanceTime: true })
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime.bind(vi) })

mockActiveSkinInContent = "legal"
mockDetectSourceType.mockResolvedValue("document")

render(<AddSourceForm />)
const input = screen.getByPlaceholderText(/Paste URL/)
await user.type(input, "https://example.com/contract.pdf")

await waitFor(() => {
expect(screen.getByRole("button", { name: /add source/i })).not.toBeDisabled()
})

await user.click(screen.getByRole("button", { name: /add source/i }))

await waitFor(() => {
expect(mockApiPost).toHaveBeenCalledWith(
"/v2/content",
expect.objectContaining({ content_type: "legal_document" }),
expect.anything()
)
})

vi.useRealTimers()
})

it("sends content_type 'document' when default skin is active and source is a PDF URL", async () => {
vi.useFakeTimers({ shouldAdvanceTime: true })
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime.bind(vi) })

mockActiveSkinInContent = "default"
mockDetectSourceType.mockResolvedValue("document")

render(<AddSourceForm />)
const input = screen.getByPlaceholderText(/Paste URL/)
await user.type(input, "https://example.com/contract.pdf")

await waitFor(() => {
expect(screen.getByRole("button", { name: /add source/i })).not.toBeDisabled()
})

await user.click(screen.getByRole("button", { name: /add source/i }))

await waitFor(() => {
expect(mockApiPost).toHaveBeenCalledWith(
"/v2/content",
expect.objectContaining({ content_type: "document" }),
expect.anything()
)
})

vi.useRealTimers()
})

it("sends content_type 'audio_video' for YouTube URL regardless of legal skin", async () => {
vi.useFakeTimers({ shouldAdvanceTime: true })
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime.bind(vi) })

mockActiveSkinInContent = "legal"
mockDetectSourceType.mockResolvedValue("youtube_video")
mockCheckNodeExists.mockResolvedValue({ exists: false, ref_id: null, status: null })

render(<AddSourceForm />)
const input = screen.getByPlaceholderText(/Paste URL/)
await user.type(input, "https://youtube.com/watch?v=abc123")

await waitFor(() => {
expect(screen.getByRole("button", { name: /add source/i })).not.toBeDisabled()
})

await user.click(screen.getByRole("button", { name: /add source/i }))

await waitFor(() => {
expect(mockApiPost).toHaveBeenCalledWith(
"/v2/content",
expect.objectContaining({ content_type: "audio_video" }),
expect.anything()
)
})

vi.useRealTimers()
})
})
Loading