Files
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

87 lines
2.9 KiB
Go

package languages
import (
"testing"
"github.com/zzet/gortex/internal/graph"
)
func rtkPlaceholderCount(fix *extractedFixture) int {
n := 0
for _, e := range fix.edgesByKind[graph.EdgeCalls] {
if v, _ := e.Meta["via"].(string); v == "rtk-query" {
n++
}
}
return n
}
func TestRTKQuery_EndpointAndHookNodes(t *testing.T) {
src := "import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';\n" +
"export const api = createApi({\n" +
" baseQuery: fetchBaseQuery({ baseUrl: '/' }),\n" +
" endpoints: (builder) => ({\n" +
" getUser: builder.query({ query: (id) => `u/${id}` }),\n" +
" updateUser: builder.mutation({ query: (u) => ({ url: 'u', method: 'POST' }) }),\n" +
" }),\n" +
"});\n"
fix := runTSExtractFixture(t, "api.ts", src)
ep := fix.nodesByID["api.ts::api.getUser"]
if ep == nil || ep.Meta["rtk_kind"] != "query" {
t.Fatalf("getUser endpoint node missing/wrong: %+v", ep)
}
mut := fix.nodesByID["api.ts::api.updateUser"]
if mut == nil || mut.Meta["rtk_kind"] != "mutation" {
t.Fatalf("updateUser endpoint node missing/wrong: %+v", mut)
}
if h := fix.nodesByID["api.ts::useGetUserQuery"]; h == nil || h.Meta["rtk_generated_hook"] != true {
t.Errorf("useGetUserQuery generated-hook node missing")
}
if h := fix.nodesByID["api.ts::useUpdateUserMutation"]; h == nil || h.Meta["rtk_generated_hook"] != true {
t.Errorf("useUpdateUserMutation generated-hook node missing")
}
if got := rtkPlaceholderCount(fix); got != 2 {
t.Errorf("want 2 hook→endpoint placeholders, got %d", got)
}
}
func TestRTKQuery_HandWrittenHookSuppressesSynthetic(t *testing.T) {
src := "import { createApi } from '@reduxjs/toolkit/query/react';\n" +
"function useGetUserQuery() { return null; }\n" +
"export const api = createApi({\n" +
" endpoints: (builder) => ({\n" +
" getUser: builder.query({ query: () => 'u' }),\n" +
" }),\n" +
"});\n"
fix := runTSExtractFixture(t, "api.ts", src)
if fix.nodesByID["api.ts::api.getUser"] == nil {
t.Fatalf("endpoint node should still be minted")
}
for _, n := range fix.nodesByID {
if n.Name == "useGetUserQuery" && n.Meta["rtk_generated_hook"] == true {
t.Errorf("hand-written useGetUserQuery must suppress the synthetic hook node")
}
}
if got := rtkPlaceholderCount(fix); got != 0 {
t.Errorf("no rtk-query placeholder when the hook is hand-written, got %d", got)
}
}
func TestRTKQuery_JSReturnBlockShape(t *testing.T) {
// JS extractor + builder arrow with an explicit return block.
src := "const api = createApi({\n" +
" endpoints: (build) => {\n" +
" return { listItems: build.query({ query: () => 'items' }) };\n" +
" },\n" +
"});\n"
fix := runJSExtractFixture(t, "api.js", src)
if fix.nodesByID["api.js::api.listItems"] == nil {
t.Errorf("endpoint from a return-block builder not detected")
}
if fix.nodesByID["api.js::useListItemsQuery"] == nil {
t.Errorf("generated hook for return-block builder not detected")
}
}