fed-sx-m1: Step 8d-dispatch-post — format-aware POST /activity (cid_response_for + post_activity_response_for) + 13 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s

This commit is contained in:
2026-05-28 15:39:23 +00:00
parent 1aaede4272
commit dd7b7d7a2d
3 changed files with 212 additions and 9 deletions

View File

@@ -12,7 +12,8 @@
cid_response/1,
accept_format/1, accept_format_from/1,
capabilities_body_for/1,
content_type_for/1, ok_response/2]).
content_type_for/1, ok_response/2,
cid_response_for/2, post_activity_response_for/1]).
%% HTTP request router per design §16.1.
%%
@@ -209,27 +210,30 @@ post_activity_response() ->
handle_post_activity(Req, Cfg) ->
case check_bearer(Req, Cfg) of
ok ->
publish_if_kernel(Req);
F = accept_format_from(Req),
publish_if_kernel(Req, F);
{error, _} ->
unauthorized_response()
end.
%% publish_if_kernel/1 — if the nx_kernel gen_server is registered,
%% publish_if_kernel/2 — if the nx_kernel gen_server is registered,
%% delegate the publish there and translate the result. Otherwise
%% keep the stub response so the auth-only tests stay green without
%% having to spin up a kernel process.
publish_if_kernel(Req) ->
%% having to spin up a kernel process. Format threads through to
%% both stub and CID responses so the Content-Type matches what
%% the client asked for via Accept.
publish_if_kernel(Req, F) ->
case erlang:whereis(nx_kernel) of
undefined ->
post_activity_response();
post_activity_response_for(F);
_Pid ->
Body = field(body, Req),
Request = [{type, create}, {object, Body}],
case nx_kernel:publish(Request) of
{ok, Result} ->
case envelope:get_field(cid, Result) of
{ok, Cid} -> cid_response(Cid);
_ -> post_activity_response()
{ok, Cid} -> cid_response_for(Cid, F);
_ -> post_activity_response_for(F)
end;
{error, _} ->
validation_failed_response()
@@ -423,3 +427,58 @@ ok_response(Body, Format) ->
[{status, 200},
{headers, [{CTKey, content_type_for(Format)}]},
{body, Body}].
%% cid_response_for/2 — format-aware version of cid_response/1.
%% Each variant emits a syntactically appropriate body for the
%% chosen format and tags the response with the matching
%% Content-Type via ok_response/2.
cid_response_for(Cid, text) ->
cid_response(Cid);
%% `{"cid":"<cid>"}\n` — 8-byte prefix + cid + 3-byte suffix
cid_response_for(Cid, json) ->
Pre = <<123,34,99,105,100,34,58,34>>, % '{"cid":"'
Suf = <<34,125,10>>, % '"}\n'
ok_response(<<Pre/binary, Cid/binary, Suf/binary>>, json);
cid_response_for(Cid, activity_json) ->
Pre = <<123,34,99,105,100,34,58,34>>,
Suf = <<34,125,10>>,
ok_response(<<Pre/binary, Cid/binary, Suf/binary>>, activity_json);
%% `(cid "<cid>")\n` — 6-byte prefix + cid + 3-byte suffix
cid_response_for(Cid, sx) ->
Pre = <<40,99,105,100,32,34>>, % '(cid "'
Suf = <<34,41,10>>, % '")\n'
ok_response(<<Pre/binary, Cid/binary, Suf/binary>>, sx);
%% v1 cbor stub: the raw CID bytes with the application/cbor CT.
%% Real cbor encoding (A1 63 cid 78 <len> ...) lands later.
cid_response_for(Cid, cbor) ->
ok_response(Cid, cbor);
cid_response_for(Cid, _) ->
cid_response(Cid).
%% post_activity_response_for/1 — format-aware version of
%% post_activity_response/0 (the kernel-absent stub).
post_activity_response_for(text) ->
post_activity_response();
%% `{"status":"stub"}\n` — hand-spelled
post_activity_response_for(json) ->
Body = <<123,34,115,116,97,116,117,115,34,58,34,
115,116,117,98,34,125,10>>,
ok_response(Body, json);
post_activity_response_for(activity_json) ->
Body = <<123,34,115,116,97,116,117,115,34,58,34,
115,116,117,98,34,125,10>>,
ok_response(Body, activity_json);
%% `(status "stub")\n`
post_activity_response_for(sx) ->
Body = <<40,115,116,97,116,117,115,32,34,
115,116,117,98,34,41,10>>,
ok_response(Body, sx);
post_activity_response_for(cbor) ->
%% Same body as text but with cbor CT — clients see the same
%% bytes as the text fallback. Step 8d-cbor encoder will replace.
[_, _, {body, Body}] = post_activity_response(),
ok_response(Body, cbor);
post_activity_response_for(_) ->
post_activity_response().