Database-BI
view release on metacpan or search on metacpan
spec_key => 'table:sales',
label => 'sales',
columns => \@COLS,
ai_config => { api_key => 'test-key' },
);
is($r->{error}, 'ask_unparseable', 'non-JSON response -> ask_unparseable');
}
# --- provider returns 500 -> ask_provider_error ---
{
local *LWP::UserAgent::request = mock_llm_failure(500);
my $r = Database::BI::Ask::ask(
question => 'something',
spec_key => 'table:sales',
label => 'sales',
columns => \@COLS,
ai_config => { api_key => 'test-key' },
);
is($r->{error}, 'ask_provider_error', '500 from provider -> ask_provider_error');
}
# --- spec datasets and version ---
{
local *LWP::UserAgent::request = mock_llm_response(
'{"kind":"pie","title":"Sales by region","encode":{"cat":"region","val":"amount"}}'
);
my $r = Database::BI::Ask::ask(
question => 'pie chart',
spec_key => 'table:sales',
label => 'sales',
columns => \@COLS,
ai_config => { api_key => 'test-key' },
);
is(ref $r->{spec}{datasets}, 'ARRAY', 'spec has datasets array');
is($r->{spec}{datasets}[0]{spec}, 'table:sales', 'spec dataset has correct spec_key');
is($r->{spec}{version}, 1, 'spec version=1');
}
# ---------------------------------------------------------------------------
# Section 4: URL construction correctness
# ---------------------------------------------------------------------------
{
# spec_key with special chars must be percent-encoded
local *LWP::UserAgent::request = mock_llm_response(
'{"kind":"pie","title":"T","encode":{"cat":"region"}}'
);
my $r = Database::BI::Ask::ask(
question => 'q',
spec_key => 'path:/my data/file.csv',
columns => [{ name => 'region', type => 'string' }],
ai_config => { api_key => 'k' },
);
is($r->{valid}, 1, 'URL build: valid');
# path:/my data/file.csv -> l=path%3A%2Fmy%20data%2Ffile.csv
like($r->{url}, qr{l=path%3A}, 'URL build: colon percent-encoded in l=');
like($r->{url}, qr{%20}, 'URL build: space percent-encoded');
}
# ---------------------------------------------------------------------------
# Section 5: HTTP endpoint tests (no ai_api_key configured -> 503)
# ---------------------------------------------------------------------------
{
# Without ai_api_key the app returns 503
$t->post_ok('/api/ask', form => { l => 'table:sales', q => 'test' })
->status_is(503)
->json_like('/error', qr/not available/i);
}
# ---------------------------------------------------------------------------
# Section 6: HTTP -- missing parameter guards
# ---------------------------------------------------------------------------
{
# Set a fake ai_api_key so the endpoint proceeds past the 503 guard
my $saved = $t->app->config->{ai_api_key};
$t->app->config->{ai_api_key} = 'fake-test-key';
$t->post_ok('/api/ask', form => { q => 'test question' })
->status_is(400)
->json_like('/error', qr/l/i, 'missing l= -> 400');
$t->post_ok('/api/ask', form => { l => 'table:sales' })
->status_is(400)
->json_like('/error', qr/question/i, 'missing q= -> 400');
# Restore
if (defined $saved) {
$t->app->config->{ai_api_key} = $saved;
} else {
delete $t->app->config->{ai_api_key};
}
}
# ---------------------------------------------------------------------------
# Section 7: HTTP -- valid request with mocked LWP
# ---------------------------------------------------------------------------
SKIP: {
skip 'data/sales.csv not found', 5 unless -f 'data/sales.csv';
my $saved = $t->app->config->{ai_api_key};
$t->app->config->{ai_api_key} = 'fake-test-key';
{
local *LWP::UserAgent::request = mock_llm_response(
'{"kind":"bar","title":"Sales by region","encode":{"cat":"region","val":"amount"}}'
);
$t->post_ok('/api/ask', form => { l => 'table:sales', q => 'bar chart' })
->status_is(200)
->json_is('/valid', 1)
->json_like('/url', qr{^/bar\?})
->json_like('/spec/name', qr/Sales by region/);
}
if (defined $saved) {
$t->app->config->{ai_api_key} = $saved;
} else {
delete $t->app->config->{ai_api_key};
}
}
# ---------------------------------------------------------------------------
# Section 8: Dashboard shows btn-ask when ai_available, absent otherwise
# ---------------------------------------------------------------------------
{
my $saved = $t->app->config->{ai_api_key};
delete $t->app->config->{ai_api_key};
$t->get_ok('/view/sales')
->status_is(200)
->content_unlike(qr/id="btn-ask"/, 'btn-ask absent when no ai_api_key');
$t->app->config->{ai_api_key} = 'fake-key';
( run in 0.843 second using v1.01-cache-2.11-cpan-eb134014566 )